Change Maven settings at build time - maven

I would like to deploy my Maven build to an Artifactory repo. This repo requires authentication, but I would prefer not modifying my settings.xml file. Is there a way to provide the credentials at build time? I know that you can set properties in the POM with the -D switch:
mvn clean package -Dmy.prop=blah
Is there a way to do something similar to provide the contents of a <server> block in the settings.xml file?

You can use the Maven Artifactory plugin, which accepts credentials in pom file and use the -D as intended.
And, of course, you'll get the full build-info support :)

You can prepare separate settings.xml file for build purpose and use this by -s options.
Eg.
mvn -s build_settings.xml clean deploy

Related

Is possible to have 2 settings.xml for a single maven home folder?

I am having 2 projects. As per the requirement, I have to maintain the repos separately and some configuration also different.
I don't have permissions to install one more maven installation folder in my lap.
Is possible to have 2 settings.xml for a single maven installation home folder? (I would like to maintain like settings_A.xml and settings_B.xml in different locations).
Is it valid? Please correct me if I am wrong.
Thanks.
yes you can point to a custom settings.xml per use and pass it as argument to Maven as following:
mvn --settings YourOwnSettings.xml clean install
or use shorter form:
mvn -s YourOwnSettings.xml clean install
you can also use project specific settings.xml configuration that explained in other stackoverflow question here

How to make maven use credentials when executing dependency plugin from command line

I have a private Maven repository. It's defined in pom.xml of the project
<repository>
<id>some.id</id>
<url>https://some.host/artifactory/some.id</url>
</repository>
In my ~/.m2/settings.xml, I have proper authentication block:
<server>
<id>some.id</id>
<username>pawel.veselov#domain.com</username>
<password>{some-fancy-password-hash-goes-here}</password>
</server>
When building the project, Maven is able to access the repository without any problems, as it's supposed to be. In debugging output, I can see it applying the credentials.
But when I try to download an artifact directly, it doesn't look like Maven is even considering the settings file.
mvn org.apache.maven.plugins:maven-dependency-plugin:3.1.1:get \
-DremoteRepositories=some.id::::https://some.host/artifactory/some.id \
-Dartifact=groupId:artifactId:1.1.1
The username is not transmitted. I can see in the debug output, that the BasicRepositoryConnector is invoked without username/password combination.
So the question goes - can a plugin be invoked so that whatever process makes Maven consider using the authentication stated in its settings file is executed?
It doesn't look like the plugin is using the saved credentials when remoteRepositories property is used. Testing using repositoryId instead worked as expected for me.
mvn org.apache.maven.plugins:maven-dependency-plugin:3.1.1:get -DrepositoryId=some.id -Dartifact=groupId:artifactId:1.1.1
The effective pom must contain repository and url for the repo. If you plan to run this in an empty directory without any pom.xml then your repository definition will need to be moved to settings.xml

maven - how to deploy non-jar files

In my project I have many modules.
One of the modules requires a jar file to be deployed to the repository which it does fine.
The others involve every other kind of file: zip, kar etc.
I can see the zip get uploaded if I look for it via the terminal but if I browse Archiva it is not there.
The kar file, for example, does not need to be built but it's being worked on and is currently manually uploaded to the repository (Archiva). This is not desirable.
Each module has a POM and each POM uploads empty jar files to Archiva when it is built (with Jenkins). How can I avoid that? And can I copy files to Archiva without them having to be built into a jar file?
You can also give a try to "maven-deploy-plugin"
Invoke a maven target in jenkins with this plugin and provide the suitable parameters.
You would also need the repository to be added in you settings.xml if your repository requires login credentials and then use the ID, you mentioned in settings.xml, in the maven target.
org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy-file
-Durl=<artifact-repo URL>
-Dfile=<name of the file>
-DgroupId=<Group Id>
-DartifactId=<Artifact Id>
-Dversion=<VERSION>
-Dpackaging=<packaging Type>
-DrepositoryId=<ID as mentioned in settings.xml>
Hope this may be of some help.

Maven local repository

I want Maven to work offline. I downloaded all artifacts like spring and hibernate onto my computer. Then I tried to set up Maven to use local repository only. I followed instructions to point Maven to local repository. However every time I tried to load spring mvc project, I got the errors like this:
Offline / Missing artifact org.springframework:spring-context:jar:3.0.6.RELEASE:compile
Offline / Missing artifact org.springframework:spring-core:jar:3.0.6.RELEASE:compile
I checked the local repository. The jar and pom files are there. I can't figure out what is wrong with my configuration. Can someone help me out here?
Thanks.
Jerry
You probably messed up your settings.xml.
Correct way to prepare for offline is this:
mvn install dependency:go-offline
The answer bilash.saha gave will not work for multi module projects.
After first command finished, you may test that everything is ok by running
mvn -o package
To save you a from typing "-o" every time use this settings.xml:
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<offline>true</offline>
</settings>
Run
mvn dependency:go-offline
then build your project offline using the '-o' flag:
mvn install -o

Specifying Maven's local repository location as a CLI parameter

Is it possible to set the location of the local Maven repository as argument on the Maven command line?
The thing is that I don't use the default one in ~/.m2/repository. However I checked out some project that is being built with its own settings with -s settings.xml. That settings.xml doesn't specify my local repository, so Maven uses uses ~/.m2/repository again... I would like to use a non-default local repository location without having to add a <localRepository> element in the project's settings.xml
I've tried
-DlocalRepository="..."
$mvn invoker:run -s settings.xml clean install -DskipTests -DlocalRepositoryPath=
-Dsettings.localRepository
but none of these options works.
So I think I have to decide whether I will be modifying a third party settings.xml or move my local repo to ~
use maven property maven.repo.local:
mvn -Dmaven.repo.local=$HOME/.my/other/repository clean install
No modifications to settings.xml are necessary.
For git:
alias mvn='mvn "-Dmaven.repo.local=$(git rev-parse --show-toplevel)/.m2/repository"'
This uses a separate maven repository in each git repository
One kind of hacky way that would work is:
Add <localRepository>${m2.localRepository}</localRepository> to your settings.xml
In your mvn.sh or mvn.bat, add -Dm2.localRepository=<full path to home dir>/.m2/repository before the "$#" in the command that gets executed. This will make your default local repo stay where it should be.
Now you can use mvn -Dm2.localRepository=... <phases/goals>. Because your command line system property gets added to the command line after the one in the mvn script, it will take precedence. (At least I'm pretty sure it works that way on both windows and linux, but testing will tell.)

Resources