2013-10-08 40 views
3

我可能需要這樣的問題與使用maven的依賴,而不拆包罐子,但可執行的JAR文件包括類路徑

Maven build assembly with dependencies

maven assembly create jar with dependency and class path

如何執行可執行的JAR?在命令行

Java的罐子給我 異常在線程「主要」 java.lang.NoClassDefFoundError:組織/阿帕奇/ log4j的/記錄器

我已經executable.jar我的目標目錄和lib目錄下的所有創造依賴關係jar下的/ lib直接。

這是我的pom.xml的片段。我應該改變什麼?

</dependencies> 
<dependency> 
<groupId><gId></groupId> 
<artifactId><aId></artifactId> 
<version><v></version> 
</dependency> 
<dependency> 
<groupId>gI2</groupId> 
<artifactId>aI2</artifactId> 
<version>v</version> 
</dependency> 
</dependencies> 
<build> 
     <plugins> 
      <plugin> 
       <artifactId>maven-assembly-plugin</artifactId> 
       <configuration> 
        <archive> 
         <manifest> 
          <mainClass>com.proj.app</mainClass> 
         </manifest> 
        </archive> 
        <descriptorRefs> 
         <descriptorRef>jar-with-dependencies</descriptorRef> 
        </descriptorRefs> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 

回答

0

您的發行版需要包含您在應用程序中使用的庫的所有JAR文件。程序集插件專門爲滿足這一要求而設計。

我通常使用th assembly插件創建一個包含整個分佈的ZIP文件。這是我自己的應用程序的JAR文件,所有必需的配置文件,當然還有所有庫JAR。

此外,您的可執行JAR文件需要一個適當的清單文件。也就是說,它需要一個正確的主類入口和一個類路徑。

1

使用Maven的陰影插件

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-shade-plugin</artifactId> 
    <version>1.6</version> 
    <configuration> 
     <createDependencyReducedPom>true</createDependencyReducedPom> 
     <filters> 
      <filter> 
       <artifact>*:*</artifact> 
       <excludes> 
        <exclude>META-INF/*.SF</exclude> 
        <exclude>META-INF/*.DSA</exclude> 
        <exclude>META-INF/*.RSA</exclude> 
       </excludes> 
      </filter> 
     </filters> 
    </configuration> 
    <executions> 
     <execution> 
      <phase>package</phase> 
      <goals> 
       <goal>shade</goal> 
      </goals> 
      <configuration> 
       <transformers> 
        <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" /> 
        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
         <mainClass><Your main class here></mainClass> 
        </transformer> 
       </transformers> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 

這將包括在一個罐子所有的依賴關係,並自動將主類添加到您的清單文件。

確保您運行「包」目標。

0

你可以用App Assembler插件來做到這一點。它會將依賴項複製到一個文件夾並創建快捷方式來執行主類。

相關問題