简体   繁体   中英

How to create a single library jar from maven project with multiple submodules?

I have a quite large java maven project with over 200 modules, it is OK as is. I am trying to merge all of those modules to single jar.

In some cases it would be very handy to declare just one new maven dependency eg. my-library-5.2.4-SNAPSHOT-bundle.jar or something similar in new projects.

I have tried to use maven assembly-plugin. I can create new jar file, jar contains all the module jars and it goes properly to local .m2 -folder if I install it. Jar can be declared as dependency in other projects.

But the problem is that I can't import any of those modules inside the library in my java classes. Importing won't recognise those.

I have added this build section to my root pom.xml:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>make-bundle</id>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <descriptors>
                            <descriptor>assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

And my assembly.xml looks like this:

<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>bundle</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <moduleSets>
        <moduleSet>
            <useAllReactorProjects>true</useAllReactorProjects>
            <binaries>
                <outputDirectory>modules</outputDirectory>
                <unpack>false</unpack>
            </binaries>
        </moduleSet>
    </moduleSets>
</assembly>

Maven practices:

Parent modules are where you define the dependencies and plugins used in common by all your child modules. It should not have output of its own.

You should use "distribution" sub-module that aggregates all your other module artifacts, rather than attempting to do it in the parent module.

For example -

Create a simple parent pom file project (with packaging 'pom') generically for all projects

<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>my.library</groupId>
    <artifactId>my-library-parent</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

    <distributionManagement>
        <repository>
            <id>site</id>
            <url>http://url_id</url>
        </repository>
    </distributionManagement>

</project>

Now for all projects which you wish to use it, simply include this section:

<parent>
  <groupId>my.library</groupId>
  <artifactId>my-library-parent</artifactId>
  <version>1.0.0</version>
</parent>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM