Maven multi-module project - copying all "package" JARS from submodules into parent/target/

Andrew Backer picture Andrew Backer · Nov 28, 2011 · Viewed 45.3k times · Source

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.

  • They don't need to be merged. Preferably not, but if they must be then that is ok.
  • Don't care about dependancies
  • This is primarily for convenience, at this point

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

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.

edit:

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.

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?

Answer

Brambo picture Brambo · Nov 28, 2011

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

You will have to add this to the pom of all submodules that you want to copy. EDIT: 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>

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.