Reputation: 298898
In a multi-module maven project, is there a variable that points to the root project folder?
${project.basedir}
points to the current project's directory,${project.parent.basedir}
points to the parent project's directory, but is there a variable that always points to the root directory (the one from which the maven command was executed), no matter from which project inside the reactor?
I realized that the problem I wanted to solve is pretty much unsolvable. I wanted a variable that pointed to either project.basedir, project.parent.basedir, project.parent.parent.basedir etc, whichever is higher. But since a project's parent pom need not be it's parent in the file system, my whole approach won't help. So I am accepting Pascal's answer because it answers my question (even if my question does not solve my problem).
Upvotes: 65
Views: 55573
Reputation: 560
Use directory-maven-plugin with directory-of goal.
Unlike other suggestions:
${session.executionRootDirectory}
The plugin lets you set a property of your choice to the absolute-path of any of the project's modules. In my case I set it to the root module... In my project root pom:
<plugin>
<groupId>org.commonjava.maven.plugins</groupId>
<artifactId>directory-maven-plugin</artifactId>
<version>0.1</version>
<executions>
<execution>
<id>directories</id>
<goals>
<goal>directory-of</goal>
</goals>
<phase>initialize</phase>
<configuration>
<property>myproject.basedir</property>
<project>
<groupId>com.my.domain</groupId>
<artifactId>my-root-artifact</artifactId>
</project>
</configuration>
</execution>
</executions>
</plugin>
From then on, ${myproject.basedir}
in any sub-module pom always has the path of the project root module. And of course, you can set the property to any module, not just the root...
Upvotes: 7
Reputation: 3846
In the latest maven, you can use ${maven.multiModuleProjectDirectory}
.
Upvotes: 45
Reputation: 1356
For me, there was a need for root directory during variable interpolation, not for plugins section - for local directory relative to root with hand-crafted jars. I know this is a bad practice to have local directory with jars, but this was a requirement of project.
Why I was unable to use different solutions:
${session.executionRootDirectory}
and ${user.dir}
are tied with directory from which maven command was executed. I want to refer to the same directory independently of directory, from which maven was launched.${project.basedir}
,as mentioned above, points to current project directory, so child modules will search for jars in wrong location.So, in my case with bad requirements I have used environment variable which refers project root and used it in pom.xml. Use it as last resort, when other solutions do not work. Here is example, how I use environment variable in my case:
<repositories>
<repository>
<id>local-maven-repo</id>
<!--NOTE: export PROJECT_ROOT=<location>-->
<url>file:///${env.PROJECT_ROOT}/local-repo</url>
</repository>
</repositories>
Upvotes: 5
Reputation: 27687
Such property can be created using: directory-maven-plugin.
Using the plugin's highest-basedir
goal you can assign the root path to any property you specify.
Upvotes: 6
Reputation: 6992
In Maven 3, ${session.executionRootDirectory}
is "a variable that always points to the ... directory ... from which the maven command was executed."
Note that this is distinct from a property that gives the top-level root directory of a multi-module project, regardless of where in the directory structure mvn
is executed from. Such a property does not exist to my knowledge, but you can use the ${basedir}/..
hack to achieve it. See this thread on maven-users for more details.
See also: Finding the root directory of a multi module maven reactor project
Upvotes: 25
Reputation: 570365
is there a variable that always points to the root directory (the one from which the maven command was executed)
user.dir
(the working directory) should be that directory.
Upvotes: 49
Reputation: 9776
As far I think, there is no such variable. There are only workaround like in accepted answer of Maven2 property that indicates the parent directory .
Upvotes: 2