Maven not able to find dependency - java

I am trying add this dependency in my pom.xml, but maven is not able to resolve the dependency for the same.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
I was working on my other computer some weeks ago. I switched my laptop, and trying to setup this project, but it's not working anymore.
Error:
Missing artifact org.springframework:spring-test:jar:4.3.4.RELEASE
I see this this is available in the maven central.
http://search.maven.org/#artifactdetails%7Corg.springframework%7Cspring-test%7C4.3.4.RELEASE%7Cjar
Does anybody have any idea why is it happening?

Thank you #Reek for the comment.
I added spring repository url in my pom.xml and it started working.
<repository>
<id>repository.spring.release</id>
<name>Spring GA Repository</name>
<url>http://repo.spring.io/release</url>
</repository>

You do not need to add spring specific repository to use those artifacts.
Maven works as follows:
Firstly Maven tries to find artifact in the local repository.
Otherwise Maven tries to download artifacts from http://repo1.maven.org/maven2/ by default.
If you wanna build your project on another computer for the first time than there is no local repository at the moment. There are two possible reasons for Error on the second stage:
Maven central repository is unavailable from another computer: check this URL.
Mirror of Maven central is misconfigured in settings.xml like this:
<mirrors>
<mirror>
<id>central-proxy</id>
<name>Proxy of Maven Central</name>
<url>http://some/invalid/url/here</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
Detailed logs are required.

Related

How to consume a jfrog artifactory in a maven project

I created a sample project A and pushed it to my jfrog artifactory. Now, I want to use this in another maven project B.
To achieve this, I added this dependency declaration (copied from jfrog) in B's pom.xml:
<dependencies>
<dependency>
<groupId>com.testartifactory.example</groupId>
<artifactId>testartifactory</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>testartifactory</id>
<name>com.testartifactory.example</name>
<url>https://myjfrogurl/artifactory/libs-release-local/com/testartifactory/example/testartifactory/</url>
</repository>
</repositories>
But when I import this import com.testartifactory.example;, I get error that package com.testartifactory does not exist.
Even though I can see the project downloaded in repositories under .m2 folder:
Here is how my project A is structured:
I am new to this so I may be doing something wrong and would appreciate any help.
The repository URL should correspond to the repository in JFrog Artifactory. In your case: https://myjfrogurl/artifactory/libs-release-local
In the pom.xml:
<repositories>
<repository>
<id>testartifactory</id>
<name>com.testartifactory.example</name>
<url>https://myjfrogurl/artifactory/libs-release-local</url>
</repository>
</repositories>
Other options to use Artifactory for Maven builds are to use the Artifactory Maven plugin or the JFrog CLI. With these integrations, you'll get some more benefits such as build-info, Xray scanning, better performance and more.
Read more about Maven and Artifactory:
Maven Repositories in JFrog Artifactory
QuickStart Guide: Maven and Gradle
Running Maven Builds using the JFrog CLI

Import project from bitbucket to local maven repository

I have a maven project in bitbucket account. I need to add that the project in my local maven repository so that I can refer that project as a maven dependency in my main project's pom.xml.
That is, perform below steps
1) Clone the project
2) run maven build install This will add to local maven repository
Please let me know if there is a way to achieve the same (in similar ways to connecting to maven public repository) ?
You can achieve this by defining repositories in pom file of the project where you want to use project 1 as a dependency.
For eg.
Git clone path of Project 1: https://github.com/yogi21jan/project1
Add below code in child project:
<repositories>
<repository>
<id>YOUR-PROJECT-NAME-mvn-repo</id>
<url>https://raw.github.com/YOUR-USERNAME/YOUR-PROJECT-NAME/mvn-repo/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
And finally Add this project as a dependency:
<dependency>
<groupId>group id of project 1</groupId>
<artifactId>artifcat id of project 1</artifactId>
<version>required version</version>
</dependency>
Look into jitpack.io. It can create a Maven repository for a GitHub project and also other hosters like BitBucket, GitLab, Azure, Gitee.
I have choosen a random Maven based project,
here fastconverter. It not GitHub is used, the full URL has to be provided. The created jar is published then here.

Loading Maven dependencies from GitHub [duplicate]

This question already has answers here:
Can I use a GitHub project directly in Maven?
(3 answers)
Closed 2 years ago.
How do I add a Java library from its GitHub repo (the library uses Maven as a build system) as a dependency to my Maven project? Can I do that without downloading and compiling the library?
Now you can import a Java library from a GitHub repo using JitPack.
In your pom.xml:
Add repository:
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
Add dependency
<dependency>
<groupId>com.github.User</groupId>
<artifactId>Repo name</artifactId>
<version>Release tag</version>
</dependency>
It works because JitPack will check out the code and build it. So you'll end up downloading the jar.
If the project doesn't have a GitHub release then its possible to use a commit id as the version.
At the moment there is no way you can do this unless the maintainer of the library provided a way to do this.
So on the title page of the library the should be an instruction containing the repository address like:
<repositories>
<repository>
<id>YOUR-PROJECT-NAME-mvn-repo</id>
<url>https://raw.github.com/YOUR-USERNAME/YOUR-PROJECT-NAME/mvn-repo/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
And a dependency name:
<dependency>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
</dependency>
This means that all artifact of your project including your dependency will be searched in this repo.
You could also have a glance at pom.xml to check if there was an effort made to deploy artifacts to a remote repo. Typically the keywords are oss.sonatype.org or raw.github.com like in this case.
FYI, here is a way to provide a repo for your gihub artifact: Hosting a Maven repository on github.
Github now supports packages https://help.github.com/en/github/managing-packages-with-github-packages/configuring-apache-maven-for-use-with-github-packages
You can follow the steps above to deploy Jar files to github properly.
Another very nice thing about Jitpack is, it has a lookup button on the main page. And if you type the URL of your GitHub repository, it displays different commits of the source code, and you can select which commit/tag you want. The Jitpack creates pom dependencies for you.
It became dead simple.

How to make m2e (maven integration for eclipse) know where to find dependencies?

When work in STS (springsource tool suite), open pom.xml file and check dependencies in "dependencies tab", add/select maven dependency will give 0 result found. We need edit pom.xml to add dependency by hand. Is there a way to let M2E know where to check repository to search dependency?
M2e by default searches maven central repository for artifacts. It downloads an index file which has artifact details. You can configure additional repositories to be searched by specifying the same in repository section of your settings.xml file. Do note that some repositories do not have this index file.
By default , all pom.xml will automatically extend the Super POM , which is located in the maven_installation_folder/lib/maven-x.x.x-uber.jar ==> package org.apache.maven.project ==> pom-4.0.0.xml . All the configuration specified in the Super POM is inherited by the POMs you created for your projects.
If you open pom-4.0.0.xml , you will find that the maven central repository http://repo1.maven.org/maven2/ is defined here . That means if you specify a <dependency> in your pom.xml , maven will try to download this dependency in the following orders:
Maven local repository (i.e your local hard disk)
Maven central repository specifed in the Super POM (i.e http://repo1.maven.org/maven2/)
Maven remote repository (Defined in the <repository> section of your pom.xml )
Normally , I will use some maven repository search engines , such as this , to find out the <dependency> of the libraries /frameworks /tools that I want to use and then paste them in the pom.xml
If the dependency exists in the Maven central repository , everything will be fine and it will be download to your local repository . However , if some of the <dependency> (eg Hibernate) cannot be found and downloaded from the Maven central repository , you can try to visit its official site to find out its repository link and paste them in the <repository> section of your pom.xml .For example , Hibernate require to define the jboss repository in the pom.xml like this:
<repositories>
<repository>
<id>repository.jboss.org</id>
<name>JBoss Repository</name>
<url>http://repository.jboss.org/nexus/content/groups/public-jboss</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
You can refer to this for the use of <repository>

Is there a way to make Maven download snapshot versions automatically?

So I have a project that depends on a snapshot version of another project. The dependency is:
<dependency>
<groupId>org.oop</groupId>
<artifactId>oop</artifactId>
<version>0.9.9-SNAPSHOT</version>
</dependency>
For the oop project, I did do a 'mvn clean deploy', so the snapshot version should be somewhere in the maven central repository. But when I do a mvn clean install, the snapshot dependency above cannot be resolved and I get this:
Missing:
1) org.oop:oop:jar:0.9.9-SNAPSHOT
Try downloading the file manually from the project website.
Then, install it using the command:
mvn install:install-file -DgroupId=org.oop -DartifactId=oop -Dversion=0.9.9-SNAPSHOT -Dpackaging=jar -Dfile=/path/to/file
Alternatively, if you host your own repository you can deploy the file there:
mvn deploy:deploy-file -DgroupId=org.oop -DartifactId=oop -Dversion=0.9.9-SNAPSHOT -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]
Is there a way to make maven download the snapshot automatically? I must be missing something here.
EDIT1: On my settings.xml I have:
<server>
<id>sonatype-nexus-snapshots</id>
<username>XXXXXX</username>
<password>XXXXXX</password>
</server>
<server>
<id>sonatype-nexus-staging</id>
<username>XXXXXX</username>
<password>XXXXXX</password>
</server>
EDIT2:
Just add this to your ~/.m2/settings.xml:
<profiles>
<profile>
<id>allow-snapshots</id>
<activation><activeByDefault>true</activeByDefault></activation>
<repositories>
<repository>
<id>snapshots-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases><enabled>false</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
</profile>
</profiles>
To update snapshots, try with the -U option
-U,--update-snapshots Forces a check for updated
releases and snapshots on remote
repositories
However, you said:
I did do a 'mvn clean deploy', so the snapshot version should be somewhere in the maven central repository.
This is just not possible, your snapshot is going somewhere else. If I do a mvn clean deploy without configuring my personal repository I get:
Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter
To enable deployment, there is some configuration to be added to pom.xml, like for instance:
<distributionManagement>
<!-- Publish versioned releases here -->
<repository>
<id>myrepo</id>
<name>My releases</name>
<url>http://nexus.mycompany.com/nexus/content/repositories/releases</url>
</repository>
<!-- Publish snapshots here -->
<snapshotRepository>
<id>myrepo</id>
<name>my snapshots</name>
<url>http://nexus.mycompany.com/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<repositories>
<repository>
<id>myrepo</id>
<name>My Public Repository</name>
<url>http://nexus.mycompany.com/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
Maven would try to download the snapshot automatically and indeed it does (as your error indicates). By default, Maven will look for newer snapshot versions once a day, but you can change that interval in your snapshot repository config (e.g. in settings.xml):
<updatePolicy>interval:5</updatePolicy>
This will make maven check every 5 minutes (if you build that often). Alternatively, you could use the -U or --update-snapshots option, to force the check manually.
However, it can't find the dependency. Could you post your repo settings and artifact config for the snapshot dependency?
Is the org.oop:oop:jar:0.9.9-SNAPSHOT artifact in your repository?
... so the snapshot version should be somewhere in the maven central repository.
No it isn't. I tried to look it up, but couldn't find it. Afaik, there's some staging mechanism, so maybe your settings are just wrong. But normally, as the others already said, you'd go and use your own repository manager like Artifactory or Nexus.
Does that dependency exists in your repository? (in pom.xml or settings.xml)?
Looks like not. By the way, take a look at your config, just you are not using -o (offline). Also you can use -U to refresh snapshots.
You can either
use a parent project which builds all your snapshots, or
deploy your snapshots to your maven build server (nexus/archiva/..) using e.g., mvn:deploy
Let's clear up terminology a bit to make sure there is no misunderstanding.
"Maven Central" (http://search.maven.org/) is a global site where you only find releases. Central doesn't accept snapshots so deploying there should give you an error.
You probably mean your local/company wide maven proxy/cache. These can also be configured to reject snapshot versions. In case of Nexus, you can also define more complex rules. In my case, I had an issue there which gave no error during mvn deploy but I could see an error in the server's logs.
Try to follow the data: Enable debug (mvn -X) to see where Maven uploads the data. Then check the server to see whether the artifacts were really uploaded. Check the server's logs for errors.
Also note that snapshot dependencies are only refreshed once a day; so this won't work:
PC #1: mvn install -> Error missing dependency
PC #2: mvn deploy
PC #1: mvn install -> Dependency is still missing because of "update once per day" policy
Try mvn install -U to force Maven to refresh its cached metadata.
I hit the issue of snapshots not updating even when setting -U on the command line. For me the issue was my client was Maven 3 and the server was Maven 2, and in Maven 3 unique snapshots are no longer supported. We had to create a new repository with timestamped snapshots to support the 3.xx clients.

Categories

Resources