How to create Jar from submodules of module?

1.6k views Asked by At

I have this java maven project structure

- parentModule
    - firstChildModule
    - firstChildModule
    - firstChildModule
        - secondChildModule -> **jar**
            - thirdChildModule
            - thirdChildModule
            - thirdChildModule

And I want to package the secondChildModule module as a jar containing its submodules.

I have tried to set the packaging type to pom but an exception is thrown stating that 'packaging' with value 'jar' is invalid.

So my question is how to do this the right way? I mention that only the thirdChildModule module types will contain source code.

here are the sources for firstChildModule, secondChildModule and

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>

thirdChildModule

 <?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>
1

There are 1 answers

0
btafarelo On

The maven-assembly-plugin does what you need.

https://maven.apache.org/plugins/maven-assembly-plugin/

Set this plugin as follow in your secondchildmodule pom.xml file.

...
<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>

Add this assembly descriptor to setup the output at 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>

The generated file at secondchildmodule/target

jar -tf secondchildmodule-1.0-SNAPSHOT-fat-jar.jar

META-INF/
META-INF/MANIFEST.MF
thirdchildmodule1/
thirdchildmodule1/Main.class
thirdchildmodule2/
thirdchildmodule2/Main.class
thirdchildmodule3/
thirdchildmodule3/Main.class
thirdchildmodule3/another/
thirdchildmodule3/another/pkg/
thirdchildmodule3/another/pkg/AnotherClass.class