How to set deployment repository in pom.xml - java

everybody!
When I deploy my artefact, I run
clean deploy -DaltDeploymentRepository=releases::default::http://nexus.******.com/nexus/content/repositories/releases
What should I put into pom.xml to be able to run this command from Idea without creating custom command on every maschine?

From: https://maven.apache.org/plugins/maven-deploy-plugin/usage.html
<distributionManagement>
<repository>
<id>internal.repo</id>
<name>MyCo Internal Repository</name>
<url>Host to Company Repository</url>
</repository>
</distributionManagement>
Here you can find description of parameters you pass:
http://maven.apache.org/plugins/maven-deploy-plugin/deploy-mojo.html
Authentication you should store in your settings.xml file:
<server>
<id>internal.repo</id>
<username>maven</username>
<password>foobar</password>
</server>

Related

Maven settings.xml - upload same build jar files to multiple maven repositories

I'm looking to use mvn deploy to deploy all of the same jar file build artifacts to different maven repositories. Basically, I'm looking to make duplicate copies in multiple places. I'm trying to do this because I'm trying to migrate from one repository to another and I want to push to both places as a transitional step. (And I can't mirror the repositories as a whole.)
My (probably flawed) understanding is that I'd setup multiple "repositories" and "servers" in my settings.xml.
from settings.xml
<servers>
<server>
<id>${env.MAVEN_SERVER_ID_1</id>
<username>${env.MAVEN_SERVER_USERNAME_1}</username>
<password>${env.MAVEN_SERVER_PASSWORD_1}</password>
</server>
<server>
<id>${env.MAVEN_SERVER_ID_2</id>
<username>${env.MAVEN_SERVER_USERNAME_2}</username>
<password>${env.MAVEN_SERVER_PASSWORD_2}</password>
</server>
</servers>
<profiles>
<profile>
<id>my-profile</id>
<repositories>
<repository>
<id>${env.MAVEN_SERVER_ID_1}</id>
<name>${env.MAVEN_SERVER_NAME_1}</name>
<url>${env.MAVEN_REPOSITORY_URL_1}</url>
<layout>default</layout>
</repository>
<repository>
<id>${env.MAVEN_SERVER_ID_2}</id>
<name>${env.MAVEN_SERVER_NAME_2}</name>
<url>${env.MAVEN_REPOSITORY_URL_2}</url>
<layout>default</layout>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>my-profile</activeProfile>
</activeProfiles>
Then, when deploying, this is run.
mvn --settings=/path/to/settings.xml \
deploy \
-DaltDeploymentRepository="${MAVEN_SERVER_ID_1}::default::${MAVEN_REPOSITORY_URL_1}" \
/path/to/project/
This only pushes to one of the 2 repositories, but how do I change it so the deploy will push to both repositories at the same time? This maven deploy is something that is currently automated, so I'm trying to update the automation to deploy to both places.
I'm pretty new to maven, so maybe this is an obvious answer, but I'd be very appreciative of any help.

Add GitLab private repository as Maven dependency

I have a private repository in GitLab (it's in a group, and I have a Developer role) which I want to add as a dependency for my Maven project.
I've been looking for a while and all I found was that I have to add the following to my pom.xml:
<repositories>
<repository>
<id>gitlab-maven</id>
<url>https://gitlab.com/api/v4/projects/.../packages/maven</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>gitlab-maven</id>
<url>https://gitlab.com/api/v4/projects/.../packages/maven</url>
</repository>
<snapshotRepository>
<id>gitlab-maven</id>
<url>https://gitlab.com/api/v4/projects/.../packages/maven</url>
</snapshotRepository>
</distributionManagement>
However, I have no idea how to add the dependency itself (with the groupId and stuff) and I'm not sure how to do authentication to allow Maven to download the project. I've seen something about Personal Access Tokens in GitLab, but I'm not sure how to set that up (I assume I'd only need read-only access?).
First, a few pre-requisites. Since you mentioned you're using a private repository, you'll need to have at least GitLab Silver (hosted on gitlab.com) or GitLab Premium (self-hosted) in order to use the GitLab Maven Packages repository. Also, if you're self-hosted, you'll need to be on GitLab 11.3 or later (should be out this time next week), and have packages_enabled set to true (see Enabling the Packages repository).
For private projects, you'll need a Personal Access Token. The token should have the api scope for Maven to upload artifacts to GitLab. Once you have the token, you configure your settings.xml like this.
<settings>
<servers>
<server>
<id>gitlab-maven</id>
<configuration>
<httpHeaders>
<property>
<name>Private-Token</name>
<value>REPLACE_WITH_YOUR_PERSONAL_ACCESS_TOKEN</value>
</property>
</httpHeaders>
</configuration>
</server>
</servers>
</settings>
In pom.xml, where you have the ellipses, you need to fill in your project ID. To find the ID, just visit the front page of your project on GitLab. It's shown near the top of the pages, just after the name and description of your project. For example, take a look at the mvn-example sample project. Its project ID is 8377576. That goes in the URL.
<repositories>
<repository>
<id>gitlab-maven</id>
<url>https://gitlab.com/api/v4/projects/8377576/packages/maven</url>
</repository>
</repositories>
The mvn-example project's pom.xml file shows a completed example.
With all that set up, you should be able to upload artifacts with mvn deploy.
First of all you should go to your projects "Packages and Registries" it is on the left menu of your Gitlab user interface and find there package registry button(if you can't see it just ask your system administrator) and just leave that tab open and wait for future instructions.
Be aware that if you work in a company that has Gitlab domain, everywhere that I write "https://gitlab.com", you should write your company's gitlab domain.
After that you should generate your "Private-Token" by going to "https://gitlab.com/profile" -> "access tokens" and select api check box and give a name to your token for example "test token" and then press generate.
After that in your java project create file "settings.xml" near your pom.xml and paste there following piece of code that is below and write there your token that you just generated. This settings.xml is required both for uploading and installing artifact.
<settings>
<servers>
<server>
<id>gitlab-maven</id>
<configuration>
<httpHeaders>
<property>
<name>Private-Token</name>
<value>your token</value>
</property>
</httpHeaders>
</configuration>
</server>
</servers>
</settings>
After that go to Gitlab's user inreface and copy your project's id from there. See screenshot:
After that paste following code that is below in your pom.xml. This must be done in the project that should be uploaded in Gitlab's "package registry"
<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>
For uploading artifact open terminal in your ide and paste following command:
mvn deploy -s settings.xml
After that go to "package registry" of your project in Gitlab user interface and see there uploaded artifact.
For installing the settings.xml also needed, and also paste following piece of code that is below in your pom.xml
<repositories>
<repository>
<id>gitlab-maven</id>
<url>https://gitlab.com/api/v4/projects/project_id/packages/maven</url>
</repository>
</repositories>
And in your terminal call: mvn install -s settings.xml or mvn dependency:get -Dartifact={groupId}:{artifactId}:{version}
If there is an error while installing, don't worry, got to your local computer's .m2 folder, find that folder with containing new created artifact(jar), delete it and then go and call the same terminal command once again.
To use your private repository as maven dependency go to your package Registry and click on the package(com/example/demo)
you will see the instruction to add the dependency.
You need to add tag with correct dependency and and tag.
if you will mention private repository url in both and , it will pull and push the artefact from the private repository.
If you want to use the private repository to only download the dependency and push your artifact to the current project registry, then you need to add the and tag of the private repository and tag will the current project one.
Scenario 1:-
<dependency>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>gitlab-maven</id>
<url>https://gitlab.com/api/v4/projects/36104875/packages/maven</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>gitlab-maven</id>
<url>https://gitlab.com/api/v4/projects/36104875/packages/maven</url>
</repository>
<snapshotRepository>
<id>gitlab-maven</id>
<url>https://gitlab.com/api/v4/projects/36104875/packages/maven</url>
</snapshotRepository>
</distributionManagement>
2nd scenario:-
<dependency>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>gitlab-maven</id>
<url>https://gitlab.com/api/v4/projects/36104875/packages/maven</url>
</repository>
</repositories>
<distributionManagement>
<snapshotRepository>
<id>gitlab-maven</id>
<url>${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/maven</url>
</snapshotRepository>
<repository>
<id>gitlab-maven</id>
<url>${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/maven</url>
</repository>
</distributionManagement>

Maven Auth error with Artifactory

I'm trying to install artifactory in my local computer to manage all my project jars.
I've set up artifactory so it has two users: admin and user, without allowing anonymous login.
Here's my settings.xml:
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<servers>
<server>
<id>central</id>
<username>user</username>
<password>{encrypted-pssw}</password>
</server>
<server>
<id>snapshot</id>
<username>user</username>
<password>{encrypted-pssw}</password>
</server>
</servers>
<mirrors>
<mirror>
<mirrorOf>*</mirrorOf>
<name>remote-repos</name>
<url>http://localhost:8081/artifactory/remote-repos</url>
<id>remote-repos</id>
</mirror>
</mirrors>
<profiles>
<profile>
<id>central</id>
<repositories>
<repository>
<id>artifactory</id>
<url>http://localhost:8081/artifactory/repo</url>
<layout>default</layout>
<name>Artifactory default repo</name>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>central</activeProfile>
</activeProfiles>
"{encrypted-pssw}" is the passord obtained from encrypting (mvn -ep) the encrypted password from artifactory.
When I try to install ("mvn clean install") or deploy ("mvn deploy") every project it gives me this error:
Could not transfer artifact org.apache.maven.plugins:maven-clean-plugin:pom:2.5 from/to remote-repos (http://localhost:8081/artifactory/remote-repos): Not authorized , ReasonPhrase: Unauthorized. -> [Help 1]
I've tryied the rsa authentication too without any success
EDIT: more informations:
If I try to login with the broswer in the repository it asks for username and password, if I put them (using the encrypted password from artifactory) it allowes me to view the repository.
If I use as the url: http://user:passw#localhost:8081 it works and maven can login, but I need to do a lot of dirty work copying my password all over the projects if I use this workaround (note: the password used in the url is the one I get from the encrypted-password field in Artifactory, not the double-encrypted one I get from the maven command)
This can be due to the following reasons:
Can you check if this user has deploy permission on the remote repositories that are being aggregated under the "remote-repos" virtual repository? If not you should grant the user deploy permissions to the remote repositories that are needed for the builds resolution. If you are not sure you can try using an admin user and see if it worked.
Check if in your Artifactory UI --> Admin --> Security configuration --> "Hide existence of unauthorised resources" is this checkbox checked? If so you will need to define "preemptive authentication" on maven (should be defined in your settings.xml file. See in google on how to add it.
Last thing that comes to my mind, can you double check that the encrypted password was written correctly in the settings.xml file?
Hope this will assist you with finding the root cause of the issue and resolve it.
Please remove all mirrors in your file.
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<servers>
<server>
<id>central</id>
<username>user</username>
<password>{encrypted-pssw}</password>
</server>
<server>
<id>snapshot</id>
<username>user</username>
<password>{encrypted-pssw}</password>
</server>
</servers>
<profiles>
<profile>
<id>central</id>
<repositories>
<repository>
<id>artifactory</id>
<url>http://localhost:8081/artifactory/repo</url>
<layout>default</layout>
<name>Artifactory default repo</name>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>central</activeProfile>
</activeProfiles>
You should also check which settings.xml. According to the maven website:
There are two locations where a settings.xml file may live:
The Maven install: ${maven.home}/conf/settings.xml
A user’s install: ${user.home}/.m2/settings.xml
I was trying to modify the first settings.xml file with no luck, adding my credentials to the second one worked. If you run your maven command with the -e flag it will also tell you the settings file that it is trying to use

Maven artifact is not yet publish in BinTray

I have a Maven project. I added in settings.xml file the configuration for the BinTray server:
<server>
<id>bintray</id>
<username>USERNAME</username>
<password>API_KEY</password>
</server>
Then in the pom.xml I have added:
<distributionManagement>
<repository>
<id>bintray</id>
<url>https://api.bintray.com/maven/USERNAME/maven/PACKAGE_NAME;publish=1</url>
</repository>
</distributionManagement>
In the BinTray web interface I get the following message:
Notice: You have 16 unpublished item(s) for this package (expiring in 6 days and 22 hours) Discard | Publish
So this means that the artifacts (jar, pom, javadoc, sources, hashes) are not yet published.
So do I need every time when I make a release to go to BinTray web interface to publish the artifacts? Is there a setting to publish them automatically from Maven?
You need a slightly different distributionManagement block so the matrix parameters are sent to bintray:
<distributionManagement>
<repository>
<id>bintray</id>
<url>https://api.bintray.com/maven/USERNAME/maven/PACKAGE_NAME/;publish=1;</url>
</repository>
</distributionManagement>

Archetype Selection Using m2eclipse

I would like to create a new Maven project in Eclipse using an archetype using the New Maven Project wizard. At the "Select an Archetype" step, I have three catalogs to choose from: Nexus Indexer, Internal and Default Local. I don't understand where the content for these catalogs should be coming from. If I click on the "Configure" button, they are all greyed out and I can't modify them.
Only the Internal catalog has any archetypes listed in it. Where are these archetypes coming from? It's not from my local Maven repository because the group/artifacts are not in it (and archetypes in the repo are not listed).
Why is the Nexus Indexer list empty? I've read some posts that the Nexus index needs to be updated, but not how to do this. Is this different than the repository indexes (which are scheduled to be updated daily).
As you can see, I'm a little confused about the whole catalog business and how Maven, m2eclipse and Nexus interact. Any clarification is most welcome!
My setup:
Eclipse: Helios Service Release 2 (Build id: 20110218-0911)
Apache Maven 3.0.3
m2eclipse: 0.12.1.20110112-1712 (set up to use external Maven install)
Sonatype Nexus™ Open Source Edition, Version: 1.9.0.2
My local Maven settings.xml looks like this:
<settings>
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://myserver:8080/nexus/content/groups/public</url>
</mirror>
</mirrors>
<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>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<!--make the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
<servers>
<server>
<id>my-snapshots</id>
<username>user</username>
<password>password</password>
</server>
<server>
<id>my-releases</id>
<username>user</username>
<password>password</password>
</server>
</servers>
</settings>
The default archetype catalog can contain new archetypes. It's a manual process, you've to add them to a custom .xml file within your ~/.m2 directory.
For more information : http://maven.apache.org/archetype/maven-archetype-plugin/specification/archetype-catalog.html
And for reference, here's a template archetype-catalog.xml
<?xml version="1.0" encoding="UTF-8"?>
<archetype-catalog>
<archetypes>
<archetype>
<groupId>com.spedge</groupId>
<artifactId>archetype-application-custom-filter</artifactId>
<version>1.0-SNAPSHOT</version>
</archetype>
</archetypes>
</archetype-catalog>
Additionally, here's a good link on SO about Archetypes : What are the URLs of all the Maven Archetype catalogs that you know about?

Categories