Maven多模块项目——复制所有“包"从子模块到父/目标/的 JARS [英] Maven multi-module project - copying all "package" JARS from submodules into parent/target/

查看:34
本文介绍了Maven多模块项目——复制所有“包"从子模块到父/目标/的 JARS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含很多子模块的 Maven 项目.我正在寻找的是一种获取聚合 POM 的/target/目录中包含的子模块生成的所有 .jar 文件的方法,以便以后可以方便地使用它们.

I have a maven project with quite a few submodules. What I am looking for is a way to get all the .jar files produced by the sub-modules included in the aggregating POM's /target/ directory, so they can be conveniently used afterwards.

  • 它们不需要合并.最好不要,但如果它们必须,那就没问题.
  • 不在乎依赖
  • 这主要是为了方便,此时

我正在做的事情的基本版本:

A basic version of what I am looking at doing:

Prj1/pom.xml  =>  prj1/target/proj1.jar  (and classes/generated-sources/etc)
Prj2/pom.xml  =>  prj2/target/proj2.jar
Main/pom.xml  =>
                  main/target/proj1.jar
                  main/target/proj2.jar
                  ... classes/generated-sources not needed at all,
                  ... but they could be combined here.  I assume they will be

我一直在阅读,并使用了 SO 的一些建议.到目前为止,我还没有找到一种方法来做到这一点,但我确信它在那里.

I've been reading, and using some suggestions from SO as well. So far I haven't found a way to do this, but I'm sure it is there.

对于所有包含的子项目,我已经放弃以简单的方式使其工作.到目前为止,我最好的答案是使用依赖插件手动指定(再次)要包含的每个子模块.我们的想法是能够为数十个客户端轻松配置 POM,只需包含必要的模块,然后让它神奇地将所有子模块的 jar 粘贴到一个位置.Maven 非常好,当您不怎么使用它时,但是当您尝试时,尖括号税是令人难以置信的.

I've given up on getting this to work in a simple way, for all included subprojets. The best answer I have so far is using the dependancy plugin to manually specify (again), each and every sub-module to be included. The idea was to be able to configure the POMs easily for the dozens of clients, simply including the modules necessary and then having it magically stick all the sub-modules's jars in one location. Maven is pretty nice, when you don't do much with it, but the angle bracket tax is incredible when you try.

我仍然觉得奇怪的是,这些看似标准的任务(从 SO 上提出的问题来看,以及任何正常项目都会做的)如此困难.maven3 更好吗?

I still find it odd that such standard-seeming tasks (judging from the questions asked on SO, and what any normal project would do) are so difficult. Is maven3 better?

推荐答案

你可以试试 maven-dependency-插件:复制 插件:目标.

You could try the maven-dependency-plugin:copy plugin:goal.

您必须将此添加到要复制的所有子模块的 pom 中.或者只是在父 pom 中(见评论).

You will have to add this to the pom of all submodules that you want to copy. Or just in the parent pom (see comments).

    <build>
        <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-dependency-plugin</artifactId>
              <executions>
                <execution>             
                  <id>copy-artifact</id>
                  <phase>package</phase>
                  <goals>
                    <goal>copy</goal>
                  </goals>
                  <configuration>
                    <artifactItems>
                        <artifactItem>
                          <groupId>${project.groupId}</groupId>
                          <artifactId>${project.artifactId}</artifactId>
                          <version>${project.version}</version>
                          <type>${project.packaging}</type>
                        </artifactItem>
                    </artifactItems>
                    <outputDirectory>../Main/target/dependencies</outputDirectory>
                  </configuration>
                </execution>
              </executions>
            </plugin>
        </plugins>
    </build>

如果你确实把它放在父 pom 中,请记住,只有当你的整个项目是一个模块时,它才会很好地工作.这意味着如果父模块下的模块有自己的子模块,它们将不会出现在所需的文件夹中.您的文件夹结构将如下所示:

If you do put this in the parent pom, keep in mind that it will work nicely only if your whole project is one module deep. Meaning that if the the modules under the parent module have their own submodules, they will not end up in the desired folder. Your folder structure will look like this:

- Parent
  - Module 1
    - Sub module 1
    **Main**
  - Module 2
  **Main**

为防止出现这种情况,请使用上述配置创建一个专用模块,并手动指定要复制的每个模块.这样,所有模块,无论它们有多深,最终都会放在一个文件夹中.

To prevent this, create a dedicated module with above configuration, and specify manually each module that you want to copy. This way all modules, no matter how deep they are will end up in one folder.

这篇关于Maven多模块项目——复制所有“包"从子模块到父/目标/的 JARS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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