Maven: Set the settings.xml location in the pom.xml? - maven

Is it possible to set the location of the settings.xml file inside the pom.xml file.
I know you can do it from the command line typing mvn -s location/of/settings.xml, but I wanted to know if I can set that within the pom.xml so I don't have to keep typing through command line.

No. And that's probably a horrible idea, from a security standpoint. It'd allow the creator of a pom to bypass all your settings.xml settings. If you do the mvn -s location/of/settings.xml you will at least know it happened. But if you just randomly build a project, who knows how malicious that project's creator was.

No, but you can set what you need from the settings.xml file into the pom.xml and it will override what's in settings.xml

Something that may help you get what you want are profiles:
A profile in Maven is an alternative set of configuration values which set or
override default values. Using a profile, you can customize a build for
different environments. Profiles are configured in the pom.xml and are given an
identifier. Then you can run Maven with a command-line flag that tells Maven to
execute goals in a specific profile. The following pom.xml uses a production
profile to override the default settings of the Compiler plugin.
Source: http://maven.apache.org/pom.html#Profiles

Related

Setting System Properties for the maven-surefire-plugin via CLI

I am currently working on a use-case where I have to set a system property for the maven-surefire-plugin via command line. I've tried to use the systemPropertiesFile property but it seems like my build doesn't pick up the properties in the file . Here's the syntax that I've tried:
mvn install -DsystemPropertiesFile=<path-to-file>
I'm using Maven 3.0.5 for this. Setting the same property via the POM file works fine, but unfortunately that is not a solution I can use. Am I missing something?
systemPropertiesFile is not exposed as a user property, so if you don't want to change pom.xml you could use argLine:
mvn install -DargLine="-DmyProperty=abc -DotherPoperty=def"

Disable Maven Settings inheritance

Maven has concept of global settings and user-level settings, that compose into effective-settings for build purposes, see this.
The question is, how can I disable inheritance of global settings inheritance for some particular build?
Command mvn verify -s settings.xml overrides only user-level settings for me, global ones are still visible in effective-settings.
If you are working on an maven-invoker-plugin test you can define a separate settings.xml file in the src/it/....
If you need having a separate repositories or using particular dependencies you should use the mock repository manager which is intended for such things.
The basic test if you integration tests is correct start with empty local repository by using mvn clean version which should work.
As an example you can take a look at the versions-maven-plugin which uses such setup.

Can I override individual maven plugin settings in settings.xml (or elsewhere)?

My team uses a plugin that generates javadocs that adds about 20 - 30 seconds to our build time. I don't ever use these javadocs, so I would like to eliminate them from my build.
Changing the POMs is not possible for various reasons, so I can't add a profile to conditionally use those (even if they default to true!).
My read of the docs suggests that this is not possible in settings.xml since it's a truncated version of and doesn't contain .
How can I override plugin settings locally?
Skip it using a system property:
mvn -Dmaven.javadoc.skip=true clean install
You can change some behavior using system properties, e.g. the value of the configuration settings skipTests is bound to the user property skipTests, so when you invoke Maven with -DskipTests=true, it will implicitly set this value.
However, that works only if a) your POMs rely on properties to make configurations, b) your POMs do so far not set these configuration settings explicitly and c) it is not some special value that cannot be set via properties, e.g. config settings that accept multiple values such as some "includes/excludes" settings.
If the plugin has an option to skip its run and you are inheriting it form a parent pom, yes you can "re-define" the plugin config in the child poms.
Here are some related answers that could help you:
Disable a Maven plugin defined in a parent POM

How does maven pick a profile (if none is set)?

I have two profiles specified in my settings.xml, a and b. A is defined before b.
I just did run a mvn clean install -X. Did maven pick a profile?
I'm not using the <activeProfiles> block in my setings.xml, and I didn't specify a -P argument. There is no <activation> block in my settings either. And I don't seem to have anything in my global settings file. I also searched my project's POM and parent POM for the profile name, but I got zero matches, so I assume it's not specified in there...
I've had a look at "Introduction to Build Profiles" in the docs but that hasn't given me any clues...
I would have expected for maven to prompt me if no profile is specified but if there are some defined in the settings...
Is it just not using any?
How does it pick a profile (if none is set)?
If you didn't explicitly active a profile (by using -P profileId) and if none of the profiles is triggered by it's activation then none is triggered.
For Maven it doesn't matter if profiles are activated or not.
You can confirm this with mvn org.apache.maven.plugins:maven-help-plugin:2.2:active-profiles.
Be aware that profiles are often used for the wrong reasons. Even here at SO some give bad advices.
It would help if you would say why you want to use a profile.

Change Maven settings at build time

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

Resources