jstarek
jstarek

Reputation: 1542

Maven > 3.04 looks for local repositories in the wrong places

I am currently working on an older Java project that we need to build with a current maven, ideally 3.1 or higher.

The project requires a version of the Oracle libraries that is not available via Maven Central, so we created a local repository for it. This works fine on a colleague's machine using maven 3.0.4, but higher versions report the following error:

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] maventest ......................................... SUCCESS [0.008s]
[INFO] maventest-core .................................... FAILURE [0.288s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.442s
[INFO] Finished at: Thu Dec 11 17:01:48 CET 2014
[INFO] Final Memory: 6M/150M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project maventest-core: Could not resolve dependencies for project tld.org.maventest:maventest-core:jar:0.1.6-SNAPSHOT: Could not find artifact com.oracle:oracle:jar:10.2.0.2.0 in maventest.local (file:/home/jstarek/src/maventest/maventest-core/repository) -> [Help 1]enter code here

I'd like to understand why maven searches ~/src/maventest/maventest-core/repository. The repository is actually located one level higher up, at ~/src/maventest/repository. As is evident by the build success for "maventest", at that top level, the repository is resolved to the correct directory, and the file is found. But descending into maventest-core, it assumes a wrong repository location.

Here's a sketch of the project's directory hierarchy:

maventest/
├── maventest.properties
├── maventest-core
│   ├── pom.xml
│   └── src
│       ├── ...
│
├── pom.xml
├── repository
│   └── com
│       └── oracle
│           ├── ojdbc14
│           │   ├── 10.2.0.2.0
│           │   │   ├── ojdbc14-10.2.0.2.0.pom
│           │   │   └── ojdbc14-10.2.0.2.0.pom.sha1
│           │   ├── 10.2.0.3.0
│           │   │   ├── ojdbc14-10.2.0.3.0.pom
│           │   │   └── ojdbc14-10.2.0.3.0.pom.sha1
│           │   └── 10.2.0.4.0
│           │       ├── ojdbc14-10.2.0.4.0.pom
│           │       └── ojdbc14-10.2.0.4.0.pom.sha1
│           └── oracle
│               ├── 10.2.0.2.0
│               │   ├── oracle-10.2.0.2.0.jar.lastUpdated
│               │   ├── oracle-10.2.0.2.0.pom
│               │   └── oracle-10.2.0.2.0.pom.lastUpdated
│               └── maven-metadata-local.xml

The dependencies are specified in maventest/pom.xml as follows:

<repositories>
  <repository>
    <id>maventest.local</id>
    <name>maventest</name>
    <url>file:${project.basedir}/repository</url>
  </repository>
</repositories>

<dependencyManagement>
  <dependencies>
    <dependency>
      ....
    </dependency>
    <dependency>
      <groupId>com.oracle</groupId>
      <artifactId>oracle</artifactId>
      <version>10.2.0.2.0</version>
    </dependency>
  </dependencies>
</dependencyManagement>

...and one level deeper, at maventest/maventest-core/pom.xml , as follows:

<dependencies>
  <dependency>
    <groupId>com.oracle</groupId>
    <artifactId>oracle</artifactId>
  </dependency>
</dependencies>

It almost looks like ${project.basedir} was different there... any ideas? What is happening, and how did this behaviour change between 3.0.4 and later versions?

Upvotes: 0

Views: 209

Answers (2)

khmarbaise
khmarbaise

Reputation: 97359

Simple answer is to start using a repository manager and install those artifacts there and afterwards you can use such artifacts simply as usual dependencies without any hassle. Everthing else will need more maintainance as really needed makes more headaches than you would like to have.

Upvotes: 0

tmarwen
tmarwen

Reputation: 16354

${project.basedir} is a maven predefined property that will always refer to the base path of the current module/project, i.e. a relative url and not an absolute unaltered one. To resolve the issue, I would suggest either of the following:

  • I would suggest either specifying an absolute directory path as your repository URL, so that parent and child modules will use the same unchanged path:
<repositories>
  <repository>
    <id>maventest.local</id>
    <name>maventest</name>
    <url>file://absolute/path/to/repository</url>
  </repository>
</repositories>
  • Otherwise, if you insist on avoiding absolute paths you can use point your repository to a custom property, e.g. ${test.repository.url} that will be altered in each modules level to keep the path relatively under the root parent project path:

Parent artifact:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

      <modelVersion>4.0.0</modelVersion>
      <groupId>tld.org.maventest</groupId>
      <artifactId>maventest</artifactId>
      <version>0.1.6-SNAPSHOT</version>

      <repositories>
        <repository>
          <id>maventest.local</id>
          <name>maventest</name>
          <url>file:${test.repository.url}</url>
        </repository>
      </repositories>

      <properties>
        <test.repository.url>${project.basedir}/repository</test.repository.url>
      </properties>
    </project>

Child module:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

      <modelVersion>4.0.0</modelVersion>    
      <parent>
        <groupId>tld.org.maventest</groupId>
        <artifactId>maventest</artifactId>
        <version>0.1.6-SNAPSHOT</version>
      </parent>

      <groupId>tld.org.maventest</groupId>
      <artifactId>maventest-core</artifactId>

      <properties>
        <test.repository.url>${project.basedir}/../repository</test.repository.url>
      </properties>
    </project>

Upvotes: 2

Related Questions