2016-05-16 140 views
1

我有一个相当大的超过200个模块的java maven项目,它可以。我试图将所有这些模块合并到单个jar中。如何使用多个子模块从maven项目创建单个库jar?

在某些情况下,只声明一个新的maven依赖项会非常方便,例如。 my-library-5.2.4-SNAPSHOT-bundle.jar或类似的新项目。

我试过使用maven assembly-plugin。我可以创建新的jar文件,jar包含所有的模块jar,并且如果我安装它,它将正确地转到本地.m2文件夹。 Jar可以在其他项目中声明为依赖项。

但问题是我不能在我的java类中导入库中的任何模块。导入不会识别这些内容。

我加入这个build一节我的根pom.xml中:

<build> 
    <plugins> 
     <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <executions> 
       <execution> 
        <id>make-bundle</id> 
        <goals> 
         <goal>single</goal> 
        </goals> 
        <phase>package</phase> 
        <configuration> 
         <descriptors> 
          <descriptor>assembly.xml</descriptor> 
         </descriptors> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

而且我assembly.xml看起来是这样的:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"> 
    <id>bundle</id> 
    <formats> 
     <format>jar</format> 
    </formats> 
    <includeBaseDirectory>false</includeBaseDirectory> 
    <moduleSets> 
     <moduleSet> 
      <useAllReactorProjects>true</useAllReactorProjects> 
      <binaries> 
       <outputDirectory>modules</outputDirectory> 
       <unpack>false</unpack> 
      </binaries> 
     </moduleSet> 
    </moduleSets> 
</assembly> 
+0

http://www.mkyong.com/maven/create-a-fat-jar-file-maven-assembly-plugin/ –

回答

0

Maven的做法:

父模块您可以在其中定义所有子模块共同使用的依赖项和插件。它不应该有它自己的输出。

您应该使用“分布”子模块来聚合所有其他模块工件,而不是尝试在父模块中执行它。

例如 -

所有项目

<?xml version="1.0" encoding="UTF-8"?> 
<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/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>my.library</groupId> 
    <artifactId>my-library-parent</artifactId> 
    <version>1.0.0</version> 
    <packaging>pom</packaging> 

    <distributionManagement> 
     <repository> 
      <id>site</id> 
      <url>http://url_id</url> 
     </repository> 
    </distributionManagement> 

</project> 

现在对于你想使用它的所有项目创建一个简单的父POM文件的项目(含包装“POM”)一般,只是包括这部分:

<parent> 
    <groupId>my.library</groupId> 
    <artifactId>my-library-parent</artifactId> 
    <version>1.0.0</version> 
</parent>