【问题标题】:How to develop a Maven Multi-module application using the Spring Boot framework如何使用 Spring Boot 框架开发 Maven 多模块应用程序
【发布时间】:2017-05-23 01:14:02
【问题描述】:

我有一个运行良好的 Maven 多模块 Java 项目,直到我尝试包含 Spring Boot 框架。名为 core 的主模块调用正常,但其余所有模块都无法实现我在 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";
    }
}

ModuleServiceImpl.javamodule1

@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>

【问题讨论】:

  • F:/mvnmodularapp/module1/src/main/java/service/impl/Service.java:[13,5] 在编译错误中,在这个包的问题中编辑的类是ServiceImpl。不一致。
  • 对不起。我修好了。

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


【解决方案1】:

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

您在每个模块中声明:

<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 之前可以工作,我认为应该通过以前的建议来解决问题。
但如果这还不够,下面是其他可能导致编译错误的猜测。我依赖这个错误信息:

[错误] /F:/mvnmodularapp/module1/src/main/java/service/impl/ModuleServiceImpl.java:[12,30] 找不到符号 [ERROR] 符号:服​​务类

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

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

【讨论】:

  • 实际上你可以使用spring-boot-starter-parent作为你的应用父模块的父模块,并从spring父模块的依赖管理中受益。
  • @StasKolodyuk 确实足够了。感谢您的反馈。我因此更新了我的答案。
猜你喜欢
  • 2019-08-13
  • 2017-02-09
  • 2019-03-26
  • 2016-12-30
  • 1970-01-01
  • 1970-01-01
  • 2017-04-26
  • 2010-10-16
  • 1970-01-01
相关资源
最近更新 更多