定制的Maven组装 [英] Custom maven assembly
问题描述
我开始与Maven合作,但尚未以Maven的角度成功思考.我有一个特定的要求,而文档却没有给我足够的提示,因此我可以使用一些帮助:
I'm starting to work with Maven but am not yet successfully thinking in Maven's terms. I have a specific requirement and the docs aren't giving me enough clues, so I could use a bit of help:
我想创建一个程序集
-
构建类似于该名称的标准"目标的
jar-with-dependencies
,但不包括几个资源.我希望log4j.properties
和其他几个配置文件不在容器中.
builds a
jar-with-dependencies
like the "standard" target of this name, but excluding a couple of the resources. I wantlog4j.properties
and a couple of other configuration files to not be in the jar.
构建一个.ZIP
文件,该文件在其根目录中包含第1步中的.jar
以及上述配置文件.
builds a .ZIP
file containing in its root directory the .jar
from step 1 as well as the above mentioned config files.
我想从命令行(仅)启动此程序集,因此无需绑定到某个阶段(或目标?mojo?).最好使用assembly:assembly
或assembly:single
.
I want to fire this assembly up from the command line (only), hence no need to tie to a phase (or goal? mojo?). Preferrably using either assembly:assembly
or assembly:single
.
- 是否需要为此自定义程序集描述符?
- 是真的我不能将其嵌套在
pom.xml
中吗?所以它进入src/assembly/something.xml
并被descriptorRef
引用? - 我可以将其编码为两个相对简单的程序集,其中一个基于另一个程序集(即.Zip程序集使用.Jar程序集)还是必须在一个程序集中进行所有操作?
- Do I need a custom assembly descriptor for this?
- And is it true I can't nest it in the
pom.xml
? So it goes insrc/assembly/something.xml
and gets referenced with adescriptorRef
? - Can I code this as two relatively simple assemblies, of which one builds on the other (i.e. the .Zip assembly uses the .Jar assembly) or do I have to do everything in one assembly?
推荐答案
我开始与Maven合作,但仍未成功按照Maven的观点进行思考.
I'm starting to work with Maven but am not yet successfully thinking in Maven's terms.
欢迎光临,卡尔! :D
Welcome on board, Carl! :D
我想从命令行(仅)启动此程序集,因此无需绑定到某个阶段(或目标?mojo?).最好同时使用Assembly:assembly或Assembly:single.
I want to fire this assembly up from the command line (only), hence no need to tie to a phase (or goal? mojo?). Preferrably using either assembly:assembly or assembly:single.
只需澄清一下:构建生命周期本身由 phases
组成(测试,包装等)和插件目标(从技术上讲是Mojos)在阶段上绑定.然后,您可以调用一个阶段...或只是一个特定的插件目标.
Just to clarify: the build lifecycle itself is made of phases
(compile, test, package, etc) and plugin goals (technically Mojos) are bound on phases. You then either invoke a phase... or just a specific plugin goal.
我是否需要一个自定义程序集描述符?
Do I need a custom assembly descriptor for this?
好吧,因为您想要的行为是预定义描述符不,是的.您甚至需要其中两个(对于uberjar,其中一个,对于zip).
Well, since you want behavior that the pre-defined descriptors don't cover, yes. You'll even need two of them (of for the uberjar, one for the zip).
是真的我不能将其嵌套在pom.xml中吗?因此它进入src/assembly/something.xml并被描述符引用引用?
And is it true I can't nest it in the pom.xml? So it goes in src/assembly/something.xml and gets referenced with a descriptorRef?
是的,这是正确的(描述符使用自定义格式),并且通常将它们放入src/main/assembly
中.不,descriptorRef
用于内置描述符,在这里您必须使用descriptor
.
Yes, that's true (descriptors use a custom format) and they usually go into src/main/assembly
. And no, descriptorRef
is for the built-in descriptors, you'll have to use descriptor
here.
我可以将其编码为两个相对简单的程序集,其中一个基于另一个程序集(即.Zip程序集使用.Jar程序集)还是必须在一个程序集中进行所有操作?
Can I code this as two relatively simple assemblies, of which one builds on the other (i.e. the .Zip assembly uses the .Jar assembly) or do I have to do everything in one assembly?
如提示所示,您将需要两个程序集描述符.让我帮忙...
As hinted, you'll need two assembly descriptors. Let me help a bit...
假设您具有以下项目结构:
Let's assume you have the following project structure:
$ tree .
.
├── pom.xml
└── src
├── main
│ ├── assembly
│ │ ├── jar.xml
│ │ └── zip.xml
│ ├── java
│ │ └── com
│ │ └── stackoverflow
│ │ └── App.java
│ └── resources
│ └── log4j.properties
└── test
└── java
└── com
└── stackoverflow
└── AppTest.java
pom.xml
包含以下用于程序集插件的配置:
Where the pom.xml
contains the following configuration for the assembly plugin:
<project>
...
<dependencies>
...
</dependencies>
...
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/jar.xml</descriptor>
<descriptor>src/main/assembly/zip.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
</project>
过滤的" uberjar(jar.xml)的描述符如下:
The descriptor for the "filtered" uberjar (jar.xml) looks like this:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>uberjar</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<unpack>true</unpack>
<scope>runtime</scope>
<useProjectArtifact>false</useProjectArtifact>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<outputDirectory>/</outputDirectory>
<excludes>
<exclude>log4j.properties</exclude>
</excludes>
</fileSet>
</fileSets>
</assembly>
此描述符的作用是(简而言之):
What this descriptor does is (in short):
- 包括依赖项,解压缩它们,但排除项目本身(是的,这是很直观的,但是为了保持向后兼容性,保留了这种奇怪的默认行为)
- 包括项目文件,但排除其中一些.
- include the dependencies, unpack them, but exclude the project itself (yes, this is counter intuitive but this weird default behavior has been kept for backward compatibility)
- include the project files but exclude some of them.
zip的描述符(zip.xml)如下所示:
And the descriptor for the zip (zip.xml) looks like this:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>bin</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.basedir}/src/main/resources</directory>
<outputDirectory/>
<includes>
<include>log4j.properties</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory/>
<includes>
<include>*-uberjar.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
(在某种程度上)自我解释:)
Which is (somehow) self explaining :)
- 它在程序集的根目录中包含配置文件(相对于
<directory>
) - 它在程序集的根目录中包含uberjar(相对于
<directory>
)
- it includes the configuration files (relatively to
<directory>
) at the root of the assembly - it includes the uberjar (relatively to
<directory>
) at the root of the assembly
最后,只需运行mvn assembly:assembly
(这是CLI上要使用的目标).
Finally, just run mvn assembly:assembly
(that's the goal intended to be used on the CLI).
我(不知道)没有在uberjar的程序集中包含META-INF/maven/**.有没有一种简单的方法可以防止将它们包括在内?
I didn't (knowingly) include META-INF/maven/** in the assembly for the uberjar. Is there a simple way to prevent inclusion of these?
这些来自解压缩的库.您可以使用 unpackOptions
排除它们.这是jar.xml
的修改版本:
These are coming from the libraries that are unpacked. You can exclude them using unpackOptions
. Here is a modified version of the jar.xml
:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>uberjar</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<unpack>true</unpack>
<scope>runtime</scope>
<unpackOptions>
<excludes>
<exclude>META-INF/maven/**</exclude>
</excludes>
</unpackOptions>
<useProjectArtifact>false</useProjectArtifact>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<outputDirectory>/</outputDirectory>
<excludes>
<exclude>log4j.properties</exclude>
</excludes>
</fileSet>
</fileSets>
</assembly>
这篇关于定制的Maven组装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!