英文:
Build fat jar in maven with dependencies only
问题
我在我的Java项目中使用了maven-shade-plugin
。
我有一个名为core
的模块,我想将它的所有依赖打包到一个单独的JAR文件中。我不想每次都创建一个臃肿的大JAR文件,并将其与我的应用代码一起部署。我的应用程序JAR文件大小小于1MB,但依赖项大于20MB。
在服务器上,我会这样运行我的应用程序:
java -cp core.lib.jar:core.jar myApp
当我更改应用程序代码并且需要上传core.lib.jar
时,我只需要将core.jar
部署到我的服务器,而当我更改依赖项时(这是非常罕见的情况)只需要上传core.lib.jar
。
有很多关于如何使用Java源代码构建fat JAR的文档,但我想将它们排除在外,独立地交付给服务器。
有两个用于构建fat JAR的Maven插件:assembly和shade。我想使用shade插件。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.3</version>
<executions>
<execution>
<phase>none</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>core</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
当我运行mvn -pl core shade:shade
时,我会得到错误:core:无法创建阴影化的构件,项目的主构件不存在
。
我认为这个选项:
<excludes>
<exclude>core</exclude>
</excludes>
会从构建的构件中排除我的源代码。
我将phase
设置为none
,因为我不想在运行package
时创建fat JAR。我希望运行mvn shade:shade
并获得更新后的fat JAR。
我不是Maven或其插件的专家,所以非常感谢任何帮助
英文:
I use maven-shade-plugin
in my java project.
I have a module called core
and i would like to pack all its dependencies into one single jar. I don't wan't to create fat big jar every time and deploy it along with my application code. My application jar has size < 1 MB but dependencies > 20 MB
On my server i would run my application like:
java -cp core.lib.jar:core.jar myApp
I need to deploy only core.jar
to my server when i change application code and need to upload core.lib.jar
only when i change dependencies (this is very rare).
There's a lot of docs how to build fat jar with java sources but i want to exclude them and deliver it to the server independently.
There is two maven plugins for building fat jar: assembly and shade. I want to stick with shade.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.3</version>
<executions>
<execution>
<phase>none</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>core</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
When i run mvn -pl core shade:shade
i get error: core: Failed to create shaded artifact, project main artifact does not exist
I thought that that option:
<excludes>
<exclude>core</exclude>
</excludes>
would exclude my sources from the artifact.
I set phase
to none
because i don't want to create fat jar when i want to run package
.
I would like to run mvn shade:shade
and have updated fat jar.
I'm not an expert in maven or its plugins, so any help is very appreciated
答案1
得分: 1
我自己没有使用过 shade 插件,所以无法提供实际指导,不过看起来你需要一个多模块的 Maven 项目:
- 重新组织你的项目(移动文件夹)使其结构如下:
my-project
|__app
| |__src
| | |__main(以及所有生产源代码/资源)
| | |__test(所有测试/测试资源)
| |__pom.xml
|__core-lib
| |__pom.xml
|__pom.xml
my-project/pom.xml
可以是一个“多模块”聚合器:它应该有 <packaging>pom</packaging>
,并包含以下内容:
<modules>
<module>app</module>
<module>core-lib</module>
</modules>
现在 app
模块可以是你编写应用程序代码的地方。它会经常变化,但不会生成任何大型的 JAR 文件 - 只会生成一个普通的 JAR,其中只包含来自此模块的已编译代码(在你的示例中,这将是一个 1MB 的构件)。
core-lib
模块将在其 pom.xml
中包含 shade/assembly 插件的定义,并且该模块将负责创建“大型 JAR”。在其 dependences
部分中,它将列出所有你需要很少更改的第三方依赖/代码(在你的情况下,那个大小为 20MB 的构件)。
如果需要的话,“app” 模块可以依赖于“core-lib” 模块,以便应用程序可以访问来自该库的类。
因此,你通常的工作流程将是:
cd my-project/app
mvn package
如果你想同时构建应用程序和核心库,则执行:
cd my-project
mvn package
英文:
I haven't used shade plugin by myself so I can't provide any practical guidance, however it seems like you need a multi-module maven project:
- Refactor your project (move the folders) to look like this:
my-project
|__app
| |__src
| | |__main( (and all the production sources/resources inside)
| | |__test (all the tests/test resources inside)
| |__pom.xml
|__core-lib
| |__pom.xml
|__ pom.xml
my-project/pom.xml
could be a "multi-module" aggregator: it should have <packaging>pom</packaging>
and have the following:
<modules>
<module>app</module>
<module>core-lib</module>
</modules>
Now app
module could be a place where you write the code of your application. It will change often, but it won't produce any fat jar - only a regular jar with a compiled code from this module only (in your example, it will be 1mb artifact).
The core-lib
module will be a module that in it's pom.xml
will contain the definitions of the shade / assembly plugin and this module will be responsible for the creation of the "fat jar". In its dependences
section it will list all the 3rd party dependencies/code that you'll have to change 'rarely' (in your case, the one with 20 mb artifact).
If you need that, the "app" module could depend on "core-lib" module so that the application will have an access to the classes from the library.
So your usual workflow will be:
cd my-project/app
mvn package
If you want to build both application and core-libraries, then:
cd my-project
mvn package
答案2
得分: 0
我需要将这个块移动到插件/配置下面
以前在executions
块中。
现在我可以运行mvn -pl core shade:shade
并且只有依赖项的厚Jar文件
英文:
I needed to move this block under plugin / configuration
<artifactSet>
<excludes>
<exclude>core</exclude>
</excludes>
</artifactSet>
Used to be in executions
block.
Now i can run mvn -pl core shade:shade
and have fat jar with dependencies only
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论