Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the root directory of a multi module Maven reactor project

Is there an easy way to find the root of a multi-module Maven project, like Gradle's rootDir?


Background:

I want to use the maven-dependency-plugin to copy artifacts from all sub-modules of my multi-module project to a directory that is relative to the root directory of the entire project.

That is, my layout looks similar to this, names changed:

to-deploy/ my-project/     module-a/     module-b/     more-modules-1/         module-c/         module-d/     more-modules-2/         module-e/         module-f/     ... 

And i want all the artifacts to be copied from the target-directories of their respective modules to my-project/../to-deploy so i end up with

to-deploy/     module-a.jar     module-b.jar     module-c.jar     module-d.jar     module-e.jar     module-f.jar my-project/     ... 

I could do it with a relative path in each module, like so:

        <plugin>             <groupId>org.apache.maven.plugins</groupId>             <artifactId>maven-dependency-plugin</artifactId>             <executions>                 <execution>                     <id>copy</id>                     <phase>install</phase>                     <goals>                         <goal>copy</goal>                     </goals>                     <configuration>                         <artifactItems>                             <artifactItem>                                 <groupId>${project.groupId}</groupId>                                 <artifactId>${project.artifactId}</artifactId>                                 <version>${project.version}</version>                                 <type>jar</type>                                 <outputDirectory>../../to-deploy</outputDirectory>                             </artifactItem>                         </artifactItems>                     </configuration>                 </execution>             </executions>         </plugin> 

But i'd rather not specify a relative path in the <outputDirectory> element. I'd prefer something like ${reactor.root.directory}/../to-deploy, but i can't find anything like this.

Also, i'd prefer if there was some way to inherit this maven-dependency-plugin configuration so i don't have to specify it for each module.

I also tried inheriting a custom property from the root pom:

<properties>     <myproject.root>${basedir}</myproject.root> </properties> 

But when i tried to use ${myproject.root} in the module POM's, the ${basedir} would resolve to the basedir of the module.

Also, i found http://labs.consol.de/lang/de/blog/maven/project-root-path-in-a-maven-multi-module-project/ where it's suggested that each developer and presumably the continuous integration server should configure the root directory in a profiles.xml file, but i don't consider it a solution.

So is there an easy way to find the root of a multi-module project?

like image 352
Christoffer Hammarström Avatar asked Jun 21 '10 12:06

Christoffer Hammarström


People also ask

What is Maven root directory?

Standard Maven Directory StructureThe myprojectdir is the root dir for your project. You are free to choose whatever directory name you want for your project. The pom. xml file is the Maven POM file (Project Object Model). The POM file is described in more detail here: Maven POM file .

What is multi-module project in Maven?

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. The submodules are regular Maven projects, and they can be built separately or through the aggregator POM.


1 Answers

use ${session.executionRootDirectory}

For the record, ${session.executionRootDirectory} works for me in pom files in Maven 3.0.3. That property will be the directory you're running in, so run the parent project and each module can get the path to that root directory.

I put the plugin configuration that uses this property in the parent pom so that it's inherited. I use it in a profile that I only select when I know that I'm going to run Maven on the parent project. This way, it's less likely that I'll use this variable in an undesired way when I run Maven on a child project (because then the variable would not be the path to the parent).

For example,

<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>${session.executionRootDirectory}/target/</outputDirectory>             </configuration>         </execution>     </executions> </plugin> 
like image 56
anonymous Avatar answered Sep 28 '22 07:09

anonymous