英文:
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
文件在其他机器上被安装并用于其他项目,如下所示:
<dependency>
<groupId>com.manuel.jordan</groupId>
<artifactId>master-project</artifactId>
<version>0.0.1.SNAPSHOT</version>
</dependency>
到目前为止,一切工作正常。
工作区-02
该应用程序发展壮大,在另一个工作区中,该应用程序已迁移到与多模块一起工作。
假设为简单起见,采用以下结构:
master-project (<packaging>pom</packaging>)
alpha (<packaging>jar</packaging>)
beta (<packaging>jar</packaging>)
numbers (<packaging>pom</packaging>)
one (<packaging>jar</packaging>)
two (<packaging>jar</packaging>)
countries (<packaging>pom</packaging>)
Europe (<packaging>pom</packaging>)
France (<packaging>jar</packaging>)
Italy (<packaging>jar</packaging>)
AmericaLatina (<packaging>pom</packaging>)
Peru (<packaging>jar</packaging>)
Argentina (<packaging>jar</packaging>)
我能够编译所有这些模块。因此,构建成功。
目标:
现在的目标是生成相同的master-project.jar
文件,其中包括内部模块作为内部jar。
经过研究,解决方案是:maven-shade-plugin
。
我有以下配置(部分配置),参考了以下链接和其他来源:
因此:
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging> <!-- 这是主要或根pom.xml文件 -->
<groupId>com.manuel.jordan</groupId>
<artifactId>master-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<!-- ... -->
</properties>
<dependencies>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-all</artifactId>
<version>${hazelcast.version}</version>
<scope>provided</scope> <!-- 否则每个子模块中的uber jar都为11.6MB -->
</dependency>
</dependencies>
<modules>
<module>...</module>
<!-- ... -->
</modules>
<build>
<plugins>
<!-- ... -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<id>create-fat-jar</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>com.manuel.jordan</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
<!-- ... -->
</plugins>
</build>
使用当前的配置,发生以下情况:
对于target
目录中的每个模块:
- 对于
<packaging>pom</packaging>
,生成一个.pom
文件 - 对于
<packaging>jar</packaging>
,生成两个.jar
文件,一个是子模块本身的jar,另一个是uber jar的jar(是的,对于每个模块都是如此)
目标:
希望做出以下调整或补充配置,以生成类似于这样的结果:
master-project.jar 包含
module1.jar
module2.jar
...
module3.jar
其中每个module#
都有相同的<groupId>com.manuel.jordan</groupId>
声明,这就是为什么我声明了:
<configuration>
<artifactSet>
<includes>
<include>com.manuel.jordan</include>
</includes>
</artifactSet>
</configuration>
并且请注意,根/主pom.xml
文件具有<packaging>pom</packaging>
。
目标:
因此,目标是让位于其他机器上的其他项目保持平稳:
<dependency>
<groupId>com.manuel.jordan</groupId>
<artifactId>master-project</artifactId>
<version>0.0.1.SNAPSHOT</version>
</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
<dependency>
<groupId>com.manuel.jordan</groupId>
<artifactId>master-project</artifactId>
<version>0.0.1.SNAPSHOT</version>
</dependency>
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
master-project (<packaging>pom</packaging>)
alpha (<packaging>jar</packaging>)
beta (<packaging>jar</packaging>)
numbers (<packaging>pom</packaging>)
one (<packaging>jar</packaging>)
two (<packaging>jar</packaging>)
countries (<packaging>pom</packaging>)
Europe (<packaging>pom</packaging>)
France (<packaging>jar</packaging>)
Italy (<packaging>jar</packaging>)
AmericaLatina (<packaging>pom</packaging>)
Peru (<packaging>jar</packaging>)
Argentina (<packaging>jar</packaging>)
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:
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging> <--- it is the main or root pom.xml file
<groupId>com.manuel.jordan</groupId>
<artifactId>master-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
....
</properties>
<dependencies>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-all</artifactId>
<version>${hazelcast.version}</version>
<scope>provided</scope> <!-- Otherwise each uber jar within each submodule is 11.6MB -->
</dependency>
</dependencies>
<modules>
<module>...</module>
...
</modules>
<build>
<plugins>
....
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<id>create-fat-jar</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>com.manuel.jordan</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
With the current configuration happens the following:
For each module within the target
directory:
- With
<packaging>pom</packaging>
generates a.pom
file - With
<packaging>jar</packaging>
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:
master-project.jar containing
module1.jar
module2.jar
...
module3.jar
Where each module#
have the same <groupId>com.manuel.jordan</groupId>
declaration, that's why I declared
<configuration>
<artifactSet>
<includes>
<include>com.manuel.jordan</include>
</includes>
</artifactSet>
</configuration>
And observe that the root/main pom.xml
file has <packaging>pom</packaging>
Goal
Therefore the goal is that the other project located in the other machine keeps in peace
<dependency>
<groupId>com.manuel.jordan</groupId>
<artifactId>master-project</artifactId>
<version>0.0.1.SNAPSHOT</version>
</dependency>
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论