Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 541
Andrew Avatar asked Nov 28 '11 07:11

Andrew


People also ask

What is the packaging type of an aggregator pom in a multi-module Maven project?

2. Maven's Multi-Module Project. A multi-module project is built from an aggregator POM that manages a group of submodules. In most cases, the aggregator is located in the project's root directory and must have packaging of type pom.

What is packaging type of parent project in a multi-module Maven project?

For a multimodule Maven project, In the root directory, there is a pom. xml there which is the parent pom and its packaging is pom .

Can two Maven modules depend on each other?

Because modules within a multi-module build can depend on each other, it is important that the reactor sorts all the projects in a way that guarantees any project is built before it is required. The following relationships are honoured when sorting projects: a project dependency on another module in the build.


2 Answers

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.

like image 181
Brambo Avatar answered Sep 22 '22 08:09

Brambo


I have spent the entire day trying to solve this... and finally it works, so even though I am on a tough schedule, I will share it here, if I can only save someone's frustration in the future...

Step 1. Create an additional Maven project that will be a parent of all the projects that you want to copy together. Let's call this project parent.

This project is needed to tell Maven, which projects to build together. Also, you will declare in your other projects that their parent is parent, so they will see the MyDir property that you define here.

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

Step 2. For every project that you want to be copied to the same location, specify that it's parent is the parent project (make sure you specify correct groupId and artifactId and version of your parent project):

<parent>     <groupId>com.your.domain</groupId>     <artifactId>parent</artifactId>     <version>0.0.1</version>     <relativePath>../parent</relativePath> </parent> 

And also, for each of these projects, also specify the jar and dependency plugins settings, like so:

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

Then just run mvn install on the parent project. Bam!

P.S. The above assumes all projects are located in the same directory (parent project next to children), but you can change relative paths as you wish.

like image 20
akuz Avatar answered Sep 20 '22 08:09

akuz