43

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?

Community
  • 1
  • 1
thomers
  • 2,603
  • 4
  • 29
  • 50

3 Answers3

24

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)

Community
  • 1
  • 1
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
6

You can determine the root directory of a module with this nice plugin:

    <plugin>
        <groupId>org.commonjava.maven.plugins</groupId>
        <artifactId>directory-maven-plugin</artifactId>
        <version>0.3.1</version>
        <executions>
            <execution>
                <id>directories</id>
                <goals>
                    <goal>directory-of</goal>
                </goals>
                <phase>initialize</phase>
                <configuration>
                    <property>maven-parent.basedir</property>
                    <project>
                        <groupId>my.group</groupId>
                        <artifactId>maven-parent</artifactId>
                    </project>
                </configuration>
            </execution>
        </executions>
    </plugin>

Use the directory like this: ${maven-parent.basedir}

Boris Brodski
  • 8,425
  • 4
  • 40
  • 55
3

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>
basin
  • 3,949
  • 2
  • 27
  • 63
  • 1
    This works...but I believe it can create unnecessary "ibm-jars-bundle/repo" directories in child directories when looking up a local dependency since the first repository definition (ibm-jars-bundle-lv0) is tried first which looks in the root of the current child directory and creates the directory if it doesn't exist. – sdoxsee Dec 18 '15 at 04:32
  • I have an erro because file://${basedir} ... Is replaced as file:///app Note the 3 / – Luis Roberto Feb 21 '19 at 22:34
  • @LuisRoberto it works with 3 slashes: `Downloading from ibm-jars-bundle-lv0: file:///home/il/build/test-jar-bundle/ibm-jars-bundle/repo/foo/test-jar-bundl2/0.1-SNAPSHOT/test-jar-bundl2-0.1-SNAPSHOT.pom` – basin Feb 22 '19 at 17:45