2014-08-28 61 views
2

我正在使用下面的插件來生成可執行jar.I在我的類路徑中有一個屬性文件,我想添加到我的jar,但使用程序集插件我我無法做到這一點。我找到了一些答案,說我們可以使用jar插件創建一個jar,然後使用assembly插件打包它們。任何人都可以幫助我如何做到這一點。如何添加classpath屬性到使用maven程序集的可執行jar

我的插件描述

<plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-jar-plugin</artifactId> 
      <configuration> 
       <archive> 
        <manifest> 
         <mainClass>mainclass</mainClass> 
         <addClasspath>true</addClasspath> 
         <classpathPrefix>lib/</classpathPrefix> 
        </manifest> 
       </archive> 
       <includes> 
       <include>myconfig.propeties</include> 
       </includes> 
      </configuration> 
     </plugin> 
     <plugin> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.1</version> 
      <configuration> 
       <source>1.7</source> 
       <target>1.7</target> 
      </configuration> 
     </plugin> 

     <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <configuration> 
       <appendAssemblyId>false</appendAssemblyId> 
       <finalName>app</finalName> 
       <archive> 
        <manifest> 
         <addClasspath>true</addClasspath> 
         <mainClass> 
          mainclass 
         </mainClass> 
        </manifest> 
       </archive> 
       <descriptorRefs> 
        <descriptorRef> 
         jar-with-dependencies 
        </descriptorRef> 
       </descriptorRefs> 
      </configuration> 
      <executions> 
       <execution> 
        <id>make-assembly</id> <!-- this is used for inheritance merges --> 
        <phase>package</phase> <!-- bind to the packaging phase --> 
        <goals> 
         <goal>single</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 

我的罐子與 - dependeinceis文件

<?xml version="1.0" encoding="UTF-8"?> 
<assembly> 
<id>bin</id> 
<!-- Generates a zip package containing the needed files --> 
<formats> 
<format>zip</format> 
</formats> 

<!-- Adds dependencies to zip package under lib directory --> 
<dependencySets> 
    <dependencySet> 
     <!-- 
      Project artifact is not copied under library directory since 
      it is added to the root directory of the zip package. 
     --> 
     <useProjectArtifact>false</useProjectArtifact> 
     <outputDirectory>lib</outputDirectory> 
     <unpack>false</unpack> 
    </dependencySet> 
</dependencySets> 

<fileSets> 
    <!-- 
     Adds startup scripts to the root directory of zip package. The startup 
     scripts are located to src/main/scripts directory as stated by Maven 
     conventions. 
    --> 
    <fileSet> 
     <directory>${project.build.scriptSourceDirectory}</directory> 
     <outputDirectory></outputDirectory> 
     <includes> 
      <include>startup.*</include> 
     </includes> 
    </fileSet> 
    <!-- adds jar package to the root directory of zip package --> 
    <fileSet> 
     <directory>${project.build.directory}</directory> 
     <outputDirectory></outputDirectory> 
     <includes> 
      <include>*.jar</include> 
     </includes> 
    </fileSet> 
</fileSets> 

在此先感謝。

+0

當您執行全新安裝時,目標文件夾是否包含您打算包含在JAR中的屬性文件? – ppuskar 2014-08-28 04:55:40

+0

是的,它包含文件,但它不包含在jar中 – 2014-08-28 05:27:01

回答

2

我通常會發現使用陰影插件更容易。它捆綁了運行jar到一個JAR所需的所有依存關係,所以你不必與類路徑鼓搗,一切都只是在罐子:

http://maven.apache.org/plugins/maven-shade-plugin/

用法示例:

 <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-shade-plugin</artifactId> 
      <version>2.1</version> 
      <configuration> 
       <minimizeJar>true</minimizeJar> 
       <transformers> 
        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
         <mainClass>your.package.Main</mainClass> 
        </transformer> 
       </transformers> 
       <artifactSet> 
        <excludes> 
         <!-- maybe there is stuff you don't want in the jar --> 
         <exclude>log4j:log4j</exclude> 
         <exclude>org.slf4j:slf4j-api</exclude> 
         <exclude>org.slf4j:slf4j-log4j12</exclude> 
        </excludes> 
       </artifactSet> 
      </configuration> 
      <executions> 
       <execution> 
        <phase>package</phase> 
        <goals> 
         <goal>shade</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
相關問題