简体   繁体   English

无法在多模块项目中正确打包Spring Boot应用程序

[英]Cannot package Spring Boot application properly in a multi module project

Basically, I have a multi module project as follows: 基本上,我有一个多模块项目,如下所示:

parent-project
├── core-app
└── web-app

core-app and web-app are both Spring Boot projects, and their parent is parent-project instead of spring-boot-starter-parent . core-appweb-app都是Spring Boot项目,它们的父级是parent-project而不是spring-boot-starter-parent So because of it, they both have: 因此,由于它们,它们都具有:

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <type>pom</type>
                <version>${spring.boot.version}</version>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

in their pom.xml , as well as: 在他们的pom.xml ,以及:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring.boot.version}</version>
    <configuration>
        <executable>true</executable>
    </configuration>
</plugin>

This setup perfectly runs within IntelliJ, yet, if I package the application, I get a very small .jar and basically if I try to run it executable, I get several command not found errors. 此设置可在IntelliJ内完美运行,但是,如果我package应用程序,则会得到一个非常小的.jar并且基本上,如果我尝试以可执行文件身份运行它,则会收到一些command not found错误。

If I run it with java -cp my.jar com.Main I get SpringApplication not found . 如果我使用java -cp my.jar com.Main运行它, java -cp my.jar com.Main得到SpringApplication not found So from this, I understand that the core-app and web-app doesn't have all the dependencies within the jar. 因此,据此我了解到, core-appweb-app在jar中并不具有所有依赖关系。

If I build it into a standalone .jar with spring-boot-starter-parent as its parent; 如果我将其构建为以spring-boot-starter-parent的独立.jar it works. 有用。

What am I doing wrong? 我究竟做错了什么? Thanks. 谢谢。

Update: 更新:

Now that was what's happening when I mvn package . 现在,这就是我mvn package发生的情况。 However, when I actually execute mvn spring-boot:repackage I am getting: 但是,当我实际执行mvn spring-boot:repackage我得到:

Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage failed: Source must refer to an existing file

As suggested here this seems to solve the issue, yet I doubt that it might not be the standard way. 正如这里建议的那样, 似乎可以解决问题,但我怀疑这可能不是标准方法。 For example: 例如:

mvn clean package spring-boot:repackage does the trick, however, I cannot mvn clean && mvn package && mvn spring-boot:repackage . mvn clean package spring-boot:repackage可以解决问题,但是我不能mvn clean && mvn package && mvn spring-boot:repackage I would appreciate an explanation on this. 我希望对此做出解释。 Thanks! 谢谢!

Maven projects can only have one parent, and your "modules" can't be modules unless parent-project is the parent. Maven项目只能有一个父项,除非parent-project是父项,否则您的“模块”不能是模块。 You can theoretically make project-parent extend the Boot parent, but that can get complicated in a hurry. 从理论上讲,您可以使project-parent扩展Boot父级,但是这可能会很麻烦。

Instead, simply go ahead and use the Boot dependencies and inclusions selectively. 相反,只需继续并选择性地使用Boot依赖项和包含项。 Use the dependencyManagement and BOM import in parent-project , pull in only the dependencies actually needed for each module (probably including spring-boot-starter-web for your fat jar), and add the Maven plugin for standalone far-jar modules only : 使用parent-projectdependencyManagement和BOM导入,仅引入每个模块实际需要的依赖(可能为胖子添加了spring-boot-starter-web ),并仅为独立的far-jar模块添加Maven插件:

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

Then you'll get standard-sized (usually a few K) dependency jars and the whole treatment specifically where it's required. 然后,您将获得标准大小(通常为K个)的依赖项jar,以及整个处理过程,特别是在需要的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM