【问题标题】:Jar packaging in Spring bootSpring Boot 中的 Jar 包装
【发布时间】:2018-04-01 20:35:58
【问题描述】:

我正在创建一个 Spring Boot 多模块项目。模块 A 依赖于模块 B ..

当我以分解形式运行时,模块 A 运行良好..

          mvn spring-boot:run

但我无法将 jar 与依赖项一起打包 这样我就可以将其执行为

          java -jar Module-A.jar

Module A pom.xml 如下:

 <parent>
    <artifactId>Module-Test</artifactId>
    <groupId>com.module</groupId>
    <version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>Module-A</artifactId>
<packaging>jar</packaging>

<properties>
    <start-class>com.NotifyApp</start-class>
    <main.class>com.NotifyApp</main.class>
</properties>

<dependencies>
    <dependency>
        <groupId>com.module</groupId>
        <artifactId>Module-B</artifactId>
        <version>${project.version}</version>
    </dependency>
</dependencies>

<build>
    <plugins>
    <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>
        </plugin>
    </plugins>
</build>

未能执行目标 org.springframework.boot:spring-boot-maven-plugin:1.4.2.RELEASE:repackage (default) on project Module-A: 执行默认目标 org.springframework.boot:spring-boot -maven-plugin:1.4.2.RELEASE:repackage failed: Source must refe r 到现有文件

父 pom.xml

 <modelVersion>4.0.0</modelVersion>
<artifactId>Module-Test</artifactId>
<groupId>com.module</groupId>
<version>1.0.0-SNAPSHOT</version>
<modules>
    <module>Module-A</module>
    <module>Module-B</module>
    <module>Module-C</module>        
</modules>
<packaging>pom</packaging>

<parent>
    <artifactId>Module-Parent</artifactId>
    <groupId>com.parent</groupId>
    <version>1.0.2</version>
</parent>


<dependencyManagement>
    <dependencies>
        <dependency>
           <groupId>com.module</groupId>
            <artifactId>Module-A</artifactId>
            <version>${project.version}</version>
        </dependency>
       <dependency>
           <groupId>com.module</groupId>
            <artifactId>Module-B</artifactId>
            <version>${project.version}</version>
        </dependency>
          <dependency>
           <groupId>com.module</groupId>
            <artifactId>Module-C</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.module</groupId>
            <artifactId>starter-service</artifactId>
            <version>${starter.version}</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>

【问题讨论】:

  • 您执行哪个命令以及从哪个位置执行?
  • 我在 Module-A 目录中执行 mvn clean install parallel(与 pom.xml 平行).. 这是在 Intellij idea 终端上完成的
  • 好的。您可能会遇到来自父 pom 的一些问题。可以展示一下吗?
  • 我已经用父 pom.xml 更新了问题

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


【解决方案1】:

我通常使用这个插件https://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-project-dependencies.html for maven,它会在你运行时将所有依赖jar放入target/dependencies

mvn clean package

然后我将 jar 复制到一个文件夹(我通常称为 lib)中,并将 lib/* 包含在我的类路径中。最终的运行脚本通常看起来像

java -cp lib/*: my.package.MySpringBootMain

【讨论】:

  • 太棒了..这帮助我以两种方式运行我的 jar ..但我仍然不确定为什么 spring boot maven 插件在我的情况下不支持这种包装..我接受你的回答(您在链接中提供的那个)因为它解决了我的问题..
【解决方案2】:

删除

<executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>

不需要。

【讨论】:

  • 我删除了它..我在 target/Module-A.jar 中获取了 jar 文件..但它不包含打包在其中的依赖模块..我简短地说,我需要一个胖 jar Module-A 包含 Module-B 及其依赖项..
【解决方案3】:

在模块A的pom.xml中添加模块B的依赖

<dependencies>
    <dependency>
        <groupId>com.module</groupId>
        <artifactId>Module-B</artifactId>
        <version>${project.version}</version>
    </dependency>
</dependencies>

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

对模块 B 使用 ma​​ven clean compile package install 命令,首先构建模块 B,然后在模块 A 上运行 ma​​ven clean compile package。这会将模块 B 输出到模块 A。

模块 B 的 pom.xml 中不需要模块 A 的引用

【讨论】:

  • 对不起不要运行mvn clean compile package install,这是很多部分的重复。只需要简单的mvn clean package..当然使用重新打包spring-boot-maven-plugin...mvn clean compile package也只是在复制mvn clean package..life cycle contains compile
【解决方案4】:

您当然知道,但您可以从此链接初始化您的项目 Spring Initializr ,它会自动生成你需要的配置

【讨论】:

    猜你喜欢
    • 2018-09-26
    • 2015-06-10
    • 2017-09-30
    • 2014-02-10
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 2018-03-05
    相关资源
    最近更新 更多