2011-02-16 88 views
22

在我的pom.xml中,我使用maven-assembly-plugin在運行「mvn clean install」時創建了一個可執行的jar-with-dependencies。 現在它首先創建不可執行的「name-version.jar」,然後創建「name-version-jar-with-dependencies.jar」。只在Maven中創建可執行jar與依賴關係

我可以以某種方式配置pom.xml,以便它不會創建不可執行的JAR文件嗎?

目前我使用<appendAssemblyId>假</appendAssemblyId >所以它只是覆蓋的第一個文件...

而且我得到幾個「......已經加入,退出」的消息。我能以某種方式阻止他們嗎?

這是Maven的組裝插件定義在我pom.xml

<plugin> 
    <artifactId>maven-assembly-plugin</artifactId> 
    <version>2.2-beta-5</version> 
    <configuration> 
     <appendAssemblyId>false</appendAssemblyId> 
     <archive> 
      <manifest> 
       <mainClass>my.main.class</mainClass> 
      </manifest> 
     </archive> 
     <descriptorRefs> 
      <descriptorRef>jar-with-dependencies</descriptorRef> 
     </descriptorRefs> 
    </configuration> 
    <executions> 
     <execution> 
      <id>make-assembly</id> 
      <phase>package</phase> 
      <goals> 
       <goal>single</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 
+0

更改或刪除描述符罐-with依賴性。此外,將程序集插件更新到版本2.2,而不是2.2 -... – khmarbaise 2011-02-16 13:01:38

+0

好吧,我更新了版本。但是當我刪除descriptorRef消息時:「讀取程序集時出錯:找不到程序集描述符。」 – phil 2011-02-16 13:11:38

回答

0

也許我缺少一些關於你的問題,但下面的代碼讓我一個可執行的JAR:這是你在找什麼?

   <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-jar-plugin</artifactId> 
       <version>2.3.1</version> 
       <configuration> 
        <archive> 
          <manifest> 
           <addClasspath>true</addClasspath> 
           <mainClass>some.main.class</mainClass> 
          </manifest> 
        </archive> 
        <finalName>Proj-${project.version}</finalName> 
        <outputDirectory>target</outputDirectory> 
       </configuration> 
     </plugin> 
+2

是的,你錯過了,或者我沒有明確提到,我需要該可執行jar中的依賴關係。這就是爲什麼我使用maven-assembly-plugin和jar-with-dependencies設置。 – phil 2011-02-16 17:21:57

1

您可以嘗試設置爲pom。這將阻止創建default罐子。

<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
         http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    ... 
    <packaging>pom</packaging> 
    ... 
</project> 
3

我想我已經想通了如何做到這一點。你必須離開jar,否則你將失去編譯器和資源拷貝以及其他一切與構建你需要的jar一起的事情。

我的解決辦法涉及基本欺騙行家,所以我想提防的事情是,它很可能在未來的版本這是不行的......

<build> 
    <defaultGoal>install</defaultGoal> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <configuration> 
       <source>1.6</source> 
       <target>1.6</target> 
      </configuration> 
     </plugin> 
     <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <configuration> 
       <descriptorRefs> 
        <descriptorRef>jar-with-dependencies</descriptorRef> 
       </descriptorRefs> 
       <finalName>dumpster-diver</finalName> 
       <appendAssemblyId>false</appendAssemblyId> 
       <archive> 
        <manifest> 
         <mainClass>DumpsterDiver</mainClass> 
        </manifest> 
       </archive> 
      </configuration> 
      <executions> 
       <execution> 
        <phase>package</phase> 
        <goals> 
         <goal>single</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
    <finalName>${project.artifactId}</finalName> 
</build> 

注意的是,在構建配置上市,maven-assembly-plugin和構建本身的finalName匹配。另請注意<appendAssemblyId>false</appendAssemblyId>。如果你這樣做,構建和裝配jar的輸出文件名將完全匹配。 Maven肯定會提醒你這個問題,但是會把你想要的jar放到你的目標目錄中並放到你的本地倉庫中。

這裏是「MVN全新安裝」在這個項目上的輸出,以及在目標目錄和本地倉庫兩個罐子罐子-tvf:

$ mvn clean install 
[INFO] Scanning for projects... 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building dumpster-diver 
[INFO] task-segment: [clean, install] 
[INFO] ------------------------------------------------------------------------ 
[INFO] [clean:clean {execution: default-clean}] 
[INFO] Deleting directory <currentDirectory>/target 
[INFO] [resources:resources {execution: default-resources}] 
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! 
[INFO] Copying 1 resource 
[INFO] [compiler:compile {execution: default-compile}] 
[INFO] Compiling 1 source file to <currentDirectory>/target/classes 
[INFO] [resources:testResources {execution: default-testResources}] 
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! 
[INFO] skip non existing resourceDirectory <currentDirectory>/src/test/resources 
[INFO] [compiler:testCompile {execution: default-testCompile}] 
[INFO] No sources to compile 
[INFO] [surefire:test {execution: default-test}] 
[INFO] No tests to run. 
[INFO] [jar:jar {execution: default-jar}] 
[INFO] Building jar: <currentDirectory>/target/dumpster-diver.jar 
[INFO] [assembly:single {execution: default}] 
[INFO] Processing DependencySet (output=) 
[WARNING] Artifact: <groupId>:dumpster-diver:jar:0.1.0-SNAPSHOT references the same file as the assembly destination file. Moving it to a temporary location for inclusion. 
[INFO] Building jar: <currentDirectory>/target/dumpster-diver.jar 
[WARNING] Configuration options: 'appendAssemblyId' is set to false, and 'classifier' is missing. 
Instead of attaching the assembly file: <currentDirectory>/target/dumpster-diver.jar, it will become the file for main project artifact. 
NOTE: If multiple descriptors or descriptor-formats are provided for this project, the value of this file will be non-deterministic! 
[WARNING] Replacing pre-existing project main-artifact file: <currentDirectory>/target/archive-tmp/dumpster-diver.jar 
with assembly file: <currentDirectory>/target/dumpster-diver.jar 
[INFO] [install:install {execution: default-install}] 
[INFO] Installing <currentDirectory>/target/dumpster-diver.jar to /opt/m2repo/<groupId>/dumpster-diver/0.1.0-SNAPSHOT/dumpster-diver-0.1.0-SNAPSHOT.jar 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESSFUL 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 4 seconds 
[INFO] Finished at: Thu Aug 18 13:08:56 EDT 2011 
[INFO] Final Memory: 22M/618M 
[INFO] ------------------------------------------------------------------------ 
[email protected]:<currentDirectory> 
$ jar -tvf ./target/dumpster-diver.jar | grep -i "dump" 
    6831 Thu Aug 18 13:08:54 EDT 2011 DumpsterDiver.class 
    6831 Thu Aug 18 13:08:54 EDT 2011 DumpsterDiver.class 
    0 Thu Aug 18 13:08:56 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/ 
    2580 Thu Aug 18 13:01:46 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/pom.xml 
     126 Thu Aug 18 13:08:54 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/pom.properties 
[email protected]:<currentDirectory> 
$ jar -tvf /opt/m2repo/<groupId>/dumpster-diver/0.1.0-SNAPSHOT/dumpster-diver-0.1.0-SNAPSHOT.jar | grep -i "dump" 
    6831 Thu Aug 18 13:08:54 EDT 2011 DumpsterDiver.class 
    6831 Thu Aug 18 13:08:54 EDT 2011 DumpsterDiver.class 
    0 Thu Aug 18 13:08:56 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/ 
    2580 Thu Aug 18 13:01:46 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/pom.xml 
    126 Thu Aug 18 13:08:54 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/pom.properties 
0

您可以使用此版本下面和編譯與此命令:

MVN清潔編譯組件:單

<build> 
    <plugins> 
     <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <version>3.1.0</version> 
       <configuration> 
        <descriptorRefs> 
         <descriptorRef>jar-with-dependencies</descriptorRef> 
        </descriptorRefs> 
        <archive> 
         <manifest> 
          <mainClass>youMainClass</mainClass> 
         </manifest> 
        </archive> 

        <finalName>${artifactId}</finalName> 
        <appendAssemblyId>false</appendAssemblyId> 
       </configuration> 
       <executions> 
        <execution> 
         <phase>package</phase> 
         <goals> 
          <goal>single</goal> 
         </goals> 
        </execution> 
       </executions> 
     </plugin> 
    </plugins> 
</build> 
相關問題