2016-05-16 60 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> 
相關問題