Why is maven looking for artifact in the wrong repo? - maven

I'm defining a dependency in pom.xml in a Maven 3 project. Dependency is as follows:
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>el-impl</artifactId>
<scope>runtime</scope>
<version>2.2</version>
</dependency>
Repostory is described in pom as follows:
<repository>
<id>java.net</id>
<name>java.net</name>
<url>http://download.java.net/maven/2</url>
</repository>
Artifact is indeed present in the repository. It's easy to check. Despite that, Maven is trying to obtain the artifact from repo1.maven.org. What could be the reason of this? Maybe I make some crucial mistake in defining repository access? Other dependencies seem to do fine.
Plugin org.mortbay.jetty:maven-jetty-plugin:6.1.26 or one of its
dependencies could not be resolved: Could not find artifact
org.glassfish.web:el-impl:jar:2.2
in central (http://repo1.maven.org/maven2)

The repository that you have defined is used for dependencies, but not for plugins. Hence the error.
To address this, you need to define pluginRepositories:
<project>
<!-- ... -->
<pluginRepositories>
<pluginRepository>
<id>{repo.id}</id>
<url>{repo.url}</url>
</pluginRepository>
</pluginRepositories>
</project>
As to where you should specify - in pom.xml or settings.xml, read this SO post.

You need to check your maven settings.xml (Look into Maven folder: M2_HOME/conf).
The default repositories are defined there itself, and Maven central repository is taking precedence.
Define your repository in Maven's settings.xml like this:
<profiles>
<profile>
...
<repositories>
<repository>
<id>Java Net</id>
<name>Java Net</name>
<releases>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<url>http://download.java.net/maven/2</url>
<layout>default</layout>
</repository>
</repositories>
<pluginRepositories>
...
</pluginRepositories>
...
</profile>
You can maybe overwrite the default Maven central repository location with yours if you don't want to do much configuration.
Cheers!

In my case, the real root issue was that the repo required authentication. However for some reason maven decided that it would be much better to not tell me that, and to instead use the first repo in the <repositories> list, and throw the Could not find artifact error for that repo.
After moving the repo that contained the package so it was the first one in the <repositories> list, it started showing me a "permission denied" message. Once maven was setup for authentication with the repo in question, the issue went away.

I had a similar problem however, another developer had set up a mirror in that I simply copy-pasted into my settings.xml file. Modifying the mirrorOf property to only include specific repos did the trick.

In addition to the above mentioned answers, make sure your settings.xml is saved as an xml file not a text file i.e. its not saved settings.xml.txt.

Related

How to add more sources for Find Jar on Web in IntelliJ

When I right-click a missing import, I have an option of "Find Jar on Web", but the Jars in the list are outdated. For example, the newest ant jar on the list is ant-1.7.1.jar, but I know that ant-1.10.1.jar is available from Maven Central.
How can I add more "sources" to the search of IntelliJ?
This happens because the repositories is outdated in your IntelliJ, so, you can update with these steps, go to:
Preferences > Build, Execution, Deployment > Maven > Repositories
Choose the Remote repository entry and click on Update button, this action takes some minutes, after of that, do the same with your Local repository, IntelliJ will be able to retrieve the last versions of your dependencies.
UPDATE
To view the remote repositories in IntelliJ, you can add in your settings.xml file (check this) or directly in your pom.xml file of your project.
This is the structure for your settings.xml file:
<project>
<repositories>
<repository>
<id>maven-central</id>
<url>http://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
</project>
And this is the structure for your pom.xml file:
<dependencies>
...
</dependencies>
<repositories>
<repository>
<id>codehausSnapshots</id>
<name>Codehaus Snapshots</name>
<releases>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<url>http://repo.maven.apache.org/maven2</url>
<layout>default</layout>
</repository>
</repositories>
After of add this configuration, IntelliJ will be able to detect the new repository.
Notice: The updated specified in your pom.xml file (or inherited from parent project's pom and settings.xml)

Maven in-project repository doesn't work

This is my pom.xml file:
<project ...>
<repositories>
<repository>
<id>lib</id>
<name>lib</name>
<releases>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<url>file://${project.basedir}/src/lib</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>oracle.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
...
<dependencies>
...
</project>
But there is an error shown on the <dependency> tag: Missing artifact oracle.jdbc:ojdbc6:jar:11.2.0.4
The directory structure is this way:
test-project/
src/
lib/
oracle/
jdbc/
ojdbc6/
11.2.0.4/
ojdbc6-11.2.0.4.jar
Anything missing in the pom.xml file or wrong directory structure?
After searching for hours, eventually found the answer works for me.
Your settings.xml file declares a catch-all mirror. This takes effect over the local repository declaration in your pom file. Either remove the catch-all mirror, or try excluding the repository ID of your project repository from the mirroring:
Replace <mirror>*</mirror> with <mirror>*,!lib</mirror> in your MAVEN/conf/settings.xml
The best solution is start using a repository manager (Artifactory, Nexus, Archiva) and install the oracle jdbc driver there and than you can simply use it as a dependency.
If you really don't like to use a repository manager i can recommend the non-maven-jar-maven-plugin.
This is the solution on Windows but it's quite funny:
Replace
file://${project.basedir}/src/lib
by
file://${project.basedir}\src\lib
Reference:
See the comment in: http://blog.dub.podval.org/2010/01/maven-in-project-repository.html

Maven: Why is the -SNAPSHOT suffix missing from artifact file name?

My maven artifact is deployed to a Nexus snapshot repository. There, it is stored in the correct directory, but its filenames have the following pattern:
mylibrary-1.0-20130213.125827-2.jar
However, Maven fails to download that snapshot. According to the error log, Maven seems to expect the following file name:
mylibrary-1.0-SNAPSHOT.jar
These are the repository settings in my pom:
<repositories>
<repository>
<id>mycompany-all</id>
<url>https://servername/nexus/content/groups/mycompany/</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>mycompany-releases</id>
<url>https://servername/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>mycompany-snapshots</id>
<url>https://servername/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
Note: the nexus group includes both the releases and snapshots repo.
I did not configure these repos in settings.xml - is that the problem? Or what else am I doing wrong?
The pattern you posted (mylibrary-1.0-20130213.125827-2.jar) is a unique snapshot version. Maven 3 forces you to use this type of artifact naming, but in Maven 2 it can be prevented with a statement such as:
<distributionManagement>
...
<snapshotRepository>
...
<uniqueVersion>false</uniqueVersion>
</snapshotRepository>
...
</distributionManagement>
To use a specific snapshot in your project, declare it as:
<dependency>
<groupId>com.foo</groupId>
<artifactId>mylibrary</artifactId>
<version>1.0-20130213.125827-2</version>
</dependency>
To use the latest known snapshot, declare it "old-style":
<dependency>
<groupId>com.foo</groupId>
<artifactId>mylibrary</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
You may find the answer to this similar question helpful as well.
I made it work by adding the repositories to the settings.xml like this:
<repositories>
<repository>
<id>mycompany-releases</id>
<url>https://servername/nexus/content/repositories/releases/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
<repository>
<id>mycompany-snapshots</id>
<url>https://servername/nexus/content/repositories/snapshots/</url>
<releases><enabled>false</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
Then, the SNAPSHOT jar files were downloaded just fine. I suspect that when Maven knows it deals with a snapshot repo, it tries both with and without uniqueVersion (see Duncan Jones' answer).
Note that in our case these blocks had to be duplicated as pluginRepositories because we have custom Maven plugins.

Take artifacts from multiple repositories with Maven

How can I configure a maven project to take one of the artifacts from a different repository?
I would like to include this in the project https://github.com/twitter/hadoop-lzo but I can find it only in the twitter repository, not on the central maven repository.
You can set the repositories you want to use in your settings.xml, or in your POM.
If you have a team working on the project, you might want to put this in the POM so everybody has the conf.
You can do it like :
<repositories>
<repository>
<id>Maven Central</id>
<url>http://repo1.maven.org/maven2/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
.... other repos
</repositories>

Maven: local versus remote repository

This may well have been asked before (so apologies in advance if it has been - I just haven't found the right question yet!)
I'm working on a project with two Maven repositories: a local one (on my machine) and an internal one (on a central server). There are quite a few projects kicking around, and one parent project that uses all of them. When we're done working on a particular project, we install it to the central repository for everyone else to use.
When I build the parent project for local testing, I'd like to use the most up to date versions of each project:
if someone has updated a project in the central repository, I'd like to use that one
if I have changed a project locally and installed it into my local repository, I'd like that to override the centralised one.
What I'm seeing is the build completely ignoring my local repository and just grabbing everything from the internal one. Have I missed a setting somewhere obvious? Or is this just the way things work?
mvn -o will take you into offline mode so nothing is downloaded.
Or in your settings.xml set the update policy to not always for snapshots or releases. See here and relevant section below
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
...
<profiles>
<profile>
...
<repositories>
<repository>
<id>codehausSnapshots</id>
<name>Codehaus Snapshots</name>
<releases>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<url>http://snapshots.maven.codehaus.org/maven2</url>
<layout>default</layout>
</repository>
</repositories>
<pluginRepositories>
...
</pluginRepositories>
...
</profile>
</profiles>
...
</settings>
You have to make sure you use a -SNAPSHOT version. Then it will compare local and remote and use whatever is newest.
Of course you need to have to have it configured correctly in your settings.xml (maybe you should show us that..)

Resources