89

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?

Christoffer Hammarström
  • 27,242
  • 4
  • 49
  • 58

15 Answers15

80

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>
Community
  • 1
  • 1
anonymous
  • 825
  • 1
  • 7
  • 3
  • 3
    ${session.executionRootDirectory} was the magic bullet for me too. Why is this not in http://maven.apache.org/pom.html? – Bruce Edge Sep 04 '15 at 17:32
  • Try with ${project.basedir} if other maven environment variables such as ${session.executionRootDirectory} did not work. – Pradeeban Kathiravelu Sep 28 '16 at 21:18
  • 8
    I tried ${session.executionRootDirectory} with Maven 3.3.9 and it doesn't work anymore. You need to use ${user.dir} instead. In my opinion this option is unsatisfactory because it means I can only run the parent and not submodules independently. For that to work you need this http://stackoverflow.com/questions/1012402/maven2-property-that-indicates-the-parent-directory – Dr4gon May 02 '17 at 14:23
72

Since Maven 3.3.1, you can use ${maven.multiModuleProjectDirectory} for this purpose. (thanks to https://stackoverflow.com/a/48879554/302789)

edit: this seems to only work properly when you have a .mvn folder at the root of your project.

Grégory Joseph
  • 1,549
  • 16
  • 14
  • 1
    Could you link to the release notes or docs for this? I can't find them. – Christoffer Hammarström Mar 28 '18 at 07:14
  • 2
    @ChristofferHammarström for some reason that only shows up in the release notes of 3.3.9 as a bug fix: https://maven.apache.org/docs/3.3.9/release-notes.html But digging further in Maven's Jira project, this is where it gets introduced, and is indeed marked as fixed-for 3.3.1: https://issues.apache.org/jira/browse/MNG-5767 – Grégory Joseph Mar 28 '18 at 11:01
  • 2
    Sadly, they "fixed" it and now it just points to the current subproject, not the actual multiproject root. So it's no better than the other options. – kaqqao Jul 16 '18 at 20:02
  • In which version did this change @kaqqao? – Grégory Joseph Jul 17 '18 at 01:50
  • 1
    I maybe said it wrong, but I was referring to [this](https://issues.apache.org/jira/browse/MNG-5786). It used to point to the actual root (in some cases at least) prior to 3.3.9, and it now always points to the dir where Maven was executed. So it's not the root if executed from a subproject... so no better than `${session.executionRootDirectory}` :( – kaqqao Jul 17 '18 at 10:57
  • 8
    @kaqqao yeah it's odd. I can't say I understand exactly what that bug reports, but when I try `mvn help:evaluate -Dexpression=maven.multiModuleProjectDirectory` it works for me ¯\\_(ツ)_/¯ edit: I realised it seems to only work when I have an `.mvn` folder at the top of my project! – Grégory Joseph Aug 01 '18 at 11:20
  • @GrégoryJoseph Ah, that explains it! Thanks for the update. Anything special needed in the .mvn directory or just the presence of the dir is enough? – kaqqao Aug 01 '18 at 13:08
  • @kaqqao i haven't had a chance to test that out, let me know what you find! – Grégory Joseph Aug 06 '18 at 08:24
  • 2
    Worked for me **without** `.mvn` folder in the root. – Benny Bottema Feb 08 '19 at 19:17
  • 4
    Tried with maven 3.6.3. Conclusion: 1) there has to be a `.mvn` folder, but it can be empty 2) mvn reports the root directory for entire project properly even running the command from a child directory (module). – wlnirvana Apr 21 '20 at 08:54
  • 2
    Please do NOT use this property at any time. This is an internal property (implementation detail). It may disappear with a release. – Michael-O Aug 23 '21 at 13:23
  • @GrégoryJoseph Ḯ'm sorry, i'm unmarking this answer as the definitive solution for now, as it seems unclear that it's entirely correct. – Christoffer Hammarström Aug 24 '21 at 14:46
  • @ChristofferHammarström that's fair enough, especially given Michael-O 's answer just above -- I haven't been using Maven in anger recently enough to need/verify this. – Grégory Joseph Nov 23 '21 at 13:43
  • @Michael-O any chance you have an official answer, then? And/or a trail folks who actually need this can follow (e.g open bug/feature request) ? – Grégory Joseph Nov 23 '21 at 13:44
  • 2
    @GrégoryJoseph https://issues.apache.org/jira/browse/MNG-7038 – Michael-O Nov 23 '21 at 13:51
  • What a happy accident, that I stumble across this, just as it was resolved yesterday and pre-released with mvn 4.0.0-alpha 6 :) – Rick Moritz Apr 21 '23 at 13:39
32

Something which I have used in my projects is to override the property in the sub-module poms.

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

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

Karel Vervaeke
  • 339
  • 3
  • 2
25

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

It will assign the root path of your project to a property of your choosing. See highest-basedir goal 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.

arulraj.net
  • 4,579
  • 3
  • 36
  • 37
Andrejs
  • 26,885
  • 12
  • 107
  • 96
  • 5
    I suggest to *avoid* this solution: if you don't execute the phase (in the example is `initialize`) the property won't be solved, so, if you need it for example on a submodule at `clean/pre-clean` phase you will find yourself duplicating/hacking the config on submodules (already saw it on various projects). Use @karel 's solution unless you have a special requirement (and no, your project is not special). – Sergio Oct 26 '16 at 13:52
  • 1
    This is a good solution. For an earlier phase, you can just specify `pre-clean`. – armandino Nov 13 '19 at 18:01
  • Even after running the goal explicitely, the property does not resolve for a config of another plugin in the same reactor. I guess it's not as fool proof as it looks. Actually running the `initialize` lifecycle phase instead of the `highest-basedir` goal made it work. – Rick Moritz Apr 21 '23 at 13:54
7

As others have suggested, directory-maven-plugin is the way to go. However, I found it works best with the directory-of goal, as described here: https://stackoverflow.com/a/37965143/6498617.

I prefer that as using highest-basedir didn't work for me with a multi-module project, with nested multi-module poms. The directory-of goal lets you set a property to the path of any module in the whole project, including the root of course. It is also way better than ${session.executionRootDirectory}, because it always works, regardless of whether you build the root or a sub-module, and irrespective of the current working directory where you mvn from.

sparkyd
  • 550
  • 6
  • 6
6

Currently, Maven does not provide such the «Root project directory path» property out of the box.
«Root project» means the «topmost» parent project.

Please, refer to the ticket and consider voting for it: [MNG-7038] Introduce public property to point to a root directory of (multi-module) project - ASF JIRA.

3

The following small profile worked for me. I needed such a configuration for CheckStyle, which I put into 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 won't work for nested modules, but I'm sure it can be modified for that using several profiles with different exists's. (I have no idea why there should be "../.." in the verification tag and just ".." in the overriden property itself, but it works only in that way.)

Vic
  • 1,778
  • 3
  • 19
  • 37
  • So are you saying that the basedir is ${project.basedir}/../../? This only works in the case you're putting the child projects at a specific location inside the parent project. The question is, how to find this path in general. – djjeck Mar 14 '14 at 19:39
  • The basedir is ` ${project.basedir}/../`. Just some unique file is needed there (in my case it's `checkstyle.xml`). And yes, this works (as I mentioned) for one-level of module nesting. Usually modules are put into subdirectories, so I don't consider here any other cases (but I'm sure something similar will work). The question was how to find something like `${reactor.root.directory}`. Here is my solution. But in my case I needed the `/config` folder. With a slight modification one can get the root directory as well. And for nested modules several profiles with different `exist`'s also work. – Vic Mar 19 '14 at 19:56
  • There are different solutions, but I wanted to find a simple one which does not require copy-paste to all modules. – Vic Mar 19 '14 at 20:00
2

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

My solution was to put copied dir in a Maven artifact, then employ Ant to extract/copy

Hoang TO
  • 61
  • 4
2

Except use absolutely root path, in some case, I have another way to solve the problem.

Use maven plugin combine.children feature, refs: maven plugin

I can write my code:

parent pom:

<build>
  <plugins>
    <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>
                  <includeScope>runtime</includeScope>
                  <outputDirectory combine.children="override">${project.basedir}/to-deploy</outputDirectory>
              </configuration>
          </execution>
      </executions>
    </plugin>
  </plugins>
</build>

or write in plugin management, if parent pom doesn't use this plugin.

child pom:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <executions>
          <execution>
              <!--same as parent plugin id-->
              <id>copy-dependencies</id>
              <configuration>
                  <outputDirectory combine.children="override">${project.parent.basedir}/to-deploy</outputDirectory>
              </configuration>
          </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Similar way grandchild pom:

<outputDirectory combine.children="override">${project.parent.parent.basedir}/to-deploy</outputDirectory>
1

I'm not aware of a "nice" way to find the root of a multi-module project. But you can maybe improve a bit your current approach.

A first alternative would be to create an additional module directly under the root project, to declare all EARs as dependencies in it and to use dependency:copy-dependencies to copy the dependencies of the module to the to-deploy directory (relatively). Yes the path would still be relative but since the dependency plugin configuration would be centralized, I don't find it that annoying.

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

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 1
    Thank you! Using the assembly plugin would be the best if it was only possible to understand the docs and link it into the build lifecycle. I've just spent an hour trying to make it work, and given up... – Christoffer Hammarström Jun 21 '10 at 15:29
1

Another solution would be to use an ant task to write "rootdir=${basedir}" to a target/root.properties in the root project, and then use the Properties Plugin to read that file back in. I haven't tried it myself, but I guess it should work..?

neu242
  • 15,796
  • 20
  • 79
  • 114
  • 3
    And how would you know the location of that file, from the pom file of a referenced module? Actually, if you knew the location of that file, you wouldn't have to read its content. – djjeck Mar 14 '14 at 18:59
  • If you are using Git, and get Ant to call `git rev-parse --show-toplevel` this could just work. – Ed Randall Jun 08 '20 at 21:18
1

You can go with something like this. Note, that you have to define two profiles - one which will be activated for the root directory and one for children. Obviously this assumes children will have the same structure but this can be easily adapted.

    <profiles>
        <profile>
            <id>root-dir</id>
            <activation>
                <file>
                    <exists>${basedir}/lib</exists>
                </file>
            </activation>
            <properties>
                <project.libdir>${basedir}/lib</project.libdir>
            </properties>
        </profile>
        <profile>
            <id>child-dir</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <project.libdir>${basedir}/../../lib</project.libdir>
            </properties>
        </profile>
    </profiles>
kboom
  • 2,279
  • 3
  • 28
  • 43
0

so that: somewhere in properties of some parent Project I've the file which i need to relate later, what's why i need absolute path on it everywhere. So, i get it with help of 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!

Iakov Senatov
  • 151
  • 1
  • 5
0

With Maven 3.6.1, from a child project, ${parent.basedir} returns the fully qualified directory of the parent project.

devdanke
  • 1,309
  • 15
  • 27
0

I've got root directory for parent pom with ${project.parent.basedir}.

Vlad Cheremisin
  • 141
  • 1
  • 11