我有这个java maven项目结构
- parentModule
- firstChildModule
- firstChildModule
- firstChildModule
- secondChildModule -> **jar**
- thirdChildModule
- thirdChildModule
- thirdChildModule
我想将secondChildModule模块封装为一个包含其子模块的jar。
我试图将包装类型设置为pom,但引发了一个异常,指出值为"jar"的"包装"无效。
所以我的问题是如何以正确的方式做到这一点?我提到只有thirdChildModule模块类型才会包含源代码。
以下是firstChildModule、secondChildModule和的来源
firstChildModule
<?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>parentModule</artifactId>
<groupId>com.company</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>firstChildModule</artifactId>
<packaging>pom</packaging>
<modules>
<module>secondChildModule</module>
</modules>
</project>
secondChildModule
<?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>
<groupId>com.company</groupId>
<artifactId>firstChildModule</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>secondChildModule</artifactId>
<packaging>pom</packaging>
<name>Second Child Module</name>
<modules>
<module>thirdChildModule01</module>
<module>thirdChildModule02</module>
<module>thirdChildModule0</module>
</modules>
</project>
第三子模块
<?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>secondChildModule</artifactId>
<groupId>com.company</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>thirdChildModule01</artifactId>
</project>
maven汇编插件可以满足您的需要。
https://maven.apache.org/plugins/maven-assembly-plugin/
在您的secondchildmodule pom.xml文件中按如下方式设置此插件。
...
<artifactId>secondchildmodule</artifactId>
<packaging>pom</packaging>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
添加此程序集描述符以在src/main/assembly/assembly.xml 处设置输出
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.1.1 http://maven.apache.org/xsd/assembly-2.1.1.xsd">
<id>fat-jar</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<moduleSets>
<moduleSet>
<binaries />
</moduleSet>
</moduleSets>
在secondchildmodule/目标生成的文件
jar-tf secondchildmodule--1.0-SNAPSHOT-fat-jar.jar
META-INF/
META-INF/MMANIFEST.MF
第三子模块1/
thirdchildmodule 1/Main.class
第三子模块2/
thirdchildmodule 2/Main.class
第三子模块3/
thirdchildmodule 3/Main.class
第三子模块3/另一个/
thirdchildmodule 3/other/pkg/
thirdchildmodule 3/other/pkg/AntherClass.class