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

vaq*_*han 3 maven-3 maven

我有以下架构

在此处输入图片说明

现在,如果我要建立亲子关系并首先建立孩子并最终结束,它将可以正常工作

<packaging>jar</packaging>
Run Code Online (Sandbox Code Playgroud)

要求 :

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

在父项目“ A”上运行命令-mvn clean install package等首先创建Jar“ B”,“ C”,“ D”,然后创建Jar“ A”,然后在Jar中添加“ B”,“ C”,“ D” jar一种

当我添加模块时

  <modules>
        <module>../B</module>
         <module>../C</module>
         <module>../D</module>
    </modules> 
Run Code Online (Sandbox Code Playgroud)

然后行军添加

<packaging>pom</packaging>
Run Code Online (Sandbox Code Playgroud)

装的

<packaging>jar</packaging>
Run Code Online (Sandbox Code Playgroud)

问题 :

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

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

在此处输入图片说明

POM超级:

   <packaging>pom</packaging>

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

    </modules> 
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

POM B:

<packaging>jar</packaging>
   <parent>
        <groupId>com.khan.vaquar</groupId>
        <artifactId>A</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> 
    </parent>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

POM D:

<packaging>jar</packaging>

<parent>
        <groupId>com.khan.vaquar</groupId>
        <artifactId>A</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/>
    </parent>
Run Code Online (Sandbox Code Playgroud)

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

小智 7

要从多个模块创建胖子罐,您可以在A项目中使用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>
Run Code Online (Sandbox Code Playgroud)

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

父模块

<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>
Run Code Online (Sandbox Code Playgroud)

以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>
Run Code Online (Sandbox Code Playgroud)

依赖模块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>
Run Code Online (Sandbox Code Playgroud)

从属模块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>
Run Code Online (Sandbox Code Playgroud)

在父项目上运行maven install。

这将导致名为A.jar的 Fat罐具有 BC作为依赖关系。

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