java 使用 Spring-Boot 创建一个简单的 JAR 而不是一个可执行的 JAR
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38312448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a simple JAR instead of an executable JAR with Spring-Boot
提问by Johannes Esseling
I have a problem with a Multi-Module Spring-Boot Application.
我有一个多模块 Spring-Boot 应用程序的问题。
I have one Module that I use for Services, the core-Module. And one module for View-Related Classes, the web-Module. The are both in a parent-Module, where I add the dependencies, like the "spring-boot-starter" and that can be used by both modules.
我有一个用于服务的模块,即核心模块。和视图相关类的一个模块,web-Module。它们都在父模块中,我在其中添加了依赖项,例如“spring-boot-starter”,并且可以由两个模块使用。
Now to the problem:
现在问题来了:
I want to run the web-Module with the embedded Tomcat and have the core-Module as a dependency in the web-module.
我想使用嵌入式 Tomcat运行web-Module,并将核心-Module 作为web-module 中的依赖项。
In other Spring projects I would just include the maven-jar-pluginand create a jar of the core-Module.
在其他 Spring 项目中,我只会包含maven-jar-plugin并创建一个核心模块的 jar 。
The problem in this Spring-Boot project is that the maven-jar-pluginis already configured, in the "spring-boot-starter". And it needs a mainClass, which only the web-module has.
这个 Spring-Boot 项目的问题是maven-jar-plugin已经在“spring-boot-starter”中配置好了。它需要一个 mainClass,只有web模块才有。
Small excerpt from the "spring-boot-starter"-POM
摘自“spring-boot-starter”-POM
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>${start-class}</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
Is there a way to package the core-Module as a JAR without needing a "start-class" in that module?
有没有办法将核心-Module打包为 JAR 而不需要该模块中的“起始类”?
回答by Tom Saleeba
It seem like you can disable the fat-JAR from replacing the original and getting installed into the repo by configuring the spring-boot-maven-plugin
.
似乎您可以通过配置spring-boot-maven-plugin
.
Taken from http://docs.spring.io/spring-boot/docs/1.5.1.RELEASE/maven-plugin/examples/repackage-disable-attach.html:
By default, the repackage goal will replace the original artifact with the executable one. If you need to only deploy the original jar and yet be able to run your app with the regular file name, configure the plugin as follows:
默认情况下,重新打包目标将用可执行文件替换原始工件。如果您只需要部署原始 jar 并且能够使用常规文件名运行您的应用程序,请按如下方式配置插件:
<project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.1.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<attach>false</attach>
</configuration>
</execution>
</executions>
...
</plugin>
...
</plugins>
...
</build>
...
</project>
This configuration will generate two artifacts: the original one and the executable counter part produced by the repackage goal. Only the original one will be installed/deployed.
此配置将生成两个工件:原始工件和由重新打包目标生成的可执行计数器部分。只会安装/部署原始的。
回答by SeB.Fr
You can just disable the spring-boot-maven plugin this way.
您可以通过这种方式禁用 spring-boot-maven 插件。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
回答by zhuguowei
You can simply change
你可以简单地改变
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
to
到
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
回答by JudgingNotJudging
If you're using the embedded tomcat to run your app, don't you just want a standard Spring Boot fat jar for your web app? If so, just mark your web module as dependent on your core module in the pom, and build your project.
如果您使用嵌入式 tomcat 来运行您的应用程序,您不只是想要一个标准的 Spring Boot fat jar 用于您的 Web 应用程序吗?如果是这样,只需在 pom 中将您的 web 模块标记为依赖于您的核心模块,然后构建您的项目。
It's there a different use case that you need the jars separated?
是否有不同的用例需要将罐子分开?
Are you building your modules as completely separate modules or as modules as part of a single multi-module project? The difference is in the latter, you will have a pom at the root specifying the modules. I forget the Maven syntax specifically, so my example is Gradle (Maven docs for multi-module builds are here). Sorry about that.
您是将模块构建为完全独立的模块还是作为单个多模块项目的一部分构建的?区别在于后者,您将在根目录下有一个 pom 指定模块。我特别忘记了 Maven 语法,所以我的例子是 Gradle(多模块构建的 Maven 文档在这里)。对于那个很抱歉。
baseProject
|----web-module
|----core-module
baseProject's build.gradle:
baseProject 的 build.gradle:
project(':web-module') {
dependencies {
compile project(':core-module')
}
evaluationDependsOn(':core-module')
}
Maven has a similar structure. You should review the docs, but I believe all you need to do is specify the module order correctly in your parent pom as below and include the dependency in your web-module pom.
Maven 具有类似的结构。您应该查看文档,但我相信您需要做的就是在您的父 pom 中正确指定模块顺序,如下所示,并在您的 web 模块 pom.xml 中包含依赖项。
Parent pom:
父pom:
...
<modules>
<module>core-module</module>
<module>web-module</module>
</modules>
web-pom:
网络pom:
<project ...>
<parent>
<groupId>com.example</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>web-module</artifactId>
<packaging>war</packaging>
<name>My web-module</name>
<dependencies>
...
<dependency>
<groupId>com.example</groupId>
<artifactId>core-module</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
...
core pom should only need to include the parent
section as in the web-module above, and otherwise be the same.
core pom 应该只需要包含parent
上面 web-module 中的部分,否则是相同的。
回答by Johannes Esseling
I have found the answer. I configured my application just like @judgingnotjudging explained. The difference was that I had put this, in the parent-POM:
我找到了答案。我像@judgingnotjudging 解释的那样配置了我的应用程序。不同之处在于我把它放在了父级-POM 中:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
It was preventing the default creation of JARs in the children-modules. I could resolve this problem by including this onlyin the web-Module.
它阻止了子模块中 JAR 的默认创建。我可以通过只在web-Module 中包含它来解决这个问题。
That way Spring-Boot builds a fat-JAR from the web-Module and a simple JAR from the core-Module.
这样 Spring-Boot 从web-Module构建一个 fat-JAR,从核心-Module构建一个简单的 JAR 。