Difference between mvn -U clean compile and mvn clean compile - maven

I used both the commands to resolve dependency issues in my project. mvn -U clean compile resolved issues. But I don't know difference between them. Please explain difference between those commands and when do I Use mvn clean compile and mvn -U clean compile.

-U, --update-snapshots
Forces a check for updated releases and snapshots on remote repositories
see here (6.1.11. Downloading and Verifying Dependencies):
-clean will clean (remove all compiled and copied resources) of your maven project target directory.
-install will do almost a full maven lifecycle like testing, compiling AND copying the new builds to your local repository.
-U you will tell maven to update your local repository's remote dependencies (usually third party dependencies) as well

-U,--update-snapshots Forces a check for updated
releases and snapshots on remote
repositories
For your information,if you execute mvn -h command in the command prompt then it will display all the options just like below. So if you need to know about any option details, you can use mvn -h command.
usage: mvn [options] [<goal(s)>] [<phase(s)>]
Options:
-am,--also-make If project list is specified, also
build projects required by the
list
-amd,--also-make-dependents If project list is specified, also
build projects that depend on
projects on the list
-B,--batch-mode Run in non-interactive (batch)
mode
-C,--strict-checksums Fail the build if checksums don't
match
-c,--lax-checksums Warn if checksums don't match
-cpu,--check-plugin-updates Ineffective, only kept for
backward compatibility
-D,--define <arg> Define a system property
-e,--errors Produce execution error messages
-emp,--encrypt-master-password <arg> Encrypt master security password
-ep,--encrypt-password <arg> Encrypt server password
-f,--file <arg> Force the use of an alternate POM
file (or directory with pom.xml).
-fae,--fail-at-end Only fail the build afterwards;
allow all non-impacted builds to
continue
-ff,--fail-fast Stop at first failure in
reactorized builds
-fn,--fail-never NEVER fail the build, regardless
of project result
-gs,--global-settings <arg> Alternate path for the global
settings file
-h,--help Display help information
-l,--log-file <arg> Log file to where all build output
will go.
-llr,--legacy-local-repository Use Maven 2 Legacy Local
Repository behaviour, ie no use of
_maven.repositories. Can also be
activated by using
-Dmaven.legacyLocalRepo=true
-N,--non-recursive Do not recurse into sub-projects
-npr,--no-plugin-registry Ineffective, only kept for
backward compatibility
-npu,--no-plugin-updates Ineffective, only kept for
backward compatibility
-nsu,--no-snapshot-updates Suppress SNAPSHOT updates
-o,--offline Work offline
-P,--activate-profiles <arg> Comma-delimited list of profiles
to activate
-pl,--projects <arg> Comma-delimited list of specified
reactor projects to build instead
of all projects. A project can be
specified by [groupId]:artifactId
or by its relative path.
-q,--quiet Quiet output - only show errors
-rf,--resume-from <arg> Resume reactor from specified
project
-s,--settings <arg> Alternate path for the user
settings file
-T,--threads <arg> Thread count, for instance 2.0C
where C is core multiplied
-t,--toolchains <arg> Alternate path for the user
toolchains file
-U,--update-snapshots Forces a check for updated
releases and snapshots on remote
repositories
-up,--update-plugins Ineffective, only kept for
backward compatibility
-V,--show-version Display version information
WITHOUT stopping build
-v,--version Display version information
-X,--debug Produce execution debug output

Related

Set up Jenkins to run a specific maven command

I’m new to Jenkins and currently working on a maven project.
I am able to run a simple Jenkins job using maven commands.
mvn clean install
However, the extended requirement requires me to us an additional parameter in the maven command
mvn clean install -DfileName=file1
Is it possible to have a drop down with file names (e.g. file1, file2 ..) and have the user selected one append to the maven command.
mvn clean install -DfileName = {selected filename from dropdown}.
Could some one please assist with this along with what plugin and how can I setup.
Parameterize your jenkins job see https://wiki.jenkins.io/plugins/servlet/mobile?contentId=34930782#content/view/34930782.
Use choice parameter to add your file name choices
Active Choices Plugin - https://wiki.jenkins.io/display/JENKINS/Active+Choices+Plugin
The user selected choice can be used in your maven command using "{params.param_name}".

What is the meaning of clean test -P?

For one of the project, we are supporting I see the Maven
clean test -P DEV,CI
inside the Goals and Options for the build option of Jenkins. Its is causing the Sonar analysis of the test jobs behave incorrectly.
I tried to find the meaning of -P option, but did not get any solutions so far.
The Maven CLI Options Reference documents what -P means:
-P,--activate-profiles <arg> Comma-delimited list of profiles to activate
It is related to profiles. Read more at Introduction to Build Profiles.

Can we configure settings.xml to our own location in maven

Recently I am working with Apache Maven in Windows machine.
I am executing maven from Command Prompt every time.
I know that MAVEN will pick its settings.xml from C:\Users\abc\.m2\settings.xml
Question: Can't I change this settings.xml to be picked from my own provided location instead of default location, like C:\Users\abc\.m2\settings.xml
That is, instead of C:\Users\abc\.m2\settings.xml location I will configuring it in F:\myFolder\settings.xml.
If so, what are the steps I need to follow and check?
You can change the location of user settings using the -s option from the command line:
-s,--settings <arg> Alternate path for the user settings file
You could hence invoke Maven as following:
mvn clean install -s F:\myFolder\settings.xml
If you don't want to specify it at each and every invocation, since Maven 3.3.1 you can specify a .m/config folder in your project folder and a maven.config file in it providing this option.
The .m/config/maven.config file would have in your case the following content:
-s F:\myFolder\settings.xml
And it will be applied to all Maven invocation for that project.

Bamboo: change the build root for maven plugin?

I have a Bamboo plan that involves building with maven on Windows. The default path to the build directory under the bamboo user is long, and some files end up over the 255-char Windows limit. I wanted to solve the problem by (for this plan only) change the location where the Mavens are run to a short dir, C:\build. I can check out files, then run a script step to copy them from the build dir to C:\build. The Maven bamboo task is configured to override the project file, using C:\build\pom.xml instead. That all works fine. However, when it gets to the 'check in the updated pom' part of release:prepare, it somehow decides that the original build directory with the long path is right, dying with an error.
Anybody know how to specify that the updated pom is also supposed to come from C:\build? I tried overriding the 'Working Sub Directory' entry, but that won't let me specify a full path, so C:\build is out.
Did you try to override the localRepoDirectory parameter?
The command-line local repository directory in use for this build (if specified).
Default value is: ${maven.repo.local}.
You may set this parameter using a property in the POM:
<properties>
<propertyName>C:\build</propertyName>
</properties>
...
<localRepoDirectory>${propertyName}</localRepoDirectory>
It can be overridden in the Bamboo Maven command:
mvn -DpropertyName="D:\build" clean package
(Bamboo variables can also be used to set the propertyName)
You may define a single property with the desired path and use it in several places in the pom.xml.
Turns out there were several different things going on with the Maven 3.x task:
By setting the Override Project File to C:\build\pom.xml, I was able to get the task to try to build in C:\build.
Part of my copying of files from the normal root directory to C:\build was wrong. I'd used xcopy but forgot to add a /H to copy the .svn data as well, so the 'check-in updated pom' step failed because it couldn't find the .svn files.
Once the release:prepare and release:perform were finished, a bamboo 'Artifact Copy' step had been defined earlier to copy several generated artifacts back to the maven repository. Turns out that while this step is somewhat configurable about what files to copy and where they are to be found, it does not support providing an absolute path as the directory to copy from, unlike the Override Project File for the maven tasks. So I had to introduce yet another step, a script to copy the generated artifacts back from C:\build... to under the build root.
All in all, I wasn't able to mess with the build root as I wanted to, but by using the Override Project File and two scripts to copy the source files to C:\build and the artifacts back from C:\build, I got done what I needed to do.

Which Maven goal to use as no-op (for scripting purposes)?

I have a script on Jenkins CI which optionally does dependency:go-offline. The other option should be to do nothing. But I can't put "" in there - it must be a goal.
So - which one would you pick? It should:
Be in central, always reachable
Take minimum time
Have minimal output
Have no side effects
I was thinking of some help:... goal but those tend to have a lot of output. Any better?
You can use this goal and option:
mvn --quiet help:help
the -q,--quiet option causes the output to only show errors.
Note that Jenkins allows you to add options like --quiet as diplayed in the usage: mvn [options] [<goal(s)>]. You configure these in the Jenkins job’s “Goals and options” field.
Check mvn --help output for further information.
I know this is an old question, but I came across it when I had the same requirement and it's still unanswered, so I'm posting for anyone who needs it in future.
This still depends on the current project, but could be useful if you don't want to hardcode a specific plugin for some reason:
mvn -pl ./ validate
-pl ./ means only current project, ignore submodules. Alternatively you could specify specific project by relative path or [groupId]:artifactId.
validate is the first phase of the Default Lifecycle. Doesn't change or build anything.
Alternatively, if you don't have a maven project at all, some maven plugins, or rather specific plugin goals, can be executed without it. E.g.:
mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:help
It would still scan projects if it sees a POM in the current directory. And of course you still need to have the plugin in your local repository.

Resources