Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of maven ${basedir} in multi-module setup

Tags:

maven-2

maven

I am using a local repository as described in Maven: add a dependency to a jar by relative path.

The repository-url is defined in the topmost pom.xml as

<url>file:${basedir}/../3rdParty/maven-repository</url> 

Also, the topmost pom.xml defines 2 modules

<modules>     <module>sub1</module>     <module>sub2</module> </modules> 

The problem is, that if a module (say sub1) defines a dependency that should be downloaded from the repository, and maven is called from the topmost directory, the ${basedir} is not set to this directory, but to sub1, resulting in a wrong repository-URL.

So, say the project with the topmost pom.xml resides in

/Development/myproject/pom.xml 

And the repository is in

/Development/3rdParty/maven-repository 

Then the repository URL should be set to

/Development/myproject/../3rdParty/maven-repository 

but it turns out it is set to

/Development/myproject/sub1/../3rdParty/maven-repository 

which of course does not exist.

Any idea why that is the case?

like image 206
thomers Avatar asked May 19 '11 16:05

thomers


People also ask

What is ${ Basedir in Maven?

${project. basedir} is the root directory of your project. ${project.build.directory} is equivalent to ${project.basedir}/target. as it is defined here: https://github.com/apache/maven/blob/trunk/maven-model-builder/src/main/resources/org/apache/maven/model/pom-4.0.0.xml#L53.

What is multi-module Maven project?

A multi-module project is built from an aggregator POM that manages a group of submodules. In most cases, the aggregator is located in the project's root directory and must have packaging of type pom. The submodules are regular Maven projects, and they can be built separately or through the aggregator POM.

How can I add an existing Maven project to another Maven project as a module?

Just do a regular "Import existing maven project into workspace" to get this done. Show activity on this post. If you use M2e with Eclipse you do not need to do that, because Eclipse resolves dependencies across the workspace. You just need to have the two projects open and your dependencies declared correctly.


2 Answers

Although it is annoying in your case, this is well-known and intentional. A maven project should know about its execution directory only, no matter in what context it is executed.

I asked almost the same question: Maven variable for reactor root earlier, and the only answer that made sense was to use ${user.dir}, although it's hacky and will not work if you build from a module directory.

(There is also this very verbose solution: Maven2 property that indicates the parent directory)

like image 83
Sean Patrick Floyd Avatar answered Sep 21 '22 08:09

Sean Patrick Floyd


How about having multiple repos?

<repositories>     <repository>         <id>ibm-jars-bundle-lv0</id>         <url>file://${basedir}/ibm-jars-bundle/repo</url>     </repository>     <repository>         <id>ibm-jars-bundle-lv1</id>         <url>file://${basedir}/../ibm-jars-bundle/repo</url>     </repository>     <repository>         <id>ibm-jars-bundle-lv2</id>         <url>file://${basedir}/../../ibm-jars-bundle/repo</url>     </repository> </repositories> 
like image 32
basin Avatar answered Sep 20 '22 08:09

basin