Searching the root directory of a maven multi-mode reactor project

I want to use the maven-dependency plugin to copy EAR files from all submodules of my multi-module project into a directory related to the root directory of the entire project.

That is, my layout is similar to this, the names are changed:

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

And I want all EAR files to be copied from the target directories of their respective modules on my-project/../to-deploy , so I get

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

I could do this with a relative path in each ear module, for example:

  <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>ear</type> <outputDirectory>../../to-deploy</outputDirectory> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> 

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

Also, I would prefer some way to inherit this maven-dependency-plugin configuration, so I don't need to specify it for each EAR module.

I also tried to inherit a custom property from the pom root:

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

But when I tried to use ${myproject.root} in the ear POM module, ${basedir} will resolve based on the ear module.

Also, I found http://labs.consol.de/lang/de/blog/maven/project-root-path-in-a-maven-multi-module-project/ , where he suggested that every developer and presumably, the continuous integration server should configure the root directory in the profiles. xml, but I do not consider this a solution.

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

+48
directory module maven-2 root
Jun 21 2018-10-12T00:
source share
9 answers

use ${session.executionRootDirectory}

For the record, ${session.executionRootDirectory} works for me in pom files in Maven 3.0.3. This property will be the directory in which you work, so run the parent project, and each module can get the path to this root directory.

I put a plugin configuration that uses this property in the parent pump so that it inherits. I use it in the profile, which I only select when I know that I am going to run Maven in the parent project. Thus, it is less likely that I will use this variable in an undesirable way when I started Maven in a child project (because then the variable would not be the address of 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> 
+54
06 Oct '11 at 15:30
source share

Something I used in my projects is to override a property in submodular poms.

  root: <myproject.root>${basedir}</myproject.root> moduleA: <myproject.root>${basedir}/..</myproject.root> other/moduleX: <myproject.root>${basedir}/../..</myproject.root> 

That way, you still have relative paths, but you can define the plugin once in the root module, and your modules inherit it with the right substitution for myproject.root.

+18
Jan 13 2018-12-12T00:
source share

There is a maven plugin that solves this problem: directory-maven-plugin

It will assign the root path of your project to the property of your choice. See the goal of highest-basedir in the docs.

For example:

 <!-- Directory plugin to find parent root directory absolute path --> <plugin> <groupId>org.commonjava.maven.plugins</groupId> <artifactId>directory-maven-plugin</artifactId> <version>0.1</version> <executions> <execution> <id>directories</id> <goals> <goal>highest-basedir</goal> </goals> <phase>initialize</phase> <configuration> <property>main.basedir</property> </configuration> </execution> </executions> </plugin> 

Then use ${main.basedir} anywhere in your parent / child pom.xml.

+15
Nov 28 '13 at 11:16
source share

As others have stated, the path to the maven-plugin directory is the path. However, I found that it works best with the purpose of "directory-of", as described here: https://stackoverflow.com/a/212960/

I prefer that using high-basedir does not work for me with a multi-module project with nested multi-module pores. The target directory allows you to set the property to the path of any module in the entire project, including the root, of course. This is also better than $ {session.executionRootDirectory} because it always works, regardless of whether you are creating a root or submodule, and regardless of the current working directory in which you are mvn.

+4
Jun 22 '16 at 10:35
source share

I ran into a similar problem since I needed to copy files between projects. What Maven does is logical because it will keep pom.xml installed in the repository away from the hard coded value.

My solution was to put the copied directory into the Maven artifact and then use Ant to extract / copy

+2
Jun 06 2018-12-12T00:
source share

I don’t know a “good” way to find the root of a multi-module project. But you can improve your current approach a bit.

The first alternative would be to create an additional module directly in the root project, to declare all EARs as dependencies in it and use dependency:copy-dependencies copy the module dependencies into the to-deploy directory (relative). Yes, the path will still be relative, but since the configuration of the dependency plugin will be centralized, I do not find this annoying.

A second alternative would be to use the Maven Assembly Plugin instead of the Maven Dependency Plugin to create the distribution using the dir format (this will create the distribution in the directory). This is actually what I would do.

+1
Jun 21 '10 at 2:04 p.m.
source share

Another solution would be to use the ant task to write "rootdir = $ {basedir}" to target / root.properties in the root project, and then use the Property Plugin to read this file. I have not tried this myself, but I think it should work.?

+1
Dec 14 '10 at 11:18
source share

The following small profile worked for me. I need such a configuration for CheckStyle, which I entered in the config directory in the root of the project, so I can run it from the main module and from submodules.

 <profile> <id>root-dir</id> <activation> <file> <exists>${project.basedir}/../../config/checkstyle.xml</exists> </file> </activation> <properties> <project.config.path>${project.basedir}/../config</project.config.path> </properties> </profile> 

It will not work for nested modules, but I am sure that it can be changed for this, using several profiles with different exists . (I don’t know why the verification tag should have "../ .." in the verification tag and just ".."), but it only works that way.)

0
Jul 02 '13 at 10:44
source share

so: somewhere in the properties of some parent project, I have a file that I need to link later, why do I need an absolute path on it everywhere. So, I get it using groovy :

 <properties> <source> import java.io.File; String p =project.properties['env-properties-file']; File f = new File(p); if (!f.exists()) { f = new File("../" + p); if (!f.exists()) { f = new File("../../" + p); } } // setting path together with file name in variable xyz_format project.properties['xyz_format'] =f.getAbsolutePath() + File.separator + "abc_format.xml"; </source> </properties> 

, and then:

  <properties> <snapshots>http://localhost:8081/snapshots<snapshots> <releases>http://localhost:8081/releases</releases> <sonar>jdbc:oracle:thin:sonar/sonar@localhost/XE</sonar> <sonar.jdbc.username>sonar</sonar.jdbc.username> <format.conf> ${xyz_format}</format.conf> <---- here is it! </properties> 

it works!

0
Mar 26 '16 at 14:25
source share



All Articles