在Maven中捆绑整个项目的最佳方法是什么? [英] Whats the best way to bundle the whole project in Maven?
问题描述
我想创建一个可以自动执行的胖子(Shade插件),但是还可以通过Eclipse导入的方式来包括源代码,资源,测试和所有内容.
I'd like to create a fat-jar which is autoexecutable (Shade Plugin), but also includes sources, resources, tests, and everything in a Eclipse-importable way.
罐子将是单文件可执行应用程序,它也可以以某种可导入的方式包含整个项目,作为maven项目(我认为将罐子解压缩后).
The jar would be single-file executable app, which also contains the whole project in an importable fashion as a maven project someway (after unzipping the jar, I assume).
另一个选项是生成的项目zip,其中包括基本级别的二进制分发.
Another option would be a resulting project zip that includes the binary distribution at base level.
有这样的东西吗?
推荐答案
可能的解决方案需要3个步骤:
A possible solution requires 3 steps:
- 使用
maven-shade-插件
.该JAR将包含所有依赖项,并且将是可执行的. - 使用
maven创建由项目源组成的侧面工件-assembly-plugin
.此ZIP将包含pom.xml
和src
下的所有源. - 创建另一个人造工件,该工件将有效地成为我们最终的工件,其中将包含解压缩的阴影JAR(以使其有效执行)和源ZIP.
这可能是配置:
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>make-uberjar</id>
<goals>
<goal>shade</goal>
</goals>
<phase>package</phase>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>fully.classified.name.to.main.Class</mainClass>
</transformer>
</transformers>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>sources</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly-sources.xml</descriptor>
</descriptors>
</configuration>
</execution>
<execution>
<id>final</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
这将配置 maven-shade-plugin
的执行和2执行 maven-assembly-plugin
的执行.第一次执行将创建源ZIP.这将是 assembly-sources.xml
程序集描述符文件:
This configures the execution of the maven-shade-plugin
and 2 execution of the maven-assembly-plugin
. The first execution will creates the sources ZIP. This would be the assembly-sources.xml
assembly descriptor file:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>sources</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.basedir}</directory>
<includes>
<include>pom.xml</include>
<include>src/**</include>
</includes>
</fileSet>
</fileSets>
</assembly>
它将 pom.xml
和 src
下的所有内容都包含到具有 sources
分类符的ZIP中.
It includes pom.xml
and everything under src
into a ZIP having the sources
classifier.
第二个 assembly.xml
程序集描述符文件为:
The second assembly.xml
assembly descriptor file would be:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>jar-with-sources</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<includes>
<include>${project.groupId}:${project.artifactId}:jar:${project.version}</include>
</includes>
<unpack>true</unpack>
</dependencySet>
<dependencySet>
<useProjectAttachments>true</useProjectAttachments>
<includes>
<include>${project.groupId}:${project.artifactId}:zip:sources:${project.version}</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
这将创建2个< dependencySet>
.第一个将带阴影的JAR解压缩(在构建点此时替换了主要工件).第二个只是包含 sources
ZIP附加工件.请注意,我们需要使用 < useProjectAttachments> 代码>
,以包含随附的源ZIP.
This creates 2 <dependencySet>
. The first one unpacks the shaded JAR (which replaced the main artifact at that point in the build). The second one simply includes the sources
ZIP attached artifact; note that we need to use <useProjectAttachments>
to include the attached sources ZIP.
当您运行 mvn clean install
时,将得到一个文件 {finalName} -jar-with-sources.jar
,它将成为您想要的可执行JAR.它还将在根文件夹中包含所有来源的ZIP文件.
When you run mvn clean install
, you will have as result a file {finalName}-jar-with-sources.jar
that will be your wanted executable JAR. It will also contain a ZIP file of all the sources in the root folder.
这篇关于在Maven中捆绑整个项目的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!