简体   繁体   English

仅使用依赖项在 maven 中构建 fat jar

[英]Build fat jar in maven with dependencies only

I use maven-shade-plugin in my java project.我在我的 java 项目中use maven-shade-plugin

I have a module called core and i would like to pack all its dependencies into one single jar.我有一个名为core的模块,我想将其所有依赖项打包到一个 jar 中。 I don't wan't to create fat big jar every time and deploy it along with my application code.我不想每次都创建一个巨大的 jar 并将其与我的应用程序代码一起部署。 My application jar has size < 1 MB but dependencies > 20 MB我的应用程序 jar 的大小 < 1 MB 但依赖项 > 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).当我更改应用程序代码时,我只需要将core.jar部署到我的服务器,并且仅在我更改依赖core.lib.jar时才需要上传core.lib.jar (这种情况很少见)。

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.有很多文档介绍了如何使用 Java 源构建胖 jar,但我想排除它们并将其独立传送到服务器。

There is two maven plugins for building fat jar: assembly and shade.有两个用于构建 fat jar 的 maven 插件:assembly 和 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当我运行mvn -pl core shade:shade出现错误: core: Failed to create shaded artifact, project main artifact does not exist

I thought that that option: core would exclude my sources from the artifact.我认为那个选项:core 会从工件中排除我的来源。

I set phase to none because i don't want to create fat jar when i want to run package .我将phase设置为none因为我不想在运行package时创建 fat jar 。 I would like to run mvn shade:shade and have updated fat jar.我想运行mvn shade:shade并更新了 fat jar。

I'm not an expert in maven or its plugins, so any help is very appreciated :)我不是 Maven 或其插件方面的专家,因此非常感谢任何帮助:)

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:我自己没有使用过 shade 插件,所以我不能提供任何实用的指导,但是看起来你需要一个多模块的 maven 项目:

  1. 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: my-project/pom.xml可以是一个“多模块”聚合器:它应该有<packaging>pom</packaging>并具有以下内容:

<modules>
  <module>app</module>
  <module>core-lib</module>
</modules>

Now app module could be a place where you write the code of your application.现在app模块可以是您编写应用程序代码的地方。 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).它会经常改变,但它不会产生任何胖 jar——只有一个普通的 jar,只有来自这个模块的编译代码(在你的例子中,它将是 1mb 工件)。

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". core-lib模块将是一个模块,在它的pom.xml中将包含 shade / assembly 插件的定义,并且该模块将负责创建“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).在其dependences部分,它将列出您必须“很少”更改的所有第 3 方依赖项/代码(在您的情况下,是具有 20 mb 工件的那个)。

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.如果需要,“app”模块可以依赖“core-lib”模块,以便应用程序可以访问库中的类。

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

You can read about multi modules here or here您可以在此处此处阅读有关多模块的信息

I needed to move this block under plugin / configuration我需要在插件/配置下移动这个块

                <artifactSet>
                    <excludes>
                        <exclude>core</exclude>
                    </excludes>
                </artifactSet>

Used to be in executions block.曾经在executions块中。

Now i can run mvn -pl core shade:shade and have fat jar with dependencies only :)现在我可以运行mvn -pl core shade:shade并且只有具有依赖项的 fat jar :)

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

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