【问题标题】:Spring Boot multi module Maven project deploymentSpring Boot多模块Maven项目部署
【发布时间】:2017-02-02 09:01:24
【问题描述】:

我们有包含以下模块的多模块 Maven 项目:

  • 通用
  • 型号
  • 存储库
  • 服务
  • 网络

我们在谷歌上搜索过,但没有找到解决方案如何在项目具有这种结构时制作一个可执行的 jar。

这里是父 pom.xml:

<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>...</groupId>
    <artifactId>...</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>Model</module>
        <module>Web</module>
        <module>Service</module>
        <module>Repository</module>
        <module>Common</module>
    </modules>
    <packaging>pom</packaging>

    <name>Api</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>

        <mssql.jdbc.driver.version>4.2</mssql.jdbc.driver.version>

        <apache.commons.lang.version>3.3.2</apache.commons.lang.version>
        <apache.commons.collection.utils.version>4.1</apache.commons.collection.utils.version>
    </properties>

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

    <dependencies>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-envers</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

    </dependencies>

</project>

所以,非常基本的父 pom.xml。我们知道打包为 pom 时不能使用 Spring Boot Maven 插件,所以我们在 web 模块的 pom.xml 中定义了它:

<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>...</artifactId>
        <groupId>...</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <packaging>jar</packaging>

    <name>...</name>

    <dependencies>

        <dependency>
            <groupId>...</groupId>
            <artifactId>Service</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.4.1.RELEASE</version>
                <configuration>
                    <mainClass>...App</mainClass>
                </configuration>
                <!--<executions>-->
                    <!--<execution>-->
                        <!--<goals>-->
                            <!--<goal>repackage</goal>-->
                        <!--</goals>-->
                    <!--</execution>-->
                <!--</executions>-->
            </plugin>
        </plugins>
    </build>

</project>

其他模块的 pom.xml 的其余部分几乎相同:Service 的 pom.xml 包括 Repository.jar,Repository 包括 Model 和 Common。

问题 1:当我们运行 mvn install 时,插件在 web 模块的目标文件夹中创建了 fat jar。但是当我们尝试使用java -jar name-of-jar 运行它时,它会给出关于 Service 模块中某些类的 java.lang.NoClassDefFoundError - 您可以从 Web 的 pom.xml 中看到包含 Service 模块(还有一个打包为库的 Service.jar在 Web.jar 中)

问题 2:有趣的是 Web.jar 中的 Service.jar 还包含 Web.jar 中已经存在的几乎所有依赖项 - 基本上它们是重复的。

是否有人设法使用 Spring Boot Maven 插件和 Maven 模块项目结构创建了可执行的 fat jar?

【问题讨论】:

  • 我有一种预感,devtools 可能是这一切的根源(因此我的回答)。 NoClassDefFound 表示已找到该类,但在其初始化期间失败。我猜这可能是由 WAR 和 Service.jar 中包含的重复 jar 引起的。不同的类加载器中可能存在类的不匹配并导致 NoClassDefFound ?同样,如果即使在移动开发工具后错误仍然存​​在,请提供堆栈跟踪。
  • 是的,这解决了这两个问题,谢谢。

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


【解决方案1】:

问题 1 & 2

您不应该将 devtools 作为父 pom 中的依赖项。 将以下内容移至web/spring-boot 模块:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
</dependency>

如果你看devtools's pom,它包括以下内容:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
</dependency>

所以你的服务 jar 最终会带有一些 spring boot 依赖项。

【讨论】:

  • 我知道这是重复依赖项的原因,但是将 devtools 依赖项移动到 Web 模块对构建可执行 jar 有何帮助?
  • 您是否尝试过移动依赖并重建?
猜你喜欢
  • 2015-10-10
  • 2014-06-02
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 2018-10-30
  • 1970-01-01
  • 2016-09-13
  • 1970-01-01
相关资源
最近更新 更多