How to build maven project both Snapshot and Release build simultaneously - java

I have a requirement to build both Snapshot versions and Release build at a time.
To build snapshot versions I did like this
<groupId>com.abc.xyz</groupId>
<artifactId>service</artifactId>
<version>1.0.3-SNAPSHOT</version>
<distributionManagement>
<snapshotRepository>
<id>mvn-git-repo</id>
<uniqueVersion>true</uniqueVersion>
<name>maven git repository</name>
<url>file:/home/mvn-github-repo/snapshot</url>
</snapshotRepository>
</distributionManagement>
To build release versions,
<groupId>com.abc.xyz</groupId>
<artifactId>service</artifactId>
<version>1.0.3</version>
<distributionManagement>
<repository>
<id>mvn-git-repo</id>
<uniqueVersion>true</uniqueVersion>
<name>maven git repository</name>
<url>file:/home/mvn-github-repo/release</url>
</repository>
</distributionManagement>
Is it possible to make both together, so that it should generate snapshot version and as well release version jars at a time and deploy in respective repositories.If possible what is the version I should keep in pom ?

You use profile to define the version and repo, like this
<version>${buildVersion}</version>
.
.
.
<profiles>
<profile>
<id>snapshot-repo</id>
<activation>
<property>
<name>snapshot</name>
<value>true</value>
</property>
</activation>
<properties>
<buildVersion>1.0.3-SNAPSHOT</buildVersion>
</properties>
<distributionManagement>
<repository>
</repository>
<snapshotRepository>
</snapshotRepository>
</distributionManagement>
</profile>
<profile>
<id>git-repo</id>
<activation>
<property>
<name>!snapshot</name>
</property>
</activation>
<properties>
<buildVersion>1.0.3</buildVersion>
</properties>
<distributionManagement>
<repository>
</repository>
<snapshotRepository>
</snapshotRepository>
</distributionManagement>
</profile>
Then build by with -Dsnapshot=true

Related

Issue pulling package from Gitlab Package Registry Maven

I try to pull packages from Gitlab Package Registry, but get error:
Could not find group:package:0.0.18-SNAPSHOT.
This package is really exist in package registry
Can anybody help?
am i missing something?
I've tried with gradle:
build.gradle:
repositories {
mavenCentral(),
maven {
url "https://gitlab.com/api/v4/groups/<group-id>/-/packages/maven"
name "GitLab"
credentials(HttpHeaderCredentials) {
name = "<deploy-token-username>"
value = "deploy-token-value"
}
authentication {
header(HttpHeaderAuthentication)
}
}
}
dependencies {
implementation("<group>:<package>:0.0.18-SNAPSHOT")
}
And with maven:
pom.xml:
<repositories>
<repository>
<id>gitlab-maven</id>
<url>https://gitlab.com/api/v4/groups/<group-id>/-/packages/maven</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>gitlab-maven</id>
<url>https://gitlab.com/api/v4/projects/<project-id>/packages/maven</url>
</repository>
<snapshotRepository>
<id>gitlab-maven</id>
<url>https://gitlab.com/api/v4/projects/<project-id>/packages/maven</url>
</snapshotRepository>
</distributionManagement>
<dependencies>
<dependency>
<groupId>group</groupId>
<artifactId>package</artifactId>
<version>0.0.18-SNAPSHOT</version>
</dependency>
</dependencies>
.../.m2/settings.xml:
<settings>
<servers>
<server>
<id>gitlab-maven</id>
<configuration>
<httpHeaders>
<property>
<name>deploy-token-username</name>
<value>deploy-token-value</value>
</property>
</httpHeaders>
</configuration>
</server>
</servers>
</settings>

Maintaining different packages of different projects in a single repository

I would like to maintain all my java project's packages in a single repository. I'm using Github actions for this. I added the repository of the destination repo inside the pox.xml file.
<profiles>
<profile>
<id>github</id>
<activation>
<property>
<name>useGitHubPackages</name>
<value>true</value>
</property>
</activation>
<distributionManagement>
<repository>
<id>github</id>
<name>GitHub Packages</name>
<url>https://maven.pkg.github.com/******/test-platform</url>
</repository>
</distributionManagement>
</profile>
<profile>
<!-- We only need GitHub packages during releases as syncing to central is too slow. -->
<id>github-releases</id>
<repositories>
<repository>
<id>github</id>
<name>GitHub Packages</name>
<url>https://maven.pkg.github.com/*****/test-platform</url>
</repository>
</repositories>
</profile>
</profiles>
I'm also using the settings.xml file and added the Personal Access Token as secrets to the repository. The token has scope as repo and write:packages. But when publishing the package I'm getting the following error. Anyone can help with this?
Error: Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project demoproject: Failed to deploy artifacts: Could not find artifact com.example:demoproject:jar:3.0.1 in github (https://maven.pkg.github.com/*****/test-platform) -> [Help 1]
I would like to maintain the demoproject package inside the test-platform repository.
Thankyou.

Maven deploy to 2 different remote repositories

I have some java projects which used maven build and upload artifact to nexus. It's working well. Now there are some changes in my company process so I need to upload artifact to 2 remote repository:
Nexus
Gitlab package registry
I try to define multiple profiles in pom.xml but it also pick 1 profile to deploy. Anyone have exp about this, please advise me
<profiles>
<profile>
<id>profile-1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>profile-1-repo</id>
...
</repository>
</repositories>
</profile>
<profile>
<id>profile-2</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>profile-2-repo</id>
...
</repository>
</repositories>
</profile>
...
mvn deploy -P profile-1,profile-2...

Deployment failed: repository element was not specified in the POM inside distributionManagement element [duplicate]

I am trying to run the command, mvn release:perform, but I get this error:
Failed to execute goal
org.apache.maven.plugins:maven-deploy-plugin:2.5:deploy
(default-deploy) on project git-demo:
Deployment failed: repository element
was not specified in the POM inside
distributionManagement element or in
-DaltDeploymentRepository=id::layout::url
parameter
Here's my pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sonatype.blog</groupId>
<artifactId>git-demo</artifactId>
<packaging>jar</packaging>
<version>1.1-SNAPSHOT</version>
<name>git-demo</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<scm>
<connection>scm:git:git#github.com:Christian-Achilli-KP/git-demo.git</connection>
<url>scm:git:git#github.com:Christian-Achilli-KP/git-demo.git</url>
<developerConnection>scm:git:git#github.com:Christian-Achilli-KP/git-demo.git</developerConnection>
</scm>
<distributionManagement>
<!-- use the following if you're not using a snapshot version. -->
<repository>
<id>localSnap</id>
<name>RepositoryProxyRel</name>
<url>http://127.0.0.1:8080/nexus/content/repositories/releases/</url>
</repository>
<!-- use the following if you ARE using a snapshot version. -->
<snapshotRepository>
<id>MylocalSnap</id>
<name>RepositoryProxySnap</name>
<url>http://127.0.0.1:8080/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.1</version>
</plugin>
</plugins>
</build>
</project>
Actually I can see the
repository
declaration inside the
distributionManagent
tag.
Here's my settings.xml:
<settings>
<servers>
<server>
<id>localSnap</id>
<username>deployment</username>
<password>****</password>
</server>
<server>
<id>MylocalSnap</id>
<username>deployment</username>
<password>****</password>
</server>
<server>
<id>myserver</id>
<username>tomcat</username>
<password>tomcat</password>
</server>
</servers>
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://127.0.0.1:8080/nexus/content/groups/public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<properties>
<project.build.sourceEncoding>MacRoman</project.build.sourceEncoding>
<project.reporting.outputEncoding>MacRoman</project.reporting.outputEncoding>
</properties>
<!--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>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
Any advice why it complains?
Review the pom.xml file inside of target/checkout/. Chances are, the pom.xml in your trunk or master branch does not have the distributionManagement tag.
I got the same message ("repository element was not specified in the POM inside distributionManagement element"). I checked /target/checkout/pom.xml and as per another answer and it really lacked <distributionManagement>.
It turned out that the problem was that <distributionManagement> was missing in pom.xml in my master branch (using git).
After cleaning up (mvn release:rollback, mvn clean, mvn release:clean, git tag -d v1.0.0) I run mvn release again and it worked.
You can also override the deployment repository on the command line:
-Darguments=-DaltDeploymentRepository=myreposid::default::http://my/url/releases
The ID of the two repos are both localSnap; that's probably not what you want and it might confuse Maven.
If that's not it: There might be more repository elements in your POM. Search the output of mvn help:effective-pom for repository to make sure the number and place of them is what you expect.
For me, this was something as simple as a missing version for my artifact - "1.1-SNAPSHOT"

Maven Settings for multiple repositories

I have the following in settings.xml
<mirrors>
<mirror>
<id>paid-jars</id>
<name>jars with license</name>
<url>http://url:8081/nexus/content/repositories/paidjars/</url>
<mirrorOf>!central</mirrorOf>
</mirror>
<mirror>
<id>Org-central</id>
<name>mirror of central</name>
<url>http://url:8081/nexus/content/repositories/central/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
In pom.xml I have two jars
apache-commons.jar (which I assumes to be downloaded from central)
licensed.jar (which I assume to be downloaded from paid-jars)
But when I run maven clean install it tries to download licensed.jar from Org-central.
How can I make it use paid-jars to download? Is it possible first it goes to Org-central and if fails it tries at paid-jars? If so, how? I don't want to put repo entries in pom.xml
Settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<proxies>
<proxy>
<id>Proxy</id>
<active>true</active>
<protocol>http</protocol>
<username>username</username>
<password>******</password>
<host>host.url</host>
<port>8080</port>
<nonProxyHosts>local.net|internal.com</nonProxyHosts>
</proxy>
</proxies>
<mirrors>
<mirror>
<id>paid-jars</id>
<name>jars with license</name>
<url>http://url:8081/nexus/content/repositories/paidjars/</url>
<mirrorOf>!central</mirrorOf>
</mirror>
<mirror>
<id>Org-central</id>
<name>mirror of central</name>
<url>http://url:8081/nexus/content/repositories/central/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<id>compiler</id>
<properties>
<JAVA_1_7_HOME>C:\Program Files (x86)\Java\jdk1.7.0_51\bin</JAVA_1_7_HOME>
</properties>
</profile>
</profiles>
</settings>
you have to setup mirror
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://internal/nexus/content/repositories/thirdparty</url>
</mirror>
<mirror>
<id>google</id>
<mirrorOf>google</mirrorOf>
<url>http://google-maven-repository.googlecode.com/svn/repository</url>
</mirror>
then add internal & external repo
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>central</id>
<name>central</name>
<url>http://internal/nexus/content/repositories/thirdparty</url>
</repository>
<repository>
<id>google</id>
<name>google</name>
<url>http://google-maven-repository.googlecode.com/svn/repository</url>
</repository>
</repositories>
</profile>
If you need to distinguish between an internal (Nexus, Artifactory, ..) repository and Maven central, the already discussed solutions here based on multiple Mirrors using the <mirrorOf> tags capabilities (as described in the official docs also ) work fine (the same with this so Q&A ).
BUT in our case, where we wanted some libraries to be downloaded from internal Nexus 1 - and others (with the same package names, but different versions) from internal Nexus 2, those solutions didn't work. We simply couldn't use the <mirrorOf> tag.
We found another solution based on multiple <repository> definitions INSIDE of an ever activated Maven profile (it didn't work without the profile definition!!). Here's our settings.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- The resolution of multiple Repositories only works with profiles!-->
<profiles>
<profile>
<id>use-multiple-repos</id>
<!--Request multiple Repositories for dependencies -->
<repositories>
<repository>
<id>nexus-repository</id>
<name>Internal Nexus Repository 1 https://nexus.your.lan</name>
<url>https://nexus.your.lan/repository/maven-public/</url>
</repository>
<repository>
<id>nexus-repository-2</id>
<name>Internal Nexus Repository 2 https://nexus2.completely.other.net</name>
<url>https://nexus2.completely.other.net/repository/maven-public/</url>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<!--make the profile active all the time -->
<activeProfile>use-multiple-repos</activeProfile>
</activeProfiles>
</settings>
If you're looking for a full-blown settings.xml, where - besides the multiple repositories - also a corporate proxy is defined alongside with the credentials for maven-releases and maven-snapshots users to push to the corporate Nexus 1, have a look at this gist.
And if you want to check fast, if you're configuration is working, you could simply use maven-dependency-plugin's dependency:get:
mvn dependency:get \
-DgroupId=your.package.name \
-DartifactId=yourArtifactId \
-Dversion=YOURVERSION \
-Dpackaging=jar
If this results in a BUILD SUCCESS, where a minimum of one dependency from Nexus 1 and one of Nexus 2 could be downloaded, everything should work as expected and looks somehow like this (omitted lot's of packages!):
[INFO] Resolving your.first.package:artifact1.jar:1.1.0
Downloaded from nexus-repository: https://nexus.your.lan/repository/maven-public/your/first/package/artifact/1.1.0/artifact2.jar (575 kB at 868 kB/s)
[INFO] Resolving your.second.package:artifact2.jar:1.1.0
Downloading from nexus-repository-2: https://nexus2.completely.other.net/repository/maven-public/your/second/package/artifact2/1.1.0/artifact2.jar (14 kB at 305 kB/s)
In settings.xml, defining mirror with id and url for the repository besides using the same in profile worked for me, as below:
<mirrors>
<mirror>
<id>Artifactory</id>
<url>http://localhost:4900/archiva/repository/</url>
<mirrorOf>artifactory</mirrorOf>
</mirror>
<mirror>
<id>MavenCentral</id>
<url>https://repo.maven.apache.org/maven2</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<id>Project</id>
<properties>
<framework.version>1.0.9</framework.version>
<maven.test.skip>false</maven.test.skip>
<maven.test.failure.ignore>false</maven.test.failure.ignore>
<maven.javadoc.skip>false</maven.javadoc.skip>
<source.jdkversion>1.8</source.jdkversion>
<target.jdkversion>1.8</target.jdkversion>
</properties>
<repositories>
<repository>
<id>Artifactory</id>
<name>Maven Artifactory for Project</name>
<url>http://localhost:4900/archiva/repository/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
<repository>
<id>MavenCentral</id>
<url>https://repo.maven.apache.org/maven2/</url>
</repository>
</repositories>
</profile>
</profiles>
It's impossible to specify a dedicated repository to look up an artifact. Maven will look up all configured repositories one by one until the artifact is found. Just add both the central mirror and internal repository to the settings.xml and it will be okay.
Read Maven guide to setup multiple repositories. In respect to the order of repositories, see this answer.
In the settings.xml, you need to make 3 changes
(Replace https://repository.internal/mvn-public/ with your org internal repo url)
A) Add the mirrors as below
<mirror>
<mirrorOf>artifact-int</mirrorOf>
<url>https://repository.internal/mvn-public/</url>
<id>artifact-int-mirror</id>
<name>internal Repository</name>
</mirror>
<mirror>
<mirrorOf>artifact-web</mirrorOf>
<url>https://repo1.maven.org/maven2/</url>
<id>artifact-web-mirror</id>
<name>Amadeus Repository</name>
</mirror>
B) Update the profile section as below
<profiles>
<profile>
<id>artifact.repository.int</id>
<repositories>
<repository>
<id>artifact-int</id>
<name>Internal Artifactory</name>
<url>https://repository.internal/mvn-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>artifact-int</id>
<name>Int Artifactory</name>
<url>https://repository.internal/mvn-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>artifact.repository.web</id>
<repositories>
<repository>
<id>artifact-web</id>
<name>Web Artifactory</name>
<url>https://repo1.maven.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>artifact-web</id>
<name>Web Artifactory</name>
<url>https://repo1.maven.org/maven2/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
C) Set the active profile as below
<activeProfiles>
<activeProfile>artifact.repository.int</activeProfile>
<activeProfile>artifact.repository.web</activeProfile>
</activeProfiles>
Define (duplicate) authentication (server) entries in settings.xml,
<servers>
<server>
<id>auth-default</id>
<username>${env.ARTIFACTORY_USER}</username>
<password>${env.ARTIFACTORY_PASS}</password>
</server>
<server>
<id>auth-libs-release-art</id>
<username>${env.ARTIFACTORY_USER}</username>
<password>${env.ARTIFACTORY_PASS}</password>
</server>
[...]
</servers>
Define respective mirrors with a special "catch-the-rest" in mirrorOf for the main repository and references to "repository" IDs in the other mirrorOf entries.
<mirrors>
<mirror>
<id>auth-default</id>
<url>https://artifactory.COMPANY.net/artifactory/repo.maven.apache.org</url>
<mirrorOf>*,!libs-release,[...]</mirrorOf>
</mirror>
<mirror>
<id>auth-libs-release-art</id>
<url>https://artifactory.COMPANY.net/artifactory/libs-release</url>
<mirrorOf>libs-release</mirrorOf>
</mirror>
[...]
</mirrors>
Define repositories with "example" URLs (the URLs will be ignored because of using the above mirrors; reusing the server IDs in the repository IDs and placing the real URLs directly in the repository entries will prevent from using the indirection via mirror definitions).
<profiles>
<profile>
<id>libs-art</id>
<repositories>
<repository>
<id>libs-release</id>
<releases><enabled>true</enabled><updatePolicy>never</updatePolicy></releases>
<url>https://example.test/libs-release</url>
</repository>
<repository>
<id>libs-snapshot</id>
<snapshots />
<url>https://example.test/libs-snapshot</url>
</repository>
[...]
</profile>
</profiles>
<activeProfiles>
<activeProfile>libs-art</activeProfile>
</activeProfiles>

Categories