在Maven多模块包中创建JAR文件作为聚合

时间:2018-06-21 19:42:34

标签: maven maven-3

我有以下架构

enter image description here

现在,如果我要建立亲子关系并首先建立孩子并最终与父母建立关系,

<packaging>jar</packaging>

要求:

我需要具有以下功能的包装:

在父项目“ A”上运行的命令-mvn全新安装包等 首先创建罐子“ B”,“ C”,“ D” 然后创建罐子“ A” 然后在Jar A内添加“ B”,“ C”,“ D” jar

当我添加模块时

  <modules>
        <module>../B</module>
         <module>../C</module>
         <module>../D</module>
    </modules> 

然后用Maven强制添加

<packaging>pom</packaging>

装有

<packaging>jar</packaging>

问题:

当我添加包装pom时,罐子“ A”不会撕裂

所以我试图创建一个超级pom

enter image description here

POM Super:

   <packaging>pom</packaging>

    <modules>
        <module>../A</module>

    </modules> 

POM A:

   <parent>
        <groupId>com.khan.vaquar</groupId>
        <artifactId>Super</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> 
    </parent>

<packaging>pom</packaging>

 <modules>
        <module>../B</module>
         <module>../C</module>
         <module>../D</module>
    </modules> 


<dependencies>

        <!-- B -->
         <dependency>
            <groupId>com.khan.vaquar</groupId>
            <artifactId>B</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency> 
        <!-- C -->
         <dependency>
            <groupId>com.khan.vaquar</groupId>
            <artifactId>C</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency> 
        <!-- D-->
        <dependency>
            <groupId>com.khan.vaquar</groupId>
            <artifactId>D</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency> 
        <!-- Swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>

POM B:

<packaging>jar</packaging>
   <parent>
        <groupId>com.khan.vaquar</groupId>
        <artifactId>A</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> 
    </parent>

POM C:

<packaging>jar</packaging>
   <parent>
        <groupId>com.khan.vaquar</groupId>
        <artifactId>A</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

POM D:

<packaging>jar</packaging>

<parent>
        <groupId>com.khan.vaquar</groupId>
        <artifactId>A</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/>
    </parent>

问题:如果要添加模块,Maven不允许添加jar,所以我如何将子jar添加到父jar并创建内部版本。

1 个答案:

答案 0 :(得分:4)

要从多个模块创建胖子,可以在项目中使用 maven-shade-plugin

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.3</version>
    <executions>
        <execution>
            <id>create-fat-jar</id>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                <!-- add Main-Class to manifest file -->
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.nk.test.Application</mainClass>
                    </transformer>
                </transformers>
                    <finalName>A</finalName>
            </configuration>
        </execution>
    </executions>
</plugin>

对于您的示例,您可以将结构创建为

父模块

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.nk.test</groupId>
  <artifactId>P</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <modules>
   <module>../C</module>
   <module>../B</module>
   <module>../A</module>
  </modules>

</project>

以maven-shade-plugin和项目B&C作为依赖项的核心项目A

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.nk.test</groupId>
  <artifactId>A</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <dependencies>
   <dependency>
   <artifactId>B</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <groupId>com.nk.test</groupId>
   </dependency>
     <dependency>
   <artifactId>C</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <groupId>com.nk.test</groupId>
   </dependency>
  </dependencies>

  <build>
   <plugins>
   **<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.3</version>
    <executions>
        <execution>
            <id>create-fat-jar</id>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                <!-- add Main-Class to manifest file -->
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.nk.test.Application</mainClass>
                    </transformer>
                </transformers>
                    <finalName>A</finalName>
            </configuration>
        </execution>
    </executions>
</plugin>**
   </plugins>
  </build>
</project>

依赖模块B

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.nk.test</groupId>
  <artifactId>B</artifactId>
  <version>0.0.1-SNAPSHOT</version>
</project>

依赖模块C

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.nk.test</groupId>
  <artifactId>C</artifactId>
  <version>0.0.1-SNAPSHOT</version>
</project>

在父项目上运行maven安装。

这将导致命名为 A.jar 的Fat jar,其中包含 B C 作为其依赖项。

您可以在以下网址找到示例:https://github.com/nomanbplmp/maven-multimodule-fat-jar