Copy snapshot artifact from nexus without pom - maven

I want to download a snapshot artifact using the dependecy.copy target. I don't want to have a POM file.
mvn -U dependency:copy -Dartifact=mygroupId:myArtifactId:myversion-SNAPSHOT:jar
Unfortunately this only works if the artifact is already in the local maven repo cache. When it's not in the maven cache I get the following error:
Unable to find artifact.
...
foo-public (https://nexus.foo.org/content/groups/public-foo/, releases=true, snapshots=false)
It says foo-public because I'm using a settings.xml
<mirror>
<id>foo</id>
<mirrorOf>*</mirrorOf>
<name>My Maven Nexus Repository</name>
<url>http://nexus.foo.org/content/groups/public-foo/</url>
</mirror>
The reason seems to be that Maven's Super POM has set snapshots=false for the central repo. If I add a minimalistic pom.xml to the working directory I don't have the error as snapshots=true seems to be the default for any other repo.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>dummy</groupId>
<artifactId>dummy</artifactId>
<version>1</version>
<repositories>
<repository>
<id>dummy</id>
<url>dummy</url>
</repository>
</repositories>
</project>
My current work around is to write the dummy POM before calling my mvn command. Another possible work around is to add the following to settings.xml (Found in Sonatype nexus book)
<profiles>
<profile>
<id>nexus</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<!--make the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
Do you have a more elegant idea that works without so much preparation? For instance a command line switch?

One solution is to use dependency:get goal instead of dependeny:copy goal, see Apache Maven Dependency Plugin:
The dependency:get Mojo
This mojo is used to fetch an artifact and (optionally) its dependencies from remote repositories using its Maven coordinates.
mvn dependency:get -DgroupId=org.apache.maven -DartifactId=maven-core -Dversion=2.2.1 -Dpackaging=jar -Dclassifier=sources -DremoteRepositories=central::default::http://repo1.maven.apache.org/maven2,myrepo::::http://myrepo.com/maven2
mvn dependency:get -DgroupId=org.apache.maven -DartifactId=maven-core -Dversion=2.2.1 -Dpackaging=jar -Dclassifier=sources -DremoteRepositories=http://repo1.maven.apache.org/maven2
mvn dependency:get -Dartifact=org.apache.maven:maven-core:2.2.1:jar:sources -DremoteRepositories=http://repo1.maven.apache.org/maven2 -Ddest=/tmp/myfile.jar
Your modified command:
mvn dependency:get -U -DgroupId=mygroupId -DartifactId=myArtifactId -Dversion=myversion-SNAPSHOT -Dpackaging=jar -Dtransitive=false -s settings.xml -DremoteRepositories=https://nexus.foo.org/content/groups/public-foo/ -Ddest=target/myArtifactId.jar

Related

Github action push java artifact to github repo

I have a Github repo project using GitHub actions with a docker file used to build the SpringBoot Java project.
I want to download packages from the Github repo for custom artifacts from GitHub repo and also be able upload artifact to it.
So I followed the link Configuring Apache Maven for use with GitHub Packages by adding the section to a settings.xml file:
<activeProfiles>
<activeProfile>github</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>github</id>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>github</id>
<name>GitHub OWNER Apache Maven Packages</name>
<url>https://maven.pkg.github.com/OWNER/REPOSITORY</url>
</repository>
</repositories>
</profile>
</profiles>
<servers>
<server>
<id>github</id>
<username>USERNAME</username>
<password>${GITHUB_TOKEN}</password>
</server>
</servers>
</settings>
To publish the package from the dockerfile build I added the following to my pom.xml:
<distributionManagement>
<repository>
<id>github</id>
<name>GitHub OWNER Apache Maven Packages</name>
<url>https://maven.pkg.github.com/OWNER/REPOSITORY</url>
</repository>
</distributionManagement>
The following is the content of my dockerfile:
FROM adoptopenjdk/maven-openjdk10 as build
WORKDIR /app
ADD pom.xml /app/pom.xml
ADD src /app/src
ADD settings.xml /root/.m2/settings.xml
RUN ["mvn", "clean", "install", "deploy"]
Is it possible to deploy from the dockerfile to Github repo? Somehow the deploy piece does not seem to work. I have tried few times but not sure what's wrong with my sections.
Currently this is my error in my docker build:
Could not transfer metadata
com.chg.sa:demo-sa-java-service:1.0-SNAPSHOT/maven-metadata.xml
from/to github (https://maven.pkg.github.com/OWNER/REPOSITORY): Not
authorized -> [Help 1]
I got the push working using Dockerfile building by passing in the github token as build arg and switching the owner and repo name to their values.

maven-dependency-plugin dependency:get with AAR dependencies

I would like to fetch a maven package with all dependencies. To perform this, I use this command:
mvn org.apache.maven.plugins:maven-dependency-plugin:3.1.1:get \
--settings settings.xml \
-Dmaven.repo.local=/path/to/m2-repository \
-Dartifact=my.pkg.name:vers
My settings.xml file :
<settings>
<profiles>
<profile>
<id>global</id>
<repositories>
<repository>
<id>google</id>
<name>Google Maven</name>
<url>https://maven.google.com</url>
</repository>
<repository>
<id>jcenter</id>
<name>Jcenter</name>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>global</activeProfile>
</activeProfiles>
</settings>
It's works fine if there are no aar packages in the dependency tree, otherwise, for each aar packages, I have the error like :
The following artifacts could not be resolved: <pkg-prefix>:<pkg-name>:jar:<pkg-version>
There is a plug-in to manage aar and other android archive formats (com.simpligility.maven.plugins:android-maven-plugin) but I don't know how I will have to use it to perform the same thing that with maven-dependency-plugin with the get method

Maven multiple repositories - jcenter, nexus

I am trying to set up maven with 2 repositories - jcenter & nexus. From nexus, we don't have the ability to proxy to jcenter. Hence I need a set up which can do the following-
If a artifact is found in nexus, download it.
If not found, go to jcenter to download artifact.
With the below settings.xml, it seems like maven only tries to download from central (which is set to bintray here, because its a super set of maven central). How can I tell maven to look in central and nexus?
Note- I have tried <mirrorOf>central, !nexus</mirrorOf>
<mirrors>
<mirror>
<id>bintray</id>
<name>bintray</name>
<url>http://jcenter.bintray.com</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>nexus</id>
<mirrorOf>!bintray, *</mirrorOf>
<url>http://some/url/goes/here</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>allow-snapshots</id>
<activation><activeByDefault>true</activeByDefault></activation>
<repositories>
<repository>
<id>jcenter</id>
<url>http://jcenter.bintray.com</url>
</repository>
</repositories>
</profile>
</profiles>
I got it working by specifying multiple repo's in my parent pom. multiple repos in settings.xml had no effect on maven.

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.

Order of maven repositories in settings.xml

I have a settings.xml which looks like 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">
<servers>
<server>
<id>nReleases</id>
<username>test</username>
<password>test123</password>
</server>
<server>
<id>nSnapshots</id>
<username>test</username>
<password>test123</password>
</server>
</servers>
<profiles>
<profile>
<id>space</id>
<repositories>
<repository>
<id>central</id>
<url>http://repo1.maven.org/maven2</url>
</repository>
<repository>
<id>nReleases</id>
<name>Releases</name>
<url>http://someserver/repository/maven-releases/</url>
</repository>
<repository>
<id>nSnapshots</id>
<name>Snapshots</name>
<url>http://someserver/repository/maven-snapshots/</url>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>space</activeProfile>
</activeProfiles>
</settings>
I want maven to check repo1.maven.org first and then only look in someserver. Can someone let me know how this can be achieved?
I am using Maven 3.3.9.
I have gone through this question and tried to change the order in which repos are declared in profile but it did not help.
<profile>
<id>space</id>
<repositories>
<repository>
<id>nReleases</id>
<name>Releases</name>
<url>http://someserver/repository/maven-releases/</url>
</repository>
<repository>
<id>nSnapshots</id>
<name>Snapshots</name>
<url>http://someserver/repository/maven-snapshots/</url>
</repository>
<repository>
<id>central</id>
<url>http://repo1.maven.org/maven2</url>
</repository>
</repositories>
</profile>
Logs while doing mvn clean install
tuk-MacBook-Pro-4:camel tuk$ mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building camel 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://someserver/repository/maven-releases/org/apache/camel/camel-redis/2.16.3/camel-redis-2.16.3.pom
Downloading: http://someserver/repository/maven-snapshots/org/apache/camel/camel-redis/2.16.3/camel-redis-2.16.3.pom
The order of the repository inside the settings was not clearly specified in Maven 2, but starting with Maven 3 (and the fix of of MNG-4400), the repositories are always tried in their declaration order in the settings.
What can happen, and what is likely the cause of your problem, is that Maven tries a repository, fails in doing so, and stores in your local repository the fact that it tried and failed. This results in the creation of .lastUpdated files in your local repository, storing this information. The consequence is that Maven will not re-try to download the dependency from a repository where it knows the download failed in the past. Thus, when you start a command and the project requires an artifact not present in your local repository, Maven will still try the repositories in their order of declaration, but it will skip the ones it knows already failed.
But you can force it to bypass this mechanism by passing the -U flag on the command line. It forces Maven to update the releases and snapshots dependencies, without looking into .lastUpdated files. This way, it will re-try every active remote repositories.

Resources