如何使用Spring Boot框架开发Maven多模块应用程序

时间:2017-01-08 19:22:02

标签: java spring maven modularity spring-boot-maven-plugin

我有一个完美运行的maven多模块Java项目,直到我尝试包含Spring Boot框架。名为 core 的主模块被称为OK,但其余的都无法实现我在 Core.java 中声明的接口。

抛出此错误:

[ERROR] /F:/mvnmodularapp/module1/src/main/java/service/impl/ModuleServiceImpl.java:[12,30] cannot find symbol
[ERROR] symbol: class Service

[ERROR] /F:/mvnmodularapp/module1/src/main/java/service/impl/ModuleServiceImpl.java:[13,5] method does not override or implement a method from a supertype

项目结构:

mvnmodularapp/
             /pom
mvnmodularapp/core/
             /src
             /target/core-1.0-SNAPSHOT.jar
mvnmodularapp/module1
             /src
             /target/module1-1.0-SNAPSHOT.jar

文件/目录结构:

core/src/java/service/
                    Service.java

core/src/java/service/impl
                        CoreServiceImpl.java

module1/src/java/service/impl
                        ModuleServiceImpl.java

Service.java

public interface Service {

    String getName();
}
核心

中的

CoreServiceImpl.java

@Service
public class ServiceImpl implements Service {
    @Override
    public String getName() {
        return "Hi. You called me from ServiceImpl in Core";
    }
}
module1

中的

ModuleServiceImpl.java

@Service
public class ModuleServiceImpl implements Service {
    @Override
    public String getName() {
        return "Hi. You called me from ModuleServiceImpl in module1, but I can\'t pick up right now. Sorry";
    }
}

POMS

mvnmodularapp pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mvnmodularapp</groupId>
    <artifactId>mvnmodularapp</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>core</module>
        <module>module1</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

</project>

核心pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <artifactId>mvnmodularapp</artifactId>
        <groupId>com.mvnmodularapp</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>core</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>Core</name>
    <description>SudenGut application</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <start-class>main</start-class>
        <java.version>1.8</java.version>
        <tomcat.version>8.0.24</tomcat.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--- this will not be enough to provide a cool app :) -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

module1 pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mvnmodularapp</artifactId>
        <groupId>com.mvnmodularapp</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>module1</artifactId>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <start-class>baseview.impl.BaseViewImpl.ModuleServiceImpl</start-class>
        <java.version>1.8</java.version>
        <tomcat.version>8.0.24</tomcat.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--- this will not be enough to provide a cool app :) -->
        <dependency>
            <groupId>com.mvnmodularapp</groupId>
            <artifactId>core</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

1 个答案:

答案 0 :(得分:2)

模块之间的依赖关系已正确声明,但您有一件事未按要求配置 我不确定它会解决你的问题,但它可以。

您在每个模块中声明:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

仅应用于引导Spring Boot应用程序的模块 如果您希望在同一个Spring Boot容器中有多个Web应用程序,则可以read this question

正如你所说的那样,在添加Spring Boot之前它可行。我认为问题应该通过之前的建议来解决 但如果还不够,下面是其他猜测可能是编译错误的原因。我依赖于此错误消息:

  

[ERROR]   /F:/mvnmodularapp/module1/src/main/java/service/impl/ModuleServiceImpl.java:[12,30]   找不到符号[ERROR]符号:class service

  • Service类未导入ModuleServiceImpl 如果我依赖你在问题中写的内容,你应该添加: 导入import service.Service中的ModuleServiceImpl

  • 核心模块不会使用service.Service类创建JAR。
    因此,导入它并不能解决module1模块的问题 您应该从核心模块执行mvn clean install并检查jar是否包含已编译的类:service.Service在良好的包中。