2010-08-16 60 views
8

我开始和Maven一起工作,但还没有成功地用Maven的话来说。我有一个具体的要求和医生现在都没有给我足够的线索,所以我可以使用一些帮助:自定义的maven程序集

我想创建一个组件

  1. 建立一个jar-with-dependencies像这个名字的“标准”目标,但不包括一些资源。我想log4j.properties和一些其他配置文件而不是在罐子里。

  2. 构建一个.ZIP文件,该文件在其根目录中包含步骤1中的.jar以及上述配置文件。

我想从命令行(仅)启动该程序集,因此不需要绑定到阶段(或目标?mojo?)。优选使用assembly:assemblyassembly:single

  • 我需要一个自定义的装配描述符吗?
  • 这是真的我不能窝在pom.xml?所以它进入src/assembly/something.xml,并得到descriptorRef引用?
  • 我可以将它编码为两个相对简单的程序集,其中一个构建在另一个上(即.Zip程序集使用.Jar程序集),或者我必须在一个程序集中完成所有任务吗?

回答

27

我开始使用Maven工作,但不是在Maven的条款尚未成功地思考。

欢迎登机,卡尔! :D

我想从命令行(仅)启动该程序集,因此不需要绑定到阶段(或目标?mojo?)。最好使用装配:装配或装配:单一。

只是为了澄清:该build lifecycle本身是由phases(编译,测试,包装等)和插件的目标(技术上Mojos)绑定的阶段。然后,您可以调用阶段...或者只是一个特定的插件目标。

我需要一个自定义的装配描述符吗?

嗯,既然你想要的行为pre-defined descriptors不包括,是的。你甚至会需要其中两个(用于uberjar,一个用于zip)。

这是真的我不能将它嵌套在pom.xml中吗?所以它进入src/assembly/something.xml并用descriptorRef引用?

是的,这是真的(描述符使用自定义格式),他们通常会进入src/main/assembly。不,descriptorRef是内置的描述符,你必须在这里使用descriptor

我可以编码此为两个相对简单的组件,其中一个建立在其它(即该.zip组件使用该.jar组件)或我必须在一个组件尽一切?

正如暗示,你需要两个装配描述符。让我来帮有点...

让我们假设你有以下项目结构:

 
$ 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包含Assembly插件配置如下:

<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)看起来像这样:

<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> 

什么这个描述符的作用是(简称):

  • 包括依赖性,解包,排除项目本身(是的,这是违反直觉的,但这种怪异的默认行为已保持向后兼容)
  • 包括项目文件但排除其中的一些。

而对于ZIP(zip.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>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> 

这是(在某种程度上)自我解释:)

  • 它包括配置文件(相对到<directory>)位于组件的根部
  • 它包括位于组件根部的uberjar(相对于<directory>

最后,只需运行mvn assembly:assembly(这是要在CLI中使用的目标)。


我没有(有意地)包括META-INF /行家在组装为uberjar/**。有没有简单的方法来防止包含这些?

这些来自解压缩的库。您可以使用unpackOptions排除它们。这里是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> 
+0

萨吕帕斯卡,谢谢*非常*多的这种巨大有用的答案!我希望你能回应,我甚至考虑过直接与你联系。 “机上”可能有点夸张:自从我开始尝试在Maven中运行它以来,各种各样的灾难看起来都是随机的,一直困扰着我的构建。我仍然觉得Maven只有一个母亲可以爱的脸。但是在相信你的判断的时候,我会继续坚持下去,并希望在我的耐心给出之前在学习曲线上达到一个平台;) – 2010-08-16 17:52:30

+1

@Carl我想知道是否有人逼你使用Maven,也许把你的家人抱在人质中:)更严重的是,恭喜你的主动权,如果我可以让你的经历好一点,我会很乐意提供帮助。 – 2010-08-16 18:28:17

+0

“人质”猜测大致正确 - 这是一份工作计划。像魅力一样工作,顺便说一句。再次感谢。有没有一种方法可以影响'.zip'上的'-bin'后缀? – 2010-08-17 11:19:33