如何从具有多个子模块的maven项目创建单个库jar? [英] How to create a single library jar from maven project with multiple submodules?

查看:598
本文介绍了如何从具有多个子模块的maven项目创建单个库jar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个超过200个模块的相当大的java maven项目,它是可以的。我试图将所有这些模块合并到单个jar中。

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.

在某些情况下,宣布一个新的maven依赖是非常方便的,例如。 my-library-5.2.4-SNAPSHOT-bundle.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.

我曾尝试使用maven assembly-plugin。我可以创建新的jar文件,jar包含所有模块jar,如果我安装它,它会正确地到本地.m2 -folder。 Jar可以在其他项目中声明为依赖。

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.

但问题是我无法在我的java类中导入库中的任何模块。导入将无法识别这些。

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.

我已将此构建部分添加到我的root pom.xml:

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>

我的assembly.xml看起来像这样:

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做法:

父模块是您定义所有子模块共同使用的依赖项和插件的地方。它不应该有自己的输出。

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.

例如 -

为所有项目创建一个简单的父pom文件项目(包装'pom')

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>

这篇关于如何从具有多个子模块的maven项目创建单个库jar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆