2010-08-16 64 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