Maven deploy using profile - maven

I want to deploy to a private Maven repository only if there is a defined profile.
Is this possible? And how can I do it?

put the repository into profile
<profiles>
<profile>
<id>test</id>
<distributionManagement>
<repository>
<id>repsy</id>
<name>My Private Maven Repository on Repsy</name>
<url>https://repo.repsy.io/mvn/username/reponame</url>
</repository>
</distributionManagement>
</profile>
</profiles>
you can execute :mvn deploy -P test,it will use the repo in the test profile.

Maybe you can use the skip property described here: https://maven.apache.org/plugins/maven-deploy-plugin/deploy-mojo.html#skip
mvn deploy -Dmaven.deploy.skip=true

Related

Organisation-Wide repositories with GitHub Packages & Maven

If I have a GitHub organisation ACME, and we have two repositories Repository1 and Repository2 which both publish packages to GitHub packages via Maven, is there a way to have both packages be added in a single Maven repository, as an organisation owner?
For instance,
<!-- settings.xml -->
<profiles>
<profile>
<id>github</id>
<repositories>
<repository>
<id>github</id>
<name>GitHub OWNER Apache Maven Packages</name>
<url>https://maven.pkg.github.com/ACME</url>
</repository>
</repositories>
</profile>
</profiles>

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.

Copy snapshot artifact from nexus without pom

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

Configure Maven settings for deployment

I want to deploy artifacts to Nexus from Jenkins to different repositories (like builds-all, builds-verified, releases). The thing is that I want to keep minimal configuration in the project POM file. My settings file now looks like:
<servers>
<server>
<id>orion-nexus</id>
<username>admin</username>
<password>password</password>
</server>
</servers>
<localRepository>~/.m2/repository</localRepository>
<profiles>
<!-- Deployment configuration for CI builds for mainline -->
<profile>
<id>build</id>
<repositories>
<repository>
<id>builds-all</id>
<url>http://orion-nexus:8081/</url>
<snapshots>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
Project POM:
<distributionManagement>
<repository>
<id>orion-nexus</id>
<layout>default</layout>
<url><!-- how to avoid explicit URL? --></url>
</repository>
</distributionManagement>
I wan to run deploy like mvn -B -P build clean install deploy. However, I don't understand how to avoid setting explicit URL in distribution management section. Can I set a variable in settings file and propagate it to my POM?
Is there any step-by-step guide for such workflow?
You can declare a property inside a profile on your settings.xml and use its name within <distributionManagement/> configuration.
settings.xml
<profiles>
<profile>
<id>distmgt</id>
<properties>
<distUrl>scp://...</distUrl>
<properties/>
</profile>
</profiles>
pom.xml
<distributionManagement>
<repository>
<id>orion-nexus</id>
<layout>default</layout>
<url>${distUrl}</url>
</repository>
</distributionManagement>
And finally
mvn -P distmgt clean deploy
You can avoid the -P build params using activation.

Using profiles in maven

I have added below profile to my pom.xml :
<profiles>
<profile>
<id>nexus</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<distributionManagement>
<repository>
<id>nexusid1</id>
<url>http://</url>
</repository>
<snapshotRepository>
<id>nexusid2</id>
<url>http://</url>
</snapshotRepository>
</distributionManagement>
</profile>
</profiles>
I have added to settings.xml :
<server>
<id>nexusid1</id>
<username>username</username>
<password>passwword</password>
</server>
To add the project to the Nexus repo I use mvn deploy
Do I need to use a profile in this case ?
If I want to deploy to nexusid2 does this mean I need to add a new server entry to settings.xml even if the username/password for nexusid1 & nexusid2 are the same ?
According to this page, there is a -DaltDeploymentRepository argument for mvn:deploy. But imho, profiles would be the more elegant solution here, cause you don't need to remember the server id but the profile name.
And yes, you need to add a new server to the settings.xml, even if username and password are equal.
Note besides: Password encryption for server management

Resources