Using profiles in maven - 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

Related

Maven deploy using profile

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

Configuring Maven repositories?

I’m trying to configure Maven to be used inside my company. However, i reach the phase in which I must configure the repositories to be used.
The common configuration for all user is set in the setting.xml file
under MAVEN_HOME
A user’s configuration is set in the setting.xml file under
USER_HOME.
A project’s special configuration is set in the pom.xml
In my case, the second type of configuration should be used (A configuration for each user). However, a mismatch occurs while having a look at the Maven documentation. There is 2 cases either to use a <mirrors> or <repositories> under a default profile.
What is the recommended and in case the 2 are set what's the one it will be taken by Maven engine http://repository.poo/ or http://repository.foo/ ?
<mirrors>
<mirror>
<id> </id>
<mirrorOf>* </mirrorOf>
<name> </name>
<url>http://repository.foo </url>
</mirror>
</mirrors>
or
<profiles>
<profile>
<id> </id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id></id>
<name></name>
<url>http://repository.poo/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
You will almost certainly need the <repositories> if you have your own company repository and you want something other than the maven default.
You may need a mirror if you want to override something, for instance you may want to cache maven central artifacts in a local Nexus.
Take a look at https://maven.apache.org/guides/mini/guide-mirror-settings.html
If in doubt leave the mirror.

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.

Sonatype Nexus: How to set a single server credentials for multiple repositories in maven's settings.xml?

We have multiple repositories in Nexus (i.e., releases, snapshot and site). All 3 repos are under public group and users uses the same credentials to access all these repositories. Providing the same username and password in settings.xml for each repository makes it redundant and hard to maintain them.
Could you please suggest an elegant way to describe one server credential for all the 3 repositories?
Any help is greatly appreciated.
We are using maven 2.2.1 and Nexus OSS 2.7.1
Here is my settings.xml
<settings>
<servers>
<server>
<id>snapshot</id>
<username>deployment</username>
<password>deployment123</password>
</server>
<server>
<id>release</id>
<username>deployment</username>
<password>deployment123</password>
</server>
<server>
<id>site</id>
<username>deployment</username>
<password>deployment123</password>
</server>
</servers>
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/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>
</settings>
Just use one entry in setttings.xml like that
<server>
<id>nexus</id>
<username>deployment</username>
<password>deployment123</password>
</server>
and then in distributionManagement in your pom.xml's you use something like that
<distributionManagement>
<repository>
<id>nexus</id>
<name>Nexus Releases</name>
<url>http://localhost:8081/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>nexus</id>
<name>Nexus Snapshot</name>
<url>http://localhost:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
For fully working setup with this look at the Nexus Book Examples project that are used in the trial guide. You can add a site with the same id as well, of course. Keep in mind that there is no problem if the id;s are the same as they just detail the identifier of the server element in settings to look for and are NOT an id element for the repository. Imho it should be called serverId or something to be clearer, but thats a different story.
Not a solution but a workaround:
settings.xml will handle system properties and environment variables. So if you're not fussed about putting your server authentication details in a script or in your environment, you can stick with three server credentials but eliminate the need to update all three of them in favour of updating your script or environments (I've put examples for both options in this snippet):
<servers>
<server>
<id>releases</id>
<username>${env.NEXUS_USERNAME}</username> <!-- Env var -->
<password>${nexus.password}</password> <!-- System (-D) var -->
</server>
<server>
<id>snapshots</id>
<username>${env.NEXUS_USERNAME}</username> <!-- Env var -->
<password>${nexus.password}</password> <!-- System (-D) var -->
</server>
<server>
<id>site</id>
<username>${env.NEXUS_USERNAME}</username> <!-- Env var -->
<password>${nexus.password}</password> <!-- System (-D) var -->
</server>
</servers>
Unfortunately there's no <properties> element supported in settings.xml!
Aside: maven already handles the snapshots and releases repositories within nexus, and that is the better way to do things. Your posted settings.xml even enables them already. Why do you need separate repository entries for snapshots and releases?
It looks like you mistaken things here. The given credentials and id's are for the distributionManagement and not for the access to the Nexus in this case. Apart from that you need three different username, password combinations cause you have three possible things releases, snapshots and site. So not a big deal.
Aprt from that i would suggest to upgrade Maven into Maven 3.X line cause Maven 2.2.1 is a little bit out of date.

Maven - <server/> in settings.xml

I use tomcat-maven-plugin to deploy my war to a server. What I have to do is configure it like this in my pom.xml:
<configuration>
...
<url>http://localhost/manager</url>
<username>admin</username>
<password>admin</password>
...
</configuration>
But then I obviously want to keep this settings in a different place since I work on my computer but then there's a staging and a live server as well where the settings of the server are different.
So let's use the .m2/settings.xml:
<servers>
<server>
<id>local_tomcat</id>
<username>admin</username>
<password>admin</password>
</server>
</servers>
Now change the pom.xml:
<configuration>
<server>local_tomcat</server>
</configuration>
But where to put the URL of the server? There's no place for that in the settings.xml under the server tag! Maybe like this?
<profiles>
<profile>
<id>tomcat-config</id>
<properties>
<tomcat.url>http://localhost/manager</tomcat.url>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>tomcat-config</activeProfile>
</activeProfiles>
..and use the ${tomcat.url} property.
But then the question is, why use the server tag in settings.xml at all? Why not use properties for the username and password as well? Or is there a place for the URL as well in the settings URL so I don't have to use properties?
First off let me say, profiles are one of the most powerful features of Maven.
First make a profile in your pom.xml that looks like this:
<profiles>
<profile>
<id>tomcat-localhost</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<tomcat-server>localhost</tomcat-server>
<tomcat-url>http://localhost:8080/manager</tomcat-url>
</properties>
</profile>
</profiles>
Then in your ~/.m2/settings.xml file add servers entries like this:
<servers>
<server>
<id>localhost</id>
<username>admin</username>
<password>password</password>
</server>
</servers>
The configure your build plugin like this:
<plugin>
<!-- enable deploying to tomcat -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<server>${tomcat-server}</server>
<url>${tomcat-url}</url>
</configuration>
</plugin>
This will enabled your tomcat-localhost profile by default and allow you to deploy to it with a simple mvn clean package tomcat:deploy.
To deploy to other targets, set up a new <server/> entry in settings.xml with the appropriate credentials. Add a new profile but leave off the <activation/> stanza and configure it to point to the appropriate details.
Then to use it do mvn clean package tomcat:deploy -P [profile id] where the [profile id] is the new profile.
The reason that credentials is set in the settings.xml is because your username and password should be secret in most cases, and there is no reason to deviate from the standard way of setting up server credentials that people will have to adapt to.
settings.xml
<settings>
<servers>
<server>
<id>company.jfrog.io</id>
<username>user-name</username>
<password>user-password</password>
</server>
</servers>
</settings>
pom.xml
<repositories>
<repository>
<id>company.jfrog.io</id>
<url>https://company.jfrog.io/company/release</url>
</repository>
</repositories>
Put settings.xml to
c:/Users/user-name/.m2/settings.xml (for Windows),
~/.m2/settings.xml (for Linux).
company.jfrog.io can be any identifier, but it should be the same in settings.xml and pom.xml.
This works for Maven 3.

Resources