Inherit child-dependencies from maven dependency - maven

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?

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.

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.

Related

POM for my own repo is missing, no dependency information available

I am currently learning software development and working on an assignment based on using a release from one project in another using jitpack.
The first project, which I want to add as a dependency in my second, simply has three classes and a JUnit test for each. I changed several things so I am at V5.0 release now.
https://github.com/MohamedMoustafaNUIG/assignment1/releases/tag/V5.0
My second project is not yet pushed, but this is the pom file:
<?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>com.nuig</groupId>
<artifactId>assignment1_driver</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.github.MohamedMoustafaNUIG</groupId>
<artifactId>assignment1</artifactId>
<version>v5.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
</project>
When I try to build with dependencies (in netbeans), I get this error:
Failed to execute goal on project assignment1_driver:
Could not resolve dependencies for project com.nuig:assignment1_driver:jar:1.0-SNAPSHOT:
Failure to find com.github.MohamedMoustafaNUIG:assignment1:jar:v5.0 in
https://jitpack.io was cached in the local repository,
resolution will not be reattempted until the update interval of jitpack.io has
elapsed or updates are forced -> [Help 1]
I also get this warning:
The POM for com.github.MohamedMoustafaNUIG:assignment1:jar:v5.0 is missing, no dependency information available
Even though the zip file (from linked release) has the pom file.
Any help appreciated as I have no idea how to resolve this the issue
You either need to:
build assignment1 on the same machine with clean install to make it available in the local repository.
deploy assignment1 to a Maven repository with clean deploy and then use this as a <repository>.

pom.xml with Jitpack loads wrong version of dependency

I am using JitPack to share a private Github release that I manage.
I have uploaded a new release version to github successfully - when I download it as jar from Github, I see the correct files.
But when I use pom.xml to download it as a dependency via the pom.xml file, it downloads an earlier release version.
I ran mvn dependency:list to see what is actually being downloaded and it says that the correct version is the one being downloaded.
[INFO] com.github.myGroupId:my-artifact-id:jar:THE_CORRECT_VERSION:compile
But when I browse in the jar classes I see that it is still the old version.
The solutions I tried so far:
-submitting another new release
-cleaning my maven local repository
All didn't work. I still get the old version.
The pom.xml code:
<?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>com.shai.test</groupId>
<artifactId>sandbox</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.github.myGroupId</groupId>
<artifactId>my-artifact-id</artifactId>
<version>MY_LATEST_VERSION</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
</project>

Compiling classes with dependent jar files in maven

I want to compile some java files which have multiple dependent jars and make a jar file. I have kept all dependent jars under src/main/lib. after running mvn clean install, i get compilation failure of the classes. Dependent jars are not being copied to class path it seems. Anyone can tell whats going wrong her.
<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>test</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<repositories>
<repository>
<id>external-jars</id>
<name>external-jars</name>
<url>file://${project.basedir}\src\main\lib\</url>
</repository>
</repositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>jars</groupId>
<artifactId>jars</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib/*</systemPath>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>finaljar</finalName>
<sourceDirectory></sourceDirectory>
</build>
</project>
You construction
<dependency>
<groupId>jars</groupId>
<artifactId>jars</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib/*</systemPath>
</dependency>
does not work. In Maven, each artifact is addressed through groupId, artifactId and version. You cannot add a directory, but only separate jars.
While it is possible to add jars through system paths, it is much better to use a repository. Look at the possibilities in
How to add local jar files to a Maven project?

Maven project with prebuilt jar

Currently I am not using Maven in any way whatsoever, but since I am writing a library I would like other developers to be able to use it as a Maven dependency. It seems like the easiest way to do this would be to have a Maven project which just contains the jar.
However, all the examples I've seen of pom.xml have build logic in them and I was wondering how I am supposed specify the prebuilt jar as the resulting artifact.
Thanks!
I do not know if I understand you correctly, so maybe I am commenting something totally different.
If you want to offer your project as Maven dependency for other projects, the project must be somewhere to be downloaded (Maven central repository, your company Nexus...) and your project must have something to identify it: groupId, artifactId and the version. You can specify these separately when installing the artifact, or you can provide a pom that contains this metadata, along with dependencies if there are any. Both approaches are explained here.
Then adding something like this in the project that wants your project as dependency, it should work.
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-a</artifactId>
<version>1.0</version>
</dependency>
Still, since you control this project, you might want to consider converting your library to a Maven project to reap other benefits of Maven (in some IDEs like Eclipse there is an option that convert a regular Java Project in a Maven project), and in the generated pom.xml is the info which is mention above.
Here an example of a basic pom.xml to be a jar.
<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>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Extracted from here --> http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

Download jar from maven repository without using Maven

I am looking for a library that can help me with K-means Vector Quantization. I found a website that offered hope.
http://www.lab4inf.fh-muenster.de/lab4inf/Lab4Drools/dependencies.html
The Lab4Math-1.0.5.jar has the required KmeansQuantization class.
But as see you from the first link, there is no direct download link for the "Lab4Math-1.0.5.jar".
Instead a maven repository is specified.
http://www.lab4inf.fh-muenster.de/lab4inf/maven-repository
Since iam a mobile app developer, i dont use Maven. I am looking for a way to download the jar in any other way. Please help.
Looks like there is a problem with the repository:
[ERROR] Failed to execute goal on project demo: Could not resolve dependencies for project com.demo:demo:jar:1.0-SNAPSHOT:
Failed to collect dependencies for [de.lab4inf:Lab4Math:jar:1.0.5 (compile)]:
Failed to read artifact descriptor for de.lab4inf:Lab4Math:jar:1.0.5:
Could not transfer artifact de.lab4inf:Lab4Math:pom:1.0.5 from/to
Lab4Inf (http://www.lab4inf.fh-muenster.de/lab4inf/maven-repository):
Access denied to: http://www.lab4inf.fh-muenster.de/lab4inf/maven-repository/de/lab4inf/Lab4Math/1.0.5/Lab4Math-1.0.5.pom ,
ReasonPhrase:Forbidden. -> [Help 1]
Tried to retrieve the module metadata file and got a HTTP 403 error....
I was trying to follow the instructions for integrating Math4Lab and discovered it's out of date. Maven 3 forces the specification of a dependency version number and there is a little note at the bottom:
... This way you will have always the newest versions, if snapshots are enabled. At present no final Lab4Math version is available.
My conclusion is that this software is abandonware...
Example
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>de.lab4inf</groupId>
<artifactId>Lab4Math</artifactId>
<version>1.0.5</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>Lab4Inf</id>
<url>http://www.lab4inf.fh-muenster.de/lab4inf/maven-repository</url>
</repository>
</repositories>
</project>
I was able to download the mentioned jar using the following pom.xml
<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>com.demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>tf</name>
<description>Testing Lab4Math</description>
<repositories>
<repository>
<id>Lab4Inf</id>
<url>http://www.lab4inf.fh-muenster.de/lab4inf/maven-repository</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>de.lab4inf</groupId>
<artifactId>Lab4Math</artifactId>
<version>2.0.5-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Note that the only important difference with the accepted answer is the version of the library. (2.0.5-SNAPSHOT instead of 1.0.5)

Resources