将两个 Maven 模块合并到一个 jar 中,并让第三个模块在指定位置生成 jar

Merge two maven modules in one jar and have third module generate jar at specified location

我现在已经尝试了 5 个小时,但不确定我错过了什么。 我有关注

+- parent
   pom.xml
   +- core-module
      pom.xml
   +- excel-module
      pom.xml
   +- client-module
      pom.xml
   +- assembly-module
      pom.xml
  1. 我想为 core-module and excel-module 创建一个 core.jar 文件(我已经实现了)assembly-module/target/dist/server/core.jar
  2. 我想在 assembly-module/target/dist/client/client.jar
  3. 上创建单独的 client.jar 文件

以下是我的 pom 文件。

core/pom.xml

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.test.project</groupId>
    <artifactId>parent</artifactId>
    <version>1.0.0.0-SNAPSHOT</version>
</parent>
<artifactId>core</artifactId>
<dependencies>
    <dependency>
        <groupId>com.test.project</groupId>
        <artifactId>excel</artifactId>
        <version>${project.version}</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
</project>

excel/pom.xml

<?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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
    <artifactId>parent</artifactId>
    <groupId>com.test.project</groupId>
    <version>1.0.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>excel</artifactId>
<dependencies>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.3.2</version>
    </dependency>
</dependencies>
</project>

client/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://maven.apache.org/POM/4.0.0"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
    <artifactId>parent</artifactId>
    <groupId>com.test.project</groupId>
    <version>1.0.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>client</artifactId>
<packaging>jar</packaging>
<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifestEntries>
                        <Build-Version>${project.version}</Build-Version>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
      <dependency>
           <groupId>com.test.project</groupId>
           <artifactId>core</artifactId>
           <version>${project.version}</version>>
      </dependency>
</dependencies>

assembly/pom.xml

<?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/xsd/maven-4.0.0.xsd">
<parent>
    <artifactId>parent</artifactId>
    <groupId>com.test.project</groupId>
    <version>1.0.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>assembly</artifactId>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <id>assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptors>
                            <descriptor>src/main/assembly-    descriptor.xml</descriptor>
                        </descriptors>
                        <appendAssemblyId>false</appendAssemblyId>
                         <outputDirectory>${project.basedir}/target/dist/server/</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <finalName>core</finalName>
</build>
<dependencies>
    <dependency>
        <groupId>com.test.project</groupId>
        <artifactId>excel</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>com.test.project</groupId>
        <artifactId>core</artifactId>
        <version>${project.version}</version>
    </dependency>
</dependencies>

assembly/descriptor.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>all-jar</id>
<formats>
    <format>jar</format>
</formats>

<includeBaseDirectory>false</includeBaseDirectory>

<dependencySets>
    <dependencySet>
        <unpack>true</unpack>
        <useTransitiveDependencies>false</useTransitiveDependencies>
    </dependencySet>
</dependencySets>
</assembly>

我需要以什么方式更新 assembly.xml and/or descriptor.xml 以实现第二点。我在 SO

上查看了几乎所有相关帖子

非常感谢任何帮助。

编辑

<build>
<plugins>
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
            <execution>
                <id>core-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/core-assembly-descriptor.xml</descriptor>
                    </descriptors>
                    <appendAssemblyId>false</appendAssemblyId>
                    <outputDirectory>${project.basedir}/target/dist/framework/lib/server/</outputDirectory>
                    <finalName>core.jar</finalName>
                </configuration>
            </execution>
            <execution>
                <id>client-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/client-descriptor.xml</descriptor>
                    </descriptors>
                    <appendAssemblyId>false</appendAssemblyId>
                    <outputDirectory>${project.basedir}/target/dist/framework/runtime/</outputDirectory>
                    <finalName>client.jar</finalName>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

我实际上会尝试在 assembly/pom.xml 中添加另一个执行标记,如下所示:

<execution>
                <id>assembly-client</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/client-assembly-descriptor.xml</descriptor>
                    </descriptors>
                    <appendAssemblyId>false</appendAssemblyId>
                     <outputDirectory>${project.basedir}/target/dist/client/</outputDirectory>
                </configuration>
            </execution> 

并添加一个新的程序集描述符文件(client-assembly-descriptor.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>all-jar</id>
   <formats>
<format>jar</format>
    </formats>
      <includeBaseDirectory>false</includeBaseDirectory>
     <dependencySets>
<dependencySet>
    <unpack>true</unpack>
    <useTransitiveDependencies>false</useTransitiveDependencies>
    <includes>
    <include>com.test.project:client</include>
    </includes>
</dependencySet>

您还需要在程序集项目中将客户端 jar 添加为依赖项。