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

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

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

&lt;dependency&gt;
  &lt;groupId&gt;com.manuel.jordan&lt;/groupId&gt;
  &lt;artifactId&gt;master-project&lt;/artifactId&gt;
  &lt;version&gt;0.0.1.SNAPSHOT&lt;/version&gt;
&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

master-project (&lt;packaging&gt;pom&lt;/packaging&gt;)
alpha (&lt;packaging&gt;jar&lt;/packaging&gt;)
beta (&lt;packaging&gt;jar&lt;/packaging&gt;)
numbers (&lt;packaging&gt;pom&lt;/packaging&gt;)
   one (&lt;packaging&gt;jar&lt;/packaging&gt;)
   two (&lt;packaging&gt;jar&lt;/packaging&gt;)
countries (&lt;packaging&gt;pom&lt;/packaging&gt;)
   Europe (&lt;packaging&gt;pom&lt;/packaging&gt;)
      France (&lt;packaging&gt;jar&lt;/packaging&gt;)
      Italy (&lt;packaging&gt;jar&lt;/packaging&gt;)
   AmericaLatina (&lt;packaging&gt;pom&lt;/packaging&gt;)
      Peru (&lt;packaging&gt;jar&lt;/packaging&gt;)
      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:

  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;packaging&gt;pom&lt;/packaging&gt;  &lt;--- it is the main or root pom.xml file

  &lt;groupId&gt;com.manuel.jordan&lt;/groupId&gt;
  &lt;artifactId&gt;master-project&lt;/artifactId&gt;
  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;

  &lt;properties&gt;
    ....
  &lt;/properties&gt;

  &lt;dependencies&gt;
  	&lt;dependency&gt;
  		&lt;groupId&gt;com.hazelcast&lt;/groupId&gt;
  		&lt;artifactId&gt;hazelcast-all&lt;/artifactId&gt;
  		&lt;version&gt;${hazelcast.version}&lt;/version&gt;
  		&lt;scope&gt;provided&lt;/scope&gt; &lt;!-- Otherwise each uber jar within each submodule is 11.6MB --&gt;
  	&lt;/dependency&gt;
  &lt;/dependencies&gt;

  &lt;modules&gt;
  	&lt;module&gt;...&lt;/module&gt;
    ...
  &lt;/modules&gt;

  &lt;build&gt;
  	&lt;plugins&gt;
      ....
  	  &lt;plugin&gt;
			&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
			&lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
			&lt;version&gt;3.2.4&lt;/version&gt;
			&lt;executions&gt;
				&lt;execution&gt;
					&lt;id&gt;create-fat-jar&lt;/id&gt;
					&lt;phase&gt;package&lt;/phase&gt;
					&lt;goals&gt;
						&lt;goal&gt;shade&lt;/goal&gt;
					&lt;/goals&gt;
					&lt;configuration&gt;
						&lt;artifactSet&gt;
							&lt;includes&gt;
								&lt;include&gt;com.manuel.jordan&lt;/include&gt;
							&lt;/includes&gt;
						&lt;/artifactSet&gt;
					&lt;/configuration&gt;
				&lt;/execution&gt;
			&lt;/executions&gt;
		&lt;/plugin&gt;
  	&lt;/plugins&gt;
  &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:

master-project.jar containing
 module1.jar
 module2.jar
 ...
 module3.jar

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

&lt;configuration&gt;
   &lt;artifactSet&gt;
      &lt;includes&gt;
         &lt;include&gt;com.manuel.jordan&lt;/include&gt;
      &lt;/includes&gt;
   &lt;/artifactSet&gt;
&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

&lt;dependency&gt;
  &lt;groupId&gt;com.manuel.jordan&lt;/groupId&gt;
  &lt;artifactId&gt;master-project&lt;/artifactId&gt;
  &lt;version&gt;0.0.1.SNAPSHOT&lt;/version&gt;
&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:

确定