Maven variable for reactor root - maven-2

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).

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.

In the latest maven, you can use ${maven.multiModuleProjectDirectory}.

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

Use directory-maven-plugin with directory-of goal.
Unlike other suggestions:
This solution works for multi-module projects.
It works whether you build the whole project or a sub-module
It works whether you run maven from the root folder or a sub-module (unlike ${session.executionRootDirectory}
There's no need to set a relative path property in each and every sub-module!
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...

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.

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.
I had about 100 projects, so defining relative paths or usage of accepted answer for this question is quite complex in my case.
Directory plugin can be used only for plugin configurations, not for variable interpolation
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>

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 .

Related

Arquillian Shrinkwrap provide custom settings.xml file

I have set up a arquillian project and it all works fine locally but when running the maven build on our continuous integration box it fails. I managed to get to the bottom of the problem and it is because the maven user settings.xml file on the CI box actually has some server definitions (for another project) whereas locally to my machine it is the default file (with no servers defined). The tests throw a RuntimeException because the MavenResolver goes looking for settings-security.xml file which doesn't exist on the CI box.
I would prefer not to go adding the settings-security.xml and instead would like to provide the resolver with a 'empty' settings.xml file, or even better, tell it not to use one. Is this possible?
I tried the answer from this thread https://developer.jboss.org/thread/174873 which pretty much says to add the following option to the failsafe plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version><!-- or upper -->
<configuration>
<systemPropertyVariables>
<org.apache.maven.user-settings>nonExistentSetting.xml</org.apache.maven.user-settings>
</systemPropertyVariables>
</configuration>
</plugin>
If i do this and provide an xml file that doesnt exist all work fine from the command line running 'mvn clean install' but when i debug within my IDE it still fails.
Is there a way i can either:
Tell the maven resolver not to use a settings.xml / use a 'default' file
Create an empty settings.xml in my project and tell the maven resolver to use that
I did try the following line in my code to build my archive but it didnt seem to have an effect:
.addAsLibraries(Maven.configureResolver().fromFile("test-settings.xml").loadPomFromFile("pom.xml").importRuntimeDependencies().resolve().withTransitivity().asFile())
Thanks for any help,
Craig
ShrinkWrap resolver by default loads settings-security.xml from
DEFAULT_SETTINGS_SECURITY_PATH = userHome == null ? ".settings-security.xml" : userHome.concat("/.m2/settings-security.xml").replace('/', File.separatorChar);
but you can override this location by setting
System.setProperty(MavenSettingsBuilder.ALT_SECURITY_SETTINGS_XML_LOCATION,
"someDir/empty-settings-security.xml");
When alternative location (non-empty string) is provided theoretically it should skip the default location. However I am not 100% sure this method will work.

Maven - Best way to refer to a directory on the system path

I am trying to build an RPM from my Maven project. I have 5 different modules and each one has its own pom.xml, In the root I have one pom.xml which builds all modules (Typical Maven Setup). When I build an RPM, I want to include a directory that is not part of the maven directories. Its above a directory [from the root folder that contains my maven modules]. What is the best way to include that in my RPM? or rather what is the best way to refer to a directory with out hardcoding the path? I am confused about ${baseDir} and what it refers to?
Thank you.
${project.basedir} refers to the root of the project, ie where the pom.xml is, so you could use that in <systemPath>${project.baseDir}/../../dirYouWant</systemPath>
In general though, Maven best-practices would frown about relying on the relative paths around your projects from being there. Instead, I suggest deploying those files as there own project to your maven repository (as a zip, jar, whatever), and then getting them as part of your rpm build. Depending on what plugin you are using to build your RPM, you can unpack those files automatically.
Try this
<dependency>
...groupid,artifactid etc..
<scope>system</scope>
<systemPath>path/to/your/jar</systemPath>
</dependency>
Did you mean you want to add another project to your maven build being level above?
you can do it like this :
in your parent pom :
<modules>
<module>../projectdirectory</module>
</modules>
in your projectdirectory pom :
<parent>
<groupId>...</groupId>
<artifactId>...parent...</artifactId>
<version>...</version>
<relativePath>../parentProject/pom.xml</relativePath>
</parent>

Specifying jar file in maven build argument

We have our project build using maven. We try to run our unit test cases in maven build itself and for doing that we need to add DB2 driver jar in the dependency of all the sub projects.
Instead of doing that, we need a solution to specify the absolute path of the jar file as a mvn command line argument to use it in the running of unit test cases.
This is because the driver jar is available in our app server lib folder and we don't want to specify it in the dependencies of our projects.
Couldn't find a suitable solution googling it, hence requesting for an expert solution here.
Any workaround would be of greater help.
Thanks in advance.
The usual way would be to add a dependency to the database driver and limit the dependency to testing (test scope). So the library is available for unit tests but will not deployed and jar'ed.
Practically spoken, I'd create a maven artifact for this driver (just a basic POM file) and place it on the build servers maven repository (or the nexus, if you use it for the projects).
I'm using a dependency with scope set to 'system' to reference a jar that is available in the container but not in any maven repository. In this case the jar is put in a folder named 'lib' in the project like this, :
<dependency>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>version</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/library.jar</systemPath>
</dependency>
The groupId, artifactId and version can be set to any value you want, the trick was that system dependencies have to be given with an absolute path, which is worked around by using the project.basedir property. It should also be possible to specify the complete path as a property.
We have our project build using maven. We try to run our unit test cases in maven build itself and for doing that we need to add DB2 driver jar in the dependency of all the sub projects.
Well, the maven way would be to declare the DB2 driver as dependency with a test scope in a parent project.
Instead of doing that, we need a solution to specify the absolute path of the jar file as a mvn command line argument to use it in the running of unit test cases.
You could use the additionalClasspathElement in the plugin configuration to pass the path to the driver:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>path/to/additional/resources</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
If you variablelize it, you could pass the value on the command line.
But to be honest, I can't understand why you don't install the driver in a corporate repository and declare it as dependency. And if you don't have a corporate repository, use a file based repo as described in this previous answer (please, don't use the system scope bad practice). There is no good reason to go the hacky way.

In Maven, can I specify a relative path above my current project?

I'm learning maven on the fly while doing work on a project with a large set of projects to build.
Currently a line in the main build uses an absolute path to specify a directory that is part of the subversion repository but "above" it's directory.
as in: "C:/work/project/eclipse" where "project" is the checked-in directory, and the pom.xml is in "C:/work/project/src/subproject/pom.xml"
I'd like to make that line a relative address instead.
I tried specifying "../../Eclipse....", put that didn't seem to work.
It could also be because that same variable is being used by a sub-sub-project's pom file.
Any advice (aside from rewrite the whole mess, which I just don't know enough about maven to do yet)?
With Maven, things are relative to the directory containing the pom.xml (which is represented by the ${basedir} property and is called the base directory). There are however some situations where you could have to specify a relative path:
if a <parent> pom is not directly above a given module using a <relativePath> element (see this example)
if modules are not nested (i.e. if you use a flat layout) using a relative path in the various <module> elements (like in this example).
Having all that said, I'm not sure to understand what your situation exactly is or what you are describing.
Maybe you should show the relevant parts of your POM if this is possible.
Ideally the pom.xml should be in the same directory as your src directory. For eg suppose you have checked out your project from svn into a folder C:/work/project, then keep the main pom.xml in C:/work/project. src should also be in C:/work/project.
Your other subprojects should be in C:/work/subproject1, C:/work/subproject2 and so on each having their own pom.xml. Then in the main pom.xml you can refer to the other projects in the
<modules>
<module>../subproject1<module>
<module>../subproject2<module>
</modules>

using maven to manage java dependencies in a jruby rails app

I'm trying to write a pom.xml that will allow me to run a command locally and fetch all dependencies that my jruby Rails app has. I'm seeing two different configs though and I'm not totally sure which to use (as I'm not a java person whatsoever)
First, many Pom's i'm seeing just have a tag under the root of the pom.xml that list all dependencies. This doesn't however have any information about where these are stored etc... so I feel like this isn't what I want (I need to copy them to my rails lib dir)
Second option, I'm seeing in the mvn docs is to use the maven-dependency-plugin, which seems more like what i'm looking for. I assume then that my outputDirectory would be something like lib
So I don't fully understand what the purpose of the first option's dependency list is for. All I want is mvn to copy my jars locally (and then eventually when my CI server does a deploy). Can someone point me in the right direction?
First Option
<project>
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
</project>
Second Option
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<artifactItems>
<artifactItem>
<groupId>[ groupId ]</groupId>
<artifactId>[ artifactId ]</artifactId>
<version>[ version ]</version>
<type>[ packaging ]</type>
<classifier> [classifier - optional] </classifier>
<overWrite>[ true or false ]</overWrite>
<outputDirectory>[ output directory ]</outputDirectory>
<destFileName>[ filename ]</destFileName>
</artifactItem>
</artifactItems>
<!-- other configurations here -->
</configuration>
</plugin>
</plugins>
</build>
</project>
First, many Pom's i'm seeing just have a tag under the root of the pom.xml that list all dependencies. This doesn't however have any information about where these are stored etc... so I feel like this isn't what I want (I need to copy them to my rails lib dir)
This is the traditional way to declare and use dependencies on a Java project. Dependencies declared under the <dependencies> element are downloaded from a "remote repository" and installed to your "local repository" (in ~/.m2/repository by default) and artifacts are then handled from there. Maven projects (at least the Java ones) don't use a local lib/ folder for their dependencies.
Second option, I'm seeing in the mvn docs is to use the maven-dependency-plugin, which seems more like what i'm looking for. I assume then that my outputDirectory would be something like lib
The maven dependency plugin allows to interact with artifacts and to copy/unpack them from the local or remote repositories to a specified location. So it can be used to get some dependencies and copy them in lets say a lib/ directory indeed. Actually, it has several goals allowing to do this:
dependency:copy takes a list of artifacts defined in the plugin
configuration section and copies them
to a specified location, renaming them
or stripping the version if desired.
This goal can resolve the artifacts
from remote repositories if they don't
exist in local.
dependency:copy-dependencies takes the list of project direct
dependencies and optionally transitive
dependencies and copies them to a
specified location, stripping the
version if desired. This goal can also
be run from the command line.
The first goal would use the setup you described in your second option. The second goal would use the standard project dependencies that you described in your first option. Both approaches would work.
The problem here is that I don't know exactly what a JRuby Rails app is, what the development workflow is, how to build such an app, etc so I don't know exactly what you need to do and, consequently, what would be the best way to implement that with Maven.
So I googled a bit and found this post that shows another approach based on OS commands (using the maven exec plugin) and has a complete pom.xml doing some other things. Maybe you should look at it and use it as a starting point instead of reinventing everything. This is my suggestion actually.