Finding the root directory of a multi module Maven reactor project

106,140

Solution 1

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>

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

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.

Share:
106,140

Related videos on Youtube

Christoffer Hammarström
Author by

Christoffer Hammarström

My expert areas are Java, Perl, Shell, Unix/Linux, Web, and character encodings.

Updated on July 05, 2022

Comments

  • Christoffer Hammarström
    Christoffer Hammarström almost 2 years

    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
    Christoffer Hammarström almost 14 years
    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...
  • djjeck
    djjeck about 10 years
    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
    djjeck about 10 years
    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.
  • Vic
    Vic about 10 years
    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
    Vic about 10 years
    There are different solutions, but I wanted to find a simple one which does not require copy-paste to all modules.
  • Henry Aloni
    Henry Aloni almost 9 years
    Are you going to manage all these "/.." and "/../.."? I mean, sure this solution works, but it hides the actual structure of your project.
  • Bruce Edge
    Bruce Edge over 8 years
    ${session.executionRootDirectory} was the magic bullet for me too. Why is this not in maven.apache.org/pom.html?
  • filip
    filip almost 8 years
    I think this should be the accepted answer. This even works when you just build an individual module.
  • Pradeeban Kathiravelu
    Pradeeban Kathiravelu over 7 years
    Try with ${project.basedir} if other maven environment variables such as ${session.executionRootDirectory} did not work.
  • Sergio
    Sergio over 7 years
    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).
  • 3urdoch
    3urdoch about 7 years
    This solution works when using eclipse and m2e as it doesn't require any exotic plugins.
  • Dr4gon
    Dr4gon about 7 years
    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 stackoverflow.com/questions/1012402/…
  • Christoffer Hammarström
    Christoffer Hammarström about 6 years
    Could you link to the release notes or docs for this? I can't find them.
  • Grégory Joseph
    Grégory Joseph about 6 years
    @ChristofferHammarström for some reason that only shows up in the release notes of 3.3.9 as a bug fix: 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: issues.apache.org/jira/browse/MNG-5767
  • kaqqao
    kaqqao almost 6 years
    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.
  • Grégory Joseph
    Grégory Joseph almost 6 years
    In which version did this change @kaqqao?
  • kaqqao
    kaqqao almost 6 years
    I maybe said it wrong, but I was referring to this. 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} :(
  • Grégory Joseph
    Grégory Joseph almost 6 years
    @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!
  • kaqqao
    kaqqao almost 6 years
    @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?
  • Grégory Joseph
    Grégory Joseph over 5 years
    @kaqqao i haven't had a chance to test that out, let me know what you find!
  • Nikolai  Shevchenko
    Nikolai Shevchenko over 5 years
    Thanks! This is the most elegant way for simple projects. No additional plugins, just explicit override. +1
  • Benny Bottema
    Benny Bottema about 5 years
    Worked for me without .mvn folder in the root.
  • Christoffer Hammarström
    Christoffer Hammarström over 4 years
    So how does it work when i need the ancestor of all projects, when some child projects might have child projects of their own?
  • armandino
    armandino over 4 years
    This is a good solution. For an earlier phase, you can just specify <phase>pre-clean</phase>.
  • wlnirvana
    wlnirvana about 4 years
    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).
  • Ed Randall
    Ed Randall almost 4 years
    If you are using Git, and get Ant to call git rev-parse --show-toplevel this could just work.
  • Michael-O
    Michael-O over 2 years
    Please do NOT use this property at any time. This is an internal property (implementation detail). It may disappear with a release.
  • Christoffer Hammarström
    Christoffer Hammarström over 2 years
    @GrégoryJoseph Ḯ'm sorry, i'm unmarking this answer as the definitive solution for now, as it seems unclear that it's entirely correct.
  • Grégory Joseph
    Grégory Joseph over 2 years
    @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
    Grégory Joseph over 2 years
    @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) ?
  • Michael-O
    Michael-O over 2 years