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

百般思念 提交于 2020-01-06 04:56:09

问题


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>

回答1:


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>


来源:https://stackoverflow.com/questions/37253993/how-to-create-a-single-library-jar-from-maven-project-with-multiple-submodules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!