Aggregate Dependencies in a Multi-Module Maven Project

自作多情 提交于 2019-12-19 04:14:34

问题


I am trying to figure out how to aggregate my maven dependencies in a multi-module project. For example, if I have:

root pom/project1
root pom/project2

and I run mvn dependency:copy-dependencies, I end up with the dependencies in:

root pom/project1/target/dependency
root pom/project2/target/dependency

What I really want is that if I run the mvn command in the root pom folder, all of the dependencies to be copied to root pom/dependency. Is there a maven property that gets me the output directory of the root pom? (similar to ${project.build.directory})? I realize that I can just copy all the dependency folders to the same place after the fact, but I was hoping for something a little cleaner.


回答1:


You will have to configure the dependency plugin to copy depdendencies to a particular location. This can be done by the outputDirectory configuration property.

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-dependency-plugin</artifactId>
   <executions>
      <execution>
         <id>copy-dependencies</id>
         <phase>package</phase>
         <goals>
            <goal>copy-dependencies</goal>
         </goals>
         <configuration>
              <outputDirectory>${outputDir}</outputDirectory>
         </configuration>
      </execution>
   </executions>
</plugin>

But if you trying to do this for distribution, I'd recommend you create an assembly using the maven assembly plugin

The documentation says:

The Assembly Plugin for Maven 2.0 is primarily intended to allow users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distributable archive.




回答2:


What I really want is that if I run the mvn command in the root pom folder, all of the dependencies to be copied to root pom/dependency. Is there a maven property that gets me the output directory of the root pom? (similar to ${project.build.directory})?

No, because modules shouldn't actually be aware of that.

I realize that I can just copy all the dependency folders to the same place after the fact, but I was hoping for something a little cleaner.

The Maven way would to use the Maven Assembly Plugin and a custom descriptor. But if you're not familiar with the Maven Assembly Plugin and its descriptor format, it won't be easy.

A less clean but easier approach would be to configure the Maven Dependency plugin to copy the dependencies into the parent project using a relative path. Something like this:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>2.1</version>
  <configuration>
    <outputDirectory>../root_pom/target/dependency</outputDirectory>
  </configuration>
</plugin>

But as I said, this introduces tight coupling between modules and the root project which is not good at all (and I wouldn't go further by including the goal invocation as part of the build, modules should remain independent and you should be able to build one module without "checkouting" the parent).



来源:https://stackoverflow.com/questions/3435130/aggregate-dependencies-in-a-multi-module-maven-project

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