2

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.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Greg
  • 1,339
  • 2
  • 13
  • 18

2 Answers2

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.

naikus
  • 24,302
  • 4
  • 42
  • 43
1

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).

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • @Greg did you find a way to do this with assembly plugin ? – herau Jun 06 '14 at 12:54
  • @herau I don't recall what I did at the time to solve this, but if I was doing it again with what I've learned in the past few years I would use the assembly plugin. You'd just need to add a new maven module at the root level, you can just call it "assembly" or whatever you want. You'd include all your other modules as dependencies in the assembly project's pom file and then your assembly descriptor would include something like: runtime / That should get you close. – Greg Jun 07 '14 at 14:31
  • 1
    ok @Greg, it will try it. Does the assembly module should be linked with the parent's module ? – herau Jun 09 '14 at 15:55