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).
Use directory-maven-plugin with directory-of goal.
Unlike other suggestions:
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:
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...
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:
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
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.user.dir
(the working directory) should be that directory.In the latest maven, you can use
${maven.multiModuleProjectDirectory}
.