2011-11-26 40 views
3

我不明白行家。最好使用螞蟻,但......我已經成功地創建罐子(帶有或不帶有依賴關係),我已經成功地複製蝙蝠亞軍腳本接近罐子,但現在我要創建這個jar和這隻蝙蝠拉鍊。所以我使用程序集插件並獲取BUUUM! CADAAAM!在我的配置中,它發生的情況是,它平行於罐子包裝執行。我寫的程序集文件:行家創建罐子拉鍊和一些文件

<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>jg</id> 
    <formats> 
     <format>jar</format> 
    </formats> 
    <includeBaseDirectory>false</includeBaseDirectory> 
    <fileSets> 
     <fileSet> 
      <directory>${project.build.directory}/classes</directory> 
      <outputDirectory>/123</outputDirectory> 
      <excludes> 
       <exclude>assembly/**</exclude> 
       <exclude>runners/**</exclude> 
      </excludes> 
     </fileSet> 
    </fileSets> 
    <dependencySets> 
     <dependencySet> 
      <outputDirectory>/</outputDirectory> 
      <useProjectArtifact>true</useProjectArtifact> 
      <unpack>true</unpack> 
      <scope>runtime</scope> 
     </dependencySet> 
    </dependencySets> 
</assembly> 

然後,我勢必Maven的組裝插件:

<plugin> 
     <artifactId>maven-assembly-plugin</artifactId> 
     <version>2.2.1</version> 
     <executions> 
      <execution> 
       <phase>package</phase> 
       <goals> 
        <goal>single</goal> 
       </goals> 
       <inherited>false</inherited> 
       <configuration> 
        <archive> 
         <manifest> 
          <mainClass>by.dev.madhead.lzwj.Main</mainClass> 
          <addClasspath>true</addClasspath> 
         </manifest> 
        </archive> 
        <descriptors> 
          <descriptor>src/main/resources/assembly/assembly.xml</descriptor> 
          <!-- <descriptorRef>jar-with-dependencies</descriptorRef> --> 
        </descriptors> 
       </configuration> 
      </execution> 
     </executions> 
    </plugin> 

現在我得到這個./target

  1. runner.bat
  2. jar_without_dependencies。罐子(它是Maven的JAR-插件吧?)
  3. jar_without_dependencies.j AR

而第三激怒了我。它包含:enter image description here 而123目錄包含:enter image description here

正如你看到的,我得到的罐子解壓的依賴,不含DIRS !!!!,並與DIR 123,這實際上是我想要的(呵呵組裝插件!做過某事!!!)。

我想與依賴類路徑的正確清單罐子。作爲一個選項,我想解壓依賴關係的jar(我知道關於彙編中的<unpack>false</unpack>,但無法讓它工作)。我想將/ 123更改爲/並且無需排除文件即可獲得NORMAL JAR!我想兩個獨立的任務,以構建jar和zip(它是在Maven的配置文件完成的?)如螞蟻,我就寫了這樣的事情:

<target name="jar_with_deps" depends-on="compile"> 
     <jar> 
      here i copy classes (excluding some dirs with runner script), and build manifest 
     </jar> 
     <copy> 
      copy bat file from src/main/resources/runner/runner.bat 
     </copy> 
    </target> 
    <target name="zip" depends-on="jar_with_deps"> 
     <zip> 
      Get jar from previous target, get runner.bat. Put them in zip 
     </zip> 
    </target> 

對不起,如果我太傳神,但我我對這種隱含的行爲真的很生氣。我真的堅持這一點。

+0

jar -with-dependencies不起作用 – madhead

+0

試圖刪除 jar。不起作用,因爲罐子是默認包裝... – madhead

+0

好吧,得到與罐子相關的工作。但仍然需要定製它。 – madhead

回答

1

你必須選擇,實現自己的目標:

  1. 選項:創建兩個組件的描述,一個罐子W/DEPS,一個用於拉鍊。 Zip採用新創建的jar。
  2. 選項:在項目中創建兩個模塊:第一個模塊shades所有DEPS到一個罐子(或與您的主罐子一起附上陰影罐子,保存另一個模塊),有第二個模塊依賴於它,並在罐子吸在你的組裝中。你做完了。

根據您的項目的大小和結構,我會採取安全的方式:選項2.這個保證有正確的構建和dep順序。選項1有點違反了maven的方式。

+0

當我製作兩個裝配描述符時,第二個(zip)找不到第一個輸出。我如何指定他們的訂單? – madhead

+0

訂單與pom中定義的訂單相同。附加第二個工件,並且該罐子也包含在內。請參閱['useProjectAttachments'](http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_dependencySet)屬性。 –

1

我做了在pom.xml中兩個配置文件:

<profiles> 
    <profile> 
     <id>jar-with-dependencies</id> 
     <activation> 
      <activeByDefault>false</activeByDefault> 
     </activation> 
     <build> 
      <plugins> 
       <plugin> 
        <artifactId>maven-assembly-plugin</artifactId> 
        <version>2.2.1</version> 
        <configuration> 
         <archive> 
          <manifest> 
           <mainClass>by.dev.madhead.lzwj.Main</mainClass> 
          </manifest> 
         </archive> 
         <descriptorRefs> 
          <descriptorRef>jar-with-dependencies</descriptorRef> 
         </descriptorRefs> 
        </configuration> 
        <executions> 
         <execution> 
          <phase>package</phase> 
          <goals> 
           <goal>single</goal> 
          </goals> 
         </execution> 
        </executions> 
       </plugin> 
      </plugins> 
     </build> 
    </profile> 
    <profile> 
     <id>distro</id> 
     <activation> 
      <activeByDefault>false</activeByDefault> 
     </activation> 
     <build> 
      <plugins> 
       <plugin> 
        <artifactId>maven-assembly-plugin</artifactId> 
        <version>2.2.1</version> 
        <configuration> 
         <descriptors> 
          <descriptor>src/main/assembly/distro.xml</descriptor> 
         </descriptors> 
        </configuration> 
        <executions> 
         <execution> 
          <phase>package</phase> 
          <goals> 
           <goal>single</goal> 
          </goals> 
         </execution> 
        </executions> 
       </plugin> 
      </plugins> 
     </build> 
    </profile> 
</profiles> 

現在我能夠創建簡單的JAR(mvn clean package),罐子依賴(mvn clean package -Pjar-with-dependencies)。我也可以致電mvn package -Pdistro創建zip。但是我需要手動調用Maven和-Pjar -with-dependencies。除此之外,一切都很好。

5

爲了以防萬一它幫助其他人,我發現這很容易做,至少對我的基本需求。我已經使用Maven的陰影插件來構建一個jar具有所有依賴性包括:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-shade-plugin</artifactId> 
    <version>2.4.1</version> 
    <configuration></configuration> 
    <executions> 
    <execution> 
     <phase>package</phase> 
     <goals> 
     <goal>shade</goal> 
     </goals> 
    </execution> 
    </executions> 
</plugin> 

所以,當我跑mvn package,它會產生目標/ MyApp的-version.jar,而我想要一個MyApp-version.zip包含MyApp-version.jar以及一些其他文件(一個README等)。所以,我添加Assembly插件:

<plugin> 
    <artifactId>maven-assembly-plugin</artifactId> 
    <version>2.5.5</version> 
    <configuration> 
    <appendAssemblyId>false</appendAssemblyId> 
    <descriptors> 
     <descriptor>assembly.xml</descriptor> 
    </descriptors> 
    </configuration> 
    <executions> 
    <execution> 
     <phase>package</phase> 
     <goals> 
     <goal>single</goal> 
     </goals> 
    </execution> 
    </executions> 
</plugin> 

上述塊是指assembly.xml,它配置the way the plugin works:(${app.version}是在pom.xml定義<properties>元件)

<?xml version="1.0" encoding="utf-8"?> 
<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>release</id> 
    <formats> 
     <format>zip</format> 
    </formats> 
    <fileSets> 
     <fileSet> 
      <directory>target</directory> 
      <includes> 
       <include>MyApp-${app.version}.jar</include> 
      </includes> 
      <outputDirectory>/</outputDirectory> 
     </fileSet> 
    </fileSets> 
    <files> 
     <file> 
      <source>CHANGES.md</source> 
      <fileMode>0644</fileMode> 
     </file> 
     <file> 
      <source>LICENSE</source> 
      <fileMode>0644</fileMode> 
     </file> 
     <file> 
      <source>README</source> 
      <fileMode>0644</fileMode> 
     </file> 
    </files> 
</assembly> 

就是這樣,現在mvn package同時生成jar和zip。