2014-10-31 22 views
2

我在尋找一個pom.xml配置,它將我的應用程序打包到一個jar文件中,並將所有應用程序依賴關係打包到另一個jar文件中。我看着Maven的組裝插件,並具有以下配置我是能夠建立一個應用程序罐子,然後用所有依賴的應用程序的jar,但我需要依賴罐子不包含我的應用程序:使用Maven在單獨的jar中構建和打包應用程序和依賴關係

<plugin> 
    <artifactId>maven-assembly-plugin</artifactId> 
    <version>2.4</version> 
    <configuration> 
     <descriptorRefs> 
      <descriptorRef>jar-with-dependencies</descriptorRef> 
     </descriptorRefs> 
     <finalName>${project.artifactId}-${project.version}</finalName> 
     <appendAssemblyId>false</appendAssemblyId> 
    </configuration> 
    <executions> 
     <execution> 
      <id>jar-with-dependencies</id> 
      <phase>package</phase> 
      <goals> 
       <goal>attached</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 

謝謝

回答

2

我已經看到通過在模塊中構建應用程序解決了這個問題。這裏有一個很好的博客:http://giallone.blogspot.com/2012/12/maven-install-missing-offline.html?_sm_au_=iVVnK0HqJZDtb1Hw

對於你的情況,你可以有一個名爲'dependencies'的模塊,它使用maven-dependency-plugin和maven-assembly-plugin來複制所有依賴並將它們打包罐。然後,您的應用程序可以引用該jar,因爲它依賴於單獨的模塊。頂層將構建兩者。

頂層POM

. 
. 
. 
    <packaging>pom</packaging> 
. 
. 
    <modules> 
     <module>dependencies</module> 
     <module>yourApp</module> 
    </modules> 
. 
. 
. 

依賴性POM

. 
. 
. 
    <dependencies> 
     <dependency> 
      <groupId>some.group.id</groupId> 
      <artifactId>yourFirstDependency</artifactId> 
      <version>1.0</version> 
     </dependency> 
     <dependency> 
      <groupId>some.group.id2</groupId> 
      <artifactId>yourSecondDependency</artifactId> 
      <version>1.1.4</version> 
     </dependency> 
    <dependencies> 

    <build> 
     <defaultGoal>install</defaultGoal> 
     <finalName>${project.artifactId}</finalName> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-dependency-plugin</artifactId> 
       <executions> 
        <execution> 
         <phase>package</phase> 
         <goals> 
          <goal>copy-dependencies</goal> 
         </goals> 
         <configuration> 
          <outputDirectory>${project.build.directory}</outputDirectory> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-assembly-plugin</artifactId> 
       <configuration> 
        <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> 
    </build> 
. 
. 
. 

主應用程序POM

. 
. 
. 
    <!-- Reference your dependency module here as the first in the list --> 
    <dependencies> 
     <dependency> 
      <groupId>your.group.id</groupId> 
      <artifactId>yourDependencyJarName</artifactId> 
      <version>1.0</version> 
     </dependency> 
     . 
     . 
    </dependencies> 
. 
. 
. 
相關問題