Maven多模块项目 - 将所有"包"JARS从子模块复制到父/目标/

And*_*ker 30 maven multi-module

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

  • 它们不需要合并.最好不要,但如果必须的话那就没问题.
  • 不关心依赖性
  • 这主要是为了方便起见

我正在做的基本版本:

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
Run Code Online (Sandbox Code Playgroud)

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

编辑:

对于所有包含的子程序,我已经放弃了以简单的方式使用它.到目前为止,我得到的最佳答案是使用dependancy插件手动指定(再次)要包含的每个子模块.我们的想法是能够轻松地为数十个客户配置POM,只需包含必要的模块,然后将其神奇地粘贴在一个位置的所有子模块的罐子中.Maven非常好,当你没有做太多时,但是当你尝试时,角括号税是不可思议的.

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

Bra*_*mbo 27

你可以试试maven-dependency-plugin:copy plugin:goal.

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

    <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>
Run Code Online (Sandbox Code Playgroud)

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

- Parent
  - Module 1
    - Sub module 1
    **Main**
  - Module 2
  **Main**
Run Code Online (Sandbox Code Playgroud)

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

  • 天哪,这在父项目中就像一个魅力。万分感谢! (2认同)

aku*_*kuz 15

我花了一整天时间试图解决这个问题......最后它确实有效,所以即使我的日程艰难,我也会在这里分享,如果我能在将来拯救某人的挫败感......

第1步.创建一个额外的Maven项目,该项目将是您要一起复制的所有项目的父项.我们称之为这个项目parent.

这个项目需要告诉Maven,哪些项目要一起构建.此外,您将在其他项目中声明其父级是parent,因此他们将看到MyDir您在此处定义的属性.

<groupId>com.your.domain</groupId>
<artifactId>parent</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
<properties>
    <MyDir>/your/path/to/copy/to</MyDir>
</properties>
<modules>
    <module>../project1</module>
    <module>../project2</module>
    <module>../project2</module>
</modules>
Run Code Online (Sandbox Code Playgroud)

第2步.对于要复制到同一位置的每个项目,请指定它的父项是parent项目(确保指定正确的groupId和artifactId以及父项目的版本):

<parent>
    <groupId>com.your.domain</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1</version>
    <relativePath>../parent</relativePath>
</parent>
Run Code Online (Sandbox Code Playgroud)

而且,对于每个这些项目,还要指定jar和依赖插件设置,如下所示:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
      <configuration>
        <outputDirectory>${MyDir}</outputDirectory>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>com.your.domain.Program</mainClass>
          </manifest>
        </archive>
     </configuration>
</plugin>
<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <phase>install</phase>
        <goals>
          <goal>copy-dependencies</goal>
        </goals>
        <configuration>
          <outputDirectory>${MyDir}/lib</outputDirectory>
        </configuration>
      </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

然后只需运行mvn install父项目.巴姆!

PS以上假设所有项目都位于同一目录(子项旁边的父项目),但您可以根据需要更改相对路径.