Maven Shade插件配置,用于创建仅包含与相同groupId下的子模块相关的超级JAR。

huangapple go评论120阅读模式
英文:

Maven Shade plugin configuration to create uber jar containing just the submodules based in the same groupId

问题

以下是翻译好的部分:

现有情况:

  • STS
  • Java
  • Maven

机器一

工作区-01

基于Maven的Java应用程序针对一个单一模块。它被安装(mvn install)在存储库中,master-project.jar文件在其他机器上被安装并用于其他项目,如下所示:

  1. <dependency>
  2. <groupId>com.manuel.jordan</groupId>
  3. <artifactId>master-project</artifactId>
  4. <version>0.0.1.SNAPSHOT</version>
  5. </dependency>

到目前为止,一切工作正常。

工作区-02

该应用程序发展壮大,在另一个工作区中,该应用程序已迁移到与多模块一起工作。

假设为简单起见,采用以下结构:

  1. master-project (<packaging>pom</packaging>)
  2. alpha (<packaging>jar</packaging>)
  3. beta (<packaging>jar</packaging>)
  4. numbers (<packaging>pom</packaging>)
  5. one (<packaging>jar</packaging>)
  6. two (<packaging>jar</packaging>)
  7. countries (<packaging>pom</packaging>)
  8. Europe (<packaging>pom</packaging>)
  9. France (<packaging>jar</packaging>)
  10. Italy (<packaging>jar</packaging>)
  11. AmericaLatina (<packaging>pom</packaging>)
  12. Peru (<packaging>jar</packaging>)
  13. Argentina (<packaging>jar</packaging>)

我能够编译所有这些模块。因此,构建成功。

目标:

现在的目标是生成相同的master-project.jar文件,其中包括内部模块作为内部jar。

经过研究,解决方案是:maven-shade-plugin

我有以下配置(部分配置),参考了以下链接和其他来源:

因此:

  1. <modelVersion>4.0.0</modelVersion>
  2. <packaging>pom</packaging> <!-- 这是主要或根pom.xml文件 -->
  3. <groupId>com.manuel.jordan</groupId>
  4. <artifactId>master-project</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <properties>
  7. <!-- ... -->
  8. </properties>
  9. <dependencies>
  10. <dependency>
  11. <groupId>com.hazelcast</groupId>
  12. <artifactId>hazelcast-all</artifactId>
  13. <version>${hazelcast.version}</version>
  14. <scope>provided</scope> <!-- 否则每个子模块中的uber jar都为11.6MB -->
  15. </dependency>
  16. </dependencies>
  17. <modules>
  18. <module>...</module>
  19. <!-- ... -->
  20. </modules>
  21. <build>
  22. <plugins>
  23. <!-- ... -->
  24. <plugin>
  25. <groupId>org.apache.maven.plugins</groupId>
  26. <artifactId>maven-shade-plugin</artifactId>
  27. <version>3.2.4</version>
  28. <executions>
  29. <execution>
  30. <id>create-fat-jar</id>
  31. <phase>package</phase>
  32. <goals>
  33. <goal>shade</goal>
  34. </goals>
  35. <configuration>
  36. <artifactSet>
  37. <includes>
  38. <include>com.manuel.jordan</include>
  39. </includes>
  40. </artifactSet>
  41. </configuration>
  42. </execution>
  43. </executions>
  44. </plugin>
  45. <!-- ... -->
  46. </plugins>
  47. </build>

使用当前的配置,发生以下情况:

对于target目录中的每个模块:

  • 对于<packaging>pom</packaging>,生成一个.pom文件
  • 对于<packaging>jar</packaging>,生成两个.jar文件,一个是子模块本身的jar,另一个是uber jar的jar(是的,对于每个模块都是如此)

目标:

希望做出以下调整或补充配置,以生成类似于这样的结果:

  1. master-project.jar 包含
  2. module1.jar
  3. module2.jar
  4. ...
  5. module3.jar

其中每个module#都有相同的<groupId>com.manuel.jordan</groupId>声明,这就是为什么我声明了:

  1. <configuration>
  2. <artifactSet>
  3. <includes>
  4. <include>com.manuel.jordan</include>
  5. </includes>
  6. </artifactSet>
  7. </configuration>

并且请注意,根/主pom.xml文件具有<packaging>pom</packaging>

目标:

因此,目标是让位于其他机器上的其他项目保持平稳:

  1. <dependency>
  2. <groupId>com.manuel.jordan</groupId>
  3. <artifactId>master-project</artifactId>
  4. <version>0.0.1.SNAPSHOT</version>
  5. </dependency>

因此,无论master-project项目是单一模块还是多模块,都不影响。

英文:

Having the following situation:

  • STS
  • Java
  • Maven

Machine One

workspace-01

the Java app with Maven is based for a single module.
It is installed (mvn install) in the repository and the master-project.jar file in other Machine is installed and used for other project how

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;com.manuel.jordan&lt;/groupId&gt;
  3. &lt;artifactId&gt;master-project&lt;/artifactId&gt;
  4. &lt;version&gt;0.0.1.SNAPSHOT&lt;/version&gt;
  5. &lt;/dependency&gt;

Until here all work fine

workspace-02

The app grew up and in other workspace the app was migrated to work with multi-modules

Lets assume the following structure for simplicity

  1. master-project (&lt;packaging&gt;pom&lt;/packaging&gt;)
  2. alpha (&lt;packaging&gt;jar&lt;/packaging&gt;)
  3. beta (&lt;packaging&gt;jar&lt;/packaging&gt;)
  4. numbers (&lt;packaging&gt;pom&lt;/packaging&gt;)
  5. one (&lt;packaging&gt;jar&lt;/packaging&gt;)
  6. two (&lt;packaging&gt;jar&lt;/packaging&gt;)
  7. countries (&lt;packaging&gt;pom&lt;/packaging&gt;)
  8. Europe (&lt;packaging&gt;pom&lt;/packaging&gt;)
  9. France (&lt;packaging&gt;jar&lt;/packaging&gt;)
  10. Italy (&lt;packaging&gt;jar&lt;/packaging&gt;)
  11. AmericaLatina (&lt;packaging&gt;pom&lt;/packaging&gt;)
  12. Peru (&lt;packaging&gt;jar&lt;/packaging&gt;)
  13. Argentina (&lt;packaging&gt;jar&lt;/packaging&gt;)

I am able to compile all these modules. Therefore build success

Goal

Now the goal is generate the same master-project.jar file including the sub modules how internal jars.

After to do a research the solution is: maven-shade-plugin

I have the following configuration (part of it) using how reference the following links and other sources:

Therefore:

  1. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  2. &lt;packaging&gt;pom&lt;/packaging&gt; &lt;--- it is the main or root pom.xml file
  3. &lt;groupId&gt;com.manuel.jordan&lt;/groupId&gt;
  4. &lt;artifactId&gt;master-project&lt;/artifactId&gt;
  5. &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  6. &lt;properties&gt;
  7. ....
  8. &lt;/properties&gt;
  9. &lt;dependencies&gt;
  10. &lt;dependency&gt;
  11. &lt;groupId&gt;com.hazelcast&lt;/groupId&gt;
  12. &lt;artifactId&gt;hazelcast-all&lt;/artifactId&gt;
  13. &lt;version&gt;${hazelcast.version}&lt;/version&gt;
  14. &lt;scope&gt;provided&lt;/scope&gt; &lt;!-- Otherwise each uber jar within each submodule is 11.6MB --&gt;
  15. &lt;/dependency&gt;
  16. &lt;/dependencies&gt;
  17. &lt;modules&gt;
  18. &lt;module&gt;...&lt;/module&gt;
  19. ...
  20. &lt;/modules&gt;
  21. &lt;build&gt;
  22. &lt;plugins&gt;
  23. ....
  24. &lt;plugin&gt;
  25. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  26. &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
  27. &lt;version&gt;3.2.4&lt;/version&gt;
  28. &lt;executions&gt;
  29. &lt;execution&gt;
  30. &lt;id&gt;create-fat-jar&lt;/id&gt;
  31. &lt;phase&gt;package&lt;/phase&gt;
  32. &lt;goals&gt;
  33. &lt;goal&gt;shade&lt;/goal&gt;
  34. &lt;/goals&gt;
  35. &lt;configuration&gt;
  36. &lt;artifactSet&gt;
  37. &lt;includes&gt;
  38. &lt;include&gt;com.manuel.jordan&lt;/include&gt;
  39. &lt;/includes&gt;
  40. &lt;/artifactSet&gt;
  41. &lt;/configuration&gt;
  42. &lt;/execution&gt;
  43. &lt;/executions&gt;
  44. &lt;/plugin&gt;
  45. &lt;/plugins&gt;
  46. &lt;/build&gt;

With the current configuration happens the following:

For each module within the target directory:

  • With &lt;packaging&gt;pom&lt;/packaging&gt; generates a .pom file
  • With &lt;packaging&gt;jar&lt;/packaging&gt; generates two .jar files, one of the sub module itself and other of the uber jar (yes for each module)

Goal: what is the extra or missing configuration to generate something like this:

  1. master-project.jar containing
  2. module1.jar
  3. module2.jar
  4. ...
  5. module3.jar

Where each module# have the same &lt;groupId&gt;com.manuel.jordan&lt;/groupId&gt; declaration, that's why I declared

  1. &lt;configuration&gt;
  2. &lt;artifactSet&gt;
  3. &lt;includes&gt;
  4. &lt;include&gt;com.manuel.jordan&lt;/include&gt;
  5. &lt;/includes&gt;
  6. &lt;/artifactSet&gt;
  7. &lt;/configuration&gt;

And observe that the root/main pom.xml file has &lt;packaging&gt;pom&lt;/packaging&gt;

Goal

Therefore the goal is that the other project located in the other machine keeps in peace

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;com.manuel.jordan&lt;/groupId&gt;
  3. &lt;artifactId&gt;master-project&lt;/artifactId&gt;
  4. &lt;version&gt;0.0.1.SNAPSHOT&lt;/version&gt;
  5. &lt;/dependency&gt;

Thus it does not matter if the master-project project is either single or multi-module

答案1

得分: 1

你可以在主项目下创建一个打包的JAR模块,该模块依赖于其他模块,并且只在这里包含阴影插件的配置。

英文:

You could make a packaging jar module below your master-project with a dependency to the other modules, and include only there the shade plugin configuration

huangapple
  • 本文由 发表于 2020年8月19日 01:57:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63474169.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定