2014-10-31 16 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> 
. 
. 
. 
相关问题