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

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.

Related

How to reset the Maven repository profile to default for a particular project?

I have created a new Maven project with IntelliJ and installing the dependencies, the .m2 folder's settings.xml in my ${user.home} folder has an active profile that contains the remote repository details for an internal repository, but for the current project I need to reset all those repository details without modifying the .m2 folder's settings.xml, is there a way I can do it?
See Maven / Introduction to build profiles – Deactivating a profile:
Starting with Maven 2.0.10, one or more profiles can be deactivated using the command line by prefixing their identifier with either the character '!' or '-' as shown below:
mvn groupId:artifactId:goal -P !profile-1,!profile-2,!?profile-3
or
mvn groupId:artifactId:goal -P -profile-1,-profile-2,-?profile-3
This can be used to deactivate profiles marked as activeByDefault or profiles that would otherwise be activated through their activation config.
I would just create an additional settings-other.xml and use it for Maven on the command line (with -s settings-other.xml).

Jenkins & Maven - build process

I am learning about Jenkins and I have to explore some existing build jobs that others wrote (in the company that I'm working).
So I am trying to understand a job which uses mvn command.
So under the build part (inside the job), I see these details:
Maven version: 3.0.5
Root POM: pom:xml
Goals and options: clean install -U -Pnotest,docs
I'm trying to understand what this mvn command means?
I tried to google it: "clean install -U"
But I didn't find what the parameter U means.
And I don't know what is "-Pnotest,docs".
can you guide me regarding how I can find what's it? (maybe "-Pnotest,docs" is from a xml file or it's from the artifactory etc..)
Thanks a lot!!!!
-U Forces a check for miss releases and updated snapshots on remote repositories
If Maven is regularly used in your company, and you will have to work with it on a day-to-day basis, I would advise you to find a mentor (any colleague that knows the tool well and is ready to share its knowledge with you) and work with them. Maven, when you first look at it, can be quite of a mouthful and you'll learn it more efficiently with their help.
For the problem at hand, Elarbi Mohamed Aymen's answer already tells you what the -U flag corresponds to. As for -P, it is used to activate profiles (in your case notest and docs). These profiles are usually defined in the pom.xml of the project being build.
See Running Apache Maven for the basic commands, and as advised on that page run mvn -h to have the complete list of flags the command can use.
Maven is one of the mechanism how to handle the build process and check project dependencies, especially for Java.
One of the option can be to have physically included dependencies (artifacts / libs) in the project, but its not so useful- in case of new version, you have to replace the file, sometimes you are using same lib in more apps, ten you have to handle it manually in all projects.
Except this, there is the maven- it has a global repository with shared artifacts / libs , which are common used- ref. https://repo1.maven.org/maven2/.
Except this, you can make your own libs/ artifacts in this case, its a modules / applications which are reusable, then you are storing it in private repository- this is the artifactory.
When you want to build your project, in case of maven project you have pom.xml , which is like manual for maven what to do / how to build.
clean and install are common goals, clean will wipe your local maven repository, install will download them again, with parameter -U it force to download them.
You can define your own goals in pom file, eg. to "tree build"- build some dependent modules, then build parent project.
Eg. with -D you pass parameters to the maven eg.
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app
- that will generate new project, based on given archetype- "template", with the given groupID and artifactID- groupID can be eg. company name, artifactID is then the name of specific app / component.
-P,--activate-profiles <arg> Comma-delimited list of profiles
to activate
-D,--define <arg> Define a system property

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

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

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

Resources