Maven uses wrong repository to lookup dependencies - maven

In my settings.xml I declared a custom repository in a profile like this:
<servers>
<server>
<id>server.id</id>
<username><uname></username>
<password><pw></password>
</server>
</servers>
<profiles>
<profile>
<id>dev.id</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>repo.id</id>
<url>valid working url</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
what works great for all dependencies located there, but build fails whenever I declare dependencies which are located in the central repository. For instance:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
results in:
[ERROR] Failed to execute goal on project <name>: Could not resolve dependencies for project…
…Failed to read artifact descriptor for junit:junit-dep:jar:4.9.1-SNAPSHOT: Could not transfer artifact junit:junit-dep:pom:4.9.1-SNAPSHOT from/to <server.id>…
How can I make maven load dependencies correctly?

You are trying to use a -SNAPSHOT dependency which is not available in Maven Central.
junit:junit-dep:jar:4.9.1-SNAPSHOT: Could not transfer artifact junit:junit-dep:pom:4.9.1-SNAPSHOT fr
In Maven central there are only releases available.
Furthermore the above dependency is not coming into your build by the junit:junit:4.11:jar dependency it must be comming from other sources.

Related

Not retrieving jars from maven nexus setup specific to project

Got 2 Nexus maven repo - one serving or holding common jars - mostly from maven central & some others. And other - project specific maven nexus, where it holds 2 thirdparty jars which are needed for compilation of current project of interest.
Below is updates which is added to refer to local nexus maven setup & corresponding dependencies..
pom.xml snippet :
<project
...
<!-- download plugins from this *proj specific* repo -->
<repositories>
<repository>
<id>zzz-maven</id>
<name>zzz-maven</name>
<url>http://blah.blah.com/nexus/content/repositories/zzz-maven</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>zzz.zzz-report<groupId>
<artifactId>zzz-report<artifactId>
<version>1.2<version>
</dependency>
...
<!-- And other dependency to fetch jars from common nexus (which is working fine) -->
Added below to settings.xml (highlighted in bold text) - one covering Url to retrieve proj specific jars & other part of authenticating to proj specific Nexus
<settings>
<mirrors>
<mirror>
<id>nexus</id>
<name>Common nexus across org - Anonymous access </name>
<url>http://common-nexusxyz.com/nexus/content/repositories/maven</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
<profiles>
</profile>
<profile>
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<!--**Added this one** -->
</profile>
<profile>
<id>zzz-maven</id>
<repositories>
<repository>
<id>zzz-maven</id>
<name>zzz-maven</name>
<url>http://blah.blah.com/nexus/content/repositories/zzz-maven</url>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
<activeProfile>zzz-maven</activeProfile> <!--**activated** additional one here -->
</activeProfiles>
<servers>
<server>
<id>zzz-maven</id>
<username>userNameForZZZ</username> <!--**Specified** explicit password needed for proj specific maven nexus here -->
<password>passwordForZZZ</password>
</server>
</servers>
</settings>
But still throws out Warning & then the error for mvn install or mvn compile like :
[WARNING] The POM for zzz.zzz-report:zzz-report:jar:1.2 is missing, no dependency information available
Would like to know what is missing - so that it will download proj specific jars from proj specific maven nexus?
Tried with both <repositories> & <pluginRepositories> option in pom to consider for download
Expect it to connect to proj specific maven nexus & download dependency jar defined in pom
You have defined
<mirror>
<id>nexus</id>
<name>Common nexus across org - Anonymous access </name>
<url>http://common-nexusxyz.com/nexus/content/repositories/maven</url>
<mirrorOf>*</mirrorOf>
</mirror>
which means that all requests (due to mirrorOf having the value *) are redirected to that particular repository, no matter what other repositories are defined in the POM or the settings.xml.

Does the repos in maven not have jar files?

I have tried looking through various repos around, however i could not find one that contains the jar file for this dependency:
<dependency>
<groupId>com.oracle.java</groupId>
<artifactId>jre</artifactId>
<version>1.8.0_131</version>
</dependency>
Error i got: Could not resolve dependencies for project com.blackducksoftware.test:example-maven-travis:jar:0.1.0-SNAPSHOT: Could not find artifact com.oracle.java:jre:jar:1.8.0_131 in jboss (http://repository.jboss.org/nexus/content/groups/public-jboss)
Is there a reason why only pom files are in the repo and not the jar file?
How do I get the jar file?
From mvnrepository.com:
Note: this artifact it located at Alfresco repository (https://artifacts.alfresco.com/nexus/content/repositories/public/)
So you need to add a profile entry to your settings.xml file.
Example:
<profile>
<id>alfresco-repo</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>alfresco-repo</id>
<name>Alfresco Repository</name>
<url>https://artifacts.alfresco.com/nexus/content/repositories/public/</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
</profile>

How to build and release a Maven project from an offline machine (with a file directory as lib-repository)

I need to build and release projects using Jenkins,
on a server with no access to maven central, and even, with no access to Nexus.
Given that I have access to maven-central on dev machines,
to fill the maven local_repository, I could do
mvn dependency:resolve-plugins dependency:go-offline
to then copy the local_repository on the linux server.
Then, to get ride of a Non-resolvable parent POM error,
as described here, I filled the specific profiles for both windows (dev) and linux (jenkins) with faked central profile to override the maven-central reference made by my parent pom:
<profiles>
<profile>
<id>windows</id>
<activation>
<os>
<family>Windows</family>
</os>
</activation>
<properties> <repository.base.url>file:///c:/maven_distribution_repo/</repository.base.url>
</properties>
</profile>
<profile>
<id>linux</id>
<activation>
<os>
<family>Linux</family>
</os>
</activation>
<properties>
<repository.base.url>file:///appli/Maven_3.1.1_build/maven_distribution_repo/</repository.base.url>
</properties>
<repositories>
<repository>
<id>central</id>
<name>Maven Plugin Repository</name>
<!--<url>http://repo1.maven.org/maven2</url>-->
<url>${repository.base.url}</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Maven Plugin Repository</name>
<!--<url>http://repo1.maven.org/maven2</url>-->
<url>${repository.base.url}</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
That way, mvn -o compile still raise a Non-resolvable parent POM error !,
but using the --legacy-local-repository option suggested here ,
managed to fake the remote repository by using a local one,
and the Non-resolvable parent POM problem disappeared:
mvn --legacy-local-repository compile
Still, a strange error appeared (decribed here):
[ERROR] Failed to execute goal on project myProject: Could not resolve dependencies for project some.package:myProject:war:0.0.7-SNAPSHOT: Failed to collect dependencies at org.jxls:jxls-poi:jar:1.0.11 -> org.jxls:jxls:jar:[2.0.0,): org.jxls:jxls:jar:[2.0.0,) within specified range -> [Help 1]
But it was hiding an earlier warning:
[WARNING] Could not transfer metadata org.jxlsjxls/maven-metadata.xml from/to central (/path):/appli/Maven_3.1.1_build/maven_distribution_repo/org/jxls/jxls/maven-metadata-central.xml (access forbidden)
Using --legacy-local-repository, maven seems to use the distribution repository path as the local libs repository !
I swapped them in the pom:
<profile>
<id>linux</id>
<activation>
<os>
<family>Linux</family>
</os>
</activation>
<properties>
<!--<repository.base.url>file:///appli/Maven_3.1.1_build/maven_distribution_repo/</repository.base.url>-->
<repository.base.url>file:///appli/Maven-3.1.1_build/maven_local_repo/</repository.base.url>
</properties>
...
and had also to copy into the local repository:
all maven-metadata-maven2_central.xml into maven-metadata.xml
using the following bash command:
for file in $(find /appli/Maven-3.1.1_build/maven_local_repo -type f -name 'maven-metadata-maven2_central.xml'); do cp $file $(echo ${file%/*}/maven-metadata.xml); done
And ... BINGO !
BUILD SUCCESSFULL
Seems weird, at later stage, to release:perform into the local lib repository.
Would you have any better and not that painful solution ?
Maven deploy plugin would solve this problem.
mvn deploy -DaltDeploymentRepository=local-temp::default::file://directory/
More Exhaustive example:
# 1. Create temporary folder
tmp_repo=$(mktemp -d systemds-repo-tmp-XXXXX)
# 2. Create a simple settings.xml
cat <<EOF >../tmp-settings-nexus.xml
<settings>
<activeProfiles>
<activeProfile>local-temp</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>local-temp</id>
<repositories>
<repository>
<id>local-temp</id>
<url>${tmp_repo}</url>
</repository>
</repositories>
</profile>
</profiles>
</settings>
EOF
# 3. deploy to local
mvn --settings ../tmp-settings-nexus.xml -Pdistribution deploy \
-DaltDeploymentRepository=local-temp::default::file://${tmp_repo} \
-Daether.checksums.algorithms='SHA-512,SHA-1,MD5'
You should consider setting up a local repository (for example, Nexus) that will be accessible to your build machine.
This is a very common setup.
It enables you to build without internet connection to the build machine.

Issue pulling SNAPSHOT dependencies from Archiva snapshot repository

I've been setting up an Apache Archiva instance as both a proxy to Maven Central and to capture our development snapshots. I've managed to setup the proxy and I can deploy artifacts to the Archiva snapshot repository however I cannot pull artifacts from the snapshot repositories to use in other projects.
Relevant parts of pom.xml (dependant project)
<project>
<!-- Excluded detail -->
<dependency>
<groupId>uk.abc</groupId>
<artifactId>ABC</artifactId>
<version>1.0-SNAPSHOT</version>
<type>war</type>
</dependency>
<!-- Excluded detail -->
<distributionManagement>
<repository>
<id>archiva.snapshots</id>
<name>Snapshot Repository</name>
<url>https://xxx.xxx.xxx.xxx/archiva/repository/snapshots</url>
</repository>
</distributionManagement>
<!-- Excluded detail -->
</project>
My ~/.m2/settings.xml
<settings>
<servers>
<server>
<id>archiva.snapshots</id>
<username>username</username>
<password>xxx</password>
</server>
<server>
<id>archiva.internal</id>
<username>username</username>
<password>xxx</password>
</server>
</servers>
<mirrors>
<mirror>
<id>archiva.internal</id>
<mirrorOf>central</mirrorOf>
<url>https://xxx.xxx.xxx.xxx/archiva/repository/internal</url>
</mirror>
<mirror>
<id>archiva.snapshots</id>
<mirrorOf>snapshots</mirrorOf>
<url>https://xxx.xxx.xxx.xxx/archiva/repository/snapshots</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>archiva.internal</id>
<name>Archiva Managed Internal Repository</name>
<url>https://xxx.xxx.xxx.xxx/archiva/repository/internal/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>archiva.snapshots</id>
<name>Archiva Managed Internal Repository</name>
<url>https://xxx.xxx.xxx.xxx/archiva/repository/snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
</settings>
When I build the dependant project I cannot reference classes (public access).
Just to note that I can browse the snapshots repository and I know the war file is there.
Any ideas?
It turns out that you cannot use the "war" dependency type and expect to be able to reference the contained classes. You can however create an additional jar (create both war and jar) containing the classes:
http://maven.apache.org/plugins/maven-war-plugin/faq.html#attached
You can the use the type "jar" when pulling in the dependency... in my case:
<dependency>
<groupId>uk.abc</groupId>
<artifactId>ABC</artifactId>
<version>1.0-SNAPSHOT</version>
<type>jar</type>
</dependency>
I think therefore the question is a bit misleading... the dependency was being pulled from Archiva but was not of an accessible type.
You are probably not activating the profile correctly
before the profile in settings.xml put something like
<activeProfiles>
<activeProfile>default</activeProfile>
</activeProfiles>
Remember this about activeByDefault
This profile will automatically be active for all builds unless
another profile in the same POM is activated using one of the
previously described methods. All profiles that are active by default
are automatically deactivated when a profile in the POM is activated
on the command line or through its activation config.
if you want to confirm if this is the issue, look at the active profiles by running help:active-profiles

Unable to add repositories to local settings.xml

I'm having trouble getting Maven to download dependencies when I specify my repositories in my .m2/settings.xml file. However, Maven downloads these dependencies when I add the repository names to my pom.
Specifically, I am attempting to compile some hibernate example projects, and I've read in the instructions that I should add the following repositories to either my pom or settings.xml:
<repositories>
<repository>
<id>jboss-public-repository-group</id>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
<layout>default</layout>
<releases><enabled>true</enabled><updatePolicy>never</updatePolicy></releases>
<snapshots><enabled>true</enabled><updatePolicy>never</updatePolicy></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>jboss-public-repository-group</id>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
Everything works fine when I put the snippet in my project's pom.xml, but when I try putting it in settings.xml I receive this error:
The POM for org.hibernate:hibernate-core:jar:3.6.1.Final is missing, no dependency information available
Any ideas what I may be doing wrong?
You must have specified <repositories> and <pluginRepositories> within <profile> tag of settings.xml. Possibly this profile is not active. Ensure one of the below is present in your settings file.
<activeProfiles>
<activeProfile>myProfile</activeProfile>
</activeProfiles>
or
<profile>
<id>myProfile</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
...
</profile>

Resources