Basti
Basti

Reputation: 1186

Inherit child-dependencies from maven dependency

I have two separate maven projects.
Project A contains utility classes and similar stuff. It also uses jetbrains annotations in some interfaces to mark parameters as Nullable and NotNullable.

<dependency>
    <groupId>org.jetbrains</groupId>
    <artifactId>annotations</artifactId>
    <version>20.1.0</version>
</dependency>

Project B uses some of the Utilities of Project A. It includes it as a dependency from my repository.

<dependency>
    <groupId>org.test.group</groupId>
    <artifactId>Utilities</artifactId>
    <version>1.0</version>
</dependency>

I can access the classes from my utilities dependency just fine. But i do not see any of the annotations on the parameters. I also can't access the jetbrains annotations in project B in any of my classes there. I'd have to add the jetbrains dependency in project B as well to do so.

Is there any way to inherit the dependencies of another dependency?
I looked at other questions and found this similar one. Tho his solution was to set the optional-parameter to false which i am not even using. Perhaps also something that needs to be configured in the maven build? Currently im running my build with goals clean package deploy without any special additional configuration.
I know gradle builds allow for implementation and api dependencies, where one of them forwards the dependency to other projects that include it and the other doesn't.

Edit: Here the full configuration of my projects
Local nexus running under localhost:8081 containing my artifacts. Local TeamCity running under localhost:8080 used for the builds and deployment to the repository.

Project A pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.test.group</groupId>
    <artifactId>Utilities</artifactId>
    <version>1.0</version>

    <name>Utilities</name>

    <dependencies>
        <dependency>
            <groupId>org.jetbrains</groupId>
            <artifactId>annotations</artifactId>
            <version>20.1.0</version>
        </dependency>
    </dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <distributionManagement>
        <repository>
            <id>local_nexus</id>
            <name>Deployment</name>
            <url>http://localhost:8081/repository/org.test.group/</url>
        </repository>
    </distributionManagement>
</project>

Project B pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.test.group</groupId>
    <artifactId>TestProject</artifactId>
    <version>1.0</version>
    <name>TestProject</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.test.group</groupId>
            <artifactId>Utilities</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>local_nexus</id>
            <name>Deployment</name>
            <url>http://localhost:8081/repository/org.test.group/</url>
        </repository>
    </repositories>

    <distributionManagement>
        <repository>
            <id>local_nexus</id>
            <name>Deployment</name>
            <url>http://localhost:8081/repository/org.test.group/</url>
        </repository>
    </distributionManagement>

</project>

Edit 2:
I've made some progress. Since i didn't change Project A's version after adding the annotations my local repository didn't fetch the new version. After a purge using mvn dependency:purge-local-repository the updated state of version 1.0 was available.
mvn dependency:tree prints now

[INFO] org.test.TestProject:jar:1.0
[INFO] \- org.test:Utilities:jar:1.0:compile
[INFO]    \- org.jetbrains:annotations:jar:20.1.0:compile

Tho my IDE (Intellij) still doesn't recognize the annotation classes inside TestProject. Any idea why this is failing now?

Upvotes: 1

Views: 804

Answers (2)

Basti
Basti

Reputation: 1186

The issue was caused by my local maven repository. Since i didn't change the version of Project A's artifact (always 1.0) my local repository kept providing me with the old state of version 1.0.
Using mvn dependency:purge-local-repository i was able to cleanse the old 1.0 and load the new 1.0. Tho still my IDE refused to imports annotations dependency. At this point it was listed by mvn dependency:tree tho.
After tempering with the IDE for a while i decided to update my version to 1.1. Once Project A's 1.1 was built and Project B's dependency was updated to 1.1 it worked.

So basically it resulted from poor versioning of my projects, which interfered with my local maven repository.

Upvotes: 0

J Fabian Meier
J Fabian Meier

Reputation: 35843

You inherit annotations automatically, no configuration needed.

You don't inherit it, if it is of scope provided. Look at mvn dependency:tree to find out about the place and scope of the annotations library.

BTW: mvn clean package deploy is a waste of time, just use mvn clean deploy.

Upvotes: 1

Related Questions