Finding the root directory of a multi module maven reactor project

maven multi module project root directory
multi module maven project spring boot
how to run maven multi module project
maven profiles in multi module project
maven multi module project single jar
maven release multi module project
maven multi module archetype
maven project directory

I want to use the maven-dependency-plugin to copy EAR-files 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/
    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 the EAR-files to be copied from the target-directories of their respective modules to my-project/../to-deploy so i end up with

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 it with a relative path in each ear-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>ear</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 EAR-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 ear-module POM's, the ${basedir} would resolve to the basedir of the ear-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?

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.

Maven variable for reactor root, is there a variable that always points to the root directory (the one See also: Finding the root directory of a multi module maven reactor project. You say things are relative to the directory containing the pom.xml, but shouldn't it be things are relative to the directory containing the pom.xml being built?, because for example (and maybe I'm wrong) if you use somewhere in a parent module a variable like ${project.basedir} and then you build the child module, ${project.basedir} becomes the directory for the child, not the parent, so that

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>

Maven – Guide to Working with Multiple Modules, 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  utiliser ${session.executionRootDirectory}. Pour le dossier ${session.executionRootDirectory} travaille pour moi dans le pom fichiers dans Maven 3.0.3. Cette propriété sera le répertoire dans lequel vous exécutez, alors exécutez le projet parent et chaque module peut obtenir le chemin vers ce répertoire racine.

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.

Multi-Module Project with Maven, Learn about the benefits of using a Maven multi-module project and walk In most cases, the aggregator is located in the project's root directory and must have Basically, this kind of POM declares its modules explicitly in its pom.xml file. If you'd like to find more details about Maven, have a look at the  I'm creating a multi-module project that builds many other, related projects. Some of these projects are multi-module projects as well, and in some cases they build the same sub-project. Maven ye

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.

Project root path in a Maven multi module project, In a multi module Maven project, it seems non trival to reference the project root location from the sub modules deeper down in the module  Finding the root directory of a multi module maven reactor project (7) 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.

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.

Maven multi-module - build only a sub-module - maven - html, I have a multi-module project P with sub-modules A, B and C. A is dependent build from A's directory passing the relative path to dependent modules B and C. a certain module is located, Maven will find it for you if it is part of the reactor. EDIT Ran an mvn -N clean install to install only the root project's POM in the rep. Starting with the Maven 2.1 release, there are new Maven command line options which allow you to manipulate the way that Maven will build multi-module projects. These new options are:-rf, --resume-from Resume reactor from specified project-pl, --projects Build specified reactor projects instead of all projects-am, --also-make

Create multi-module maven project with console, Learn how to create multi-module maven project from console IDE. Notice how archetypeArtifactId is set to pom-root. In console, navigate to parent project folder and create further modules for services (common code e.g. DAO), rws (rest services or web component) Find me on Facebook and Twitter. The reactor plugin will use Maven's built-in SCM providers to attempt to figure out which files you've modified. (By default, it will ignore "unknown" files: files that have been created in a source directory but which haven't been explicitly added to source control.)

Versions Maven Plugin – Changing the project version, version specified in a property · Fixing a multi-module build · Resolve version ranges · Locking The set goal can be used to update the version of the current module. It will automatically climb up local directories to find the aggregation root. [INFO] Reactor build order: [INFO] Enforcer [INFO] Enforcer API [INFO] Enforcer​  Running a specific Maven plugin goal from the command line in a sub-module of a multi-module reactor project (3) I have a multi-module project and I'd like to run the exec:java plugin from the command-line against one of the sub-modules of my project.

Building a multi-module enterprise Java application with Maven and , The finish directory in the root of this guide contains the finished application. Navigate to the ear folder and find a template pom.xml file. Because you have multiple modules, aggregate the Maven projects to simplify the build process. Other things about Maven I personally dislike: Lack of support for accessing root pom folder for shared build resources: Other things about Maven I personally dislike: Lack of support for accessing root pom folder for shared build resources:

Comments
  • Could you link to the release notes or docs for this? I can't find them.
  • @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
  • 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.
  • 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} :(
  • @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!
  • ${session.executionRootDirectory} was the magic bullet for me too. Why is this not in maven.apache.org/pom.html?
  • Try with ${project.basedir} if other maven environment variables such as ${session.executionRootDirectory} did not work.
  • 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/…
  • Are you going to manage all these "/.." and "/../.."? I mean, sure this solution works, but it hides the actual structure of your project.
  • I think this should be the accepted answer. This even works when you just build an individual module.