Spring 引导中的 Jar 打包

Jar packaging in Spring boot

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

模块 A 运行 当我 运行 处于分解形式时很好..

          mvn spring-boot:run

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

          java -jar Module-A.jar

模块 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:在项目 Module-A 上重新打包(默认):目标的执行默认值 org.springframework.boot :spring-boot-maven-plugin:1.4.2.RELEASE:重新打包失败:源必须引用 r 到现有文件

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

删除

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

不需要。

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

在模块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使用maven clean compile package install命令,先构建模块B再运行 maven clean compile package 在模块 A 上。这会将模块 B 输出到模块 A。

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

我通常将这个插件https://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-project-dependencies.html用于maven,它会在你运行

时将所有依赖jar放入target/dependencies
mvn clean package

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

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