Spring启动时的Jar包装

时间:2017-10-20 16:05:59

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

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

当我以爆炸形式运行时,模块A运行良好..

          mvn spring-boot:run

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

          java -jar Module-A.jar

模块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:项目上的重新打包(默认)模块-A:目标org.springframework.boot的执行默认值:spring-boot -maven-plugin:1.4.2.RELEASE:重新包装失败:来源必须参考 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>

4 个答案:

答案 0 :(得分:1)

我通常将此插件https://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-project-dependencies.html用于maven,它会在您运行时将所有依赖项jar放入目标/依赖项中

mvn clean package

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

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

答案 1 :(得分:0)

删除

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

不需要。

答案 2 :(得分:0)

您确定知道,但您可以通过此链接初始化您的项目 Spring Initializr ,它会根据您的需要自动生成配置

答案 3 :(得分:0)

在模块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,然后在模块A上运行 maven clean compile package 。这将模块B输入模块A.

模块B的pom.xml中没有模块A的引用