What exactly happens during this command: mvn -pl <project list > - maven

What exactly happens during this command:
mvn -pl ABC –am -amd?
Does it compile the code?
The reason I asked is I have purposely put an invalid file and when I run mvn -pl ABC -am -amd option I get successful result and I'm confused why Maven is not complaining about the errored file?
But if I use mvn install command it errors!

-pl or --projects allows you to select a specific set of projects to apply your goal, (e.g. clean install) this way saving the time you would spend waiting for a full build on a big project if you just need to build a couple modules.
You might wanna check the following section:
Specifying a Subset of Projects

-pl makes maven build only specified modules and not the whole project (in this case it's only ABC).
-am makes maven figure out what modules our target depends on and build them too(in this case it's ABC's dependencies).

If you say mvn -pl, and give no argument to -pl, you are asking maven to do absolutely nothing.
-pl assumes that you are sitting in a project with multiple modules, and want to build a subset. You just asked for the null subset.

You haven't actually given it a goal to run. mvn -pl Abc:Xyz -am -amd has two problems with it.
First of all, -amd implies -am, so you don't need both.
Secondly, you haven't given it a goal to run, like install, package, test, or compile.

Related

How can I run all tests of just one maven sub module?

I do need to build the modules it depends on first, but I don't want to run their tests. I've tried various invocations with -am -pl, this is the most recent, and based on maven :: run only single test in multi-module project
mvn test -DfailIfNoTests=false -Dtest="**" -am -pl service-api test
All of these tests should be in the com.myapp.service package, and packages below it. Ideally though, an answer won't rely on that.
Nothing I have tried works though? What's the magic invocation to compile process-resources for dependents, and run tests for just this module?

Why should run first mvn clean clover2:setup install clover2:clover, then: mvn sonar:sonar

Based on the question Sonar + Clover only runs on src-instrumented, it is suggested using first mvn clean clover2:setup install clover2:clover, then: mvn sonar:sonar.
Just wonder why we cannot use mvn clean clover2:setup install clover2:clover sonar:sonar?
In the past it was the recommended way to run goal sonar:sonar alone. This is no more the case since SonarQube Scanner for Maven stopped trying to run unit tests + collect coverage for you by forking a new Maven lifecycle.
General advice is now to run goals in a single command. For example mvn clean package sonar:sonar
In the case of Clover the clover:setup goal will alter the Maven Model to make all other plugins (like surefire) use instrumented classes instead of original source code. This is indeed a problem because it will prevent SonarQube to match class files. So in your case you should either stick with two separate goals, or manually configure sonar.sources to refer to original source code.
Compared the maven logs and found the possible reason:
The "mvn clean clover2:setup install clover2:clover sonar:sonar" seems having issue to find the Source dirs. The log shows it uses ${project}\target\clover\src-instrumented and ${project}\target\generated-sources\annotations as the source dirs.
If explicitly specify src/main/java, then this single command works well. The only tricky thing is why running the goals separately doesn't need to specify sonar.sources but the plugin can still find the right folder for source dirs.

Define modules list which shall be built in Maven multiproject build

I would like to use Maven -pl option to define which specific modules shall be included into reactor. The option works if list of module's paths is provided. Unfortunately, providing module's artifactIds is not working at all.
Sonatype: Maven: The Complete Reference docs are using multiproject example where directories names are matching artifactIds:
Using Advanced Reactor Options
Is it possible to use -pl option with artifactId?
Yes, it's possible to do this. Take a look at mvn --help:
-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.
Note in particular that an artifactId without a leading groupId still has a leading colon.
So, for example in a case where the artifactId is the same as the directory name, these three lines would all refer to the same module in Maven:
mvn -pl maven-core
mvn -pl :maven-core
mvn -pl org.apache.maven:maven-core
mvn seems to take the list you provide with -pl to the heart and not build any dependencies automatically.
So in addition to Joe's answer: If the project/module you're trying to build depends on other modules then you can ask mvn to build them as well with -am.
-am,--also-make
If project list is specified, also build projects required by the list
If project list is specified = if -pl option is used
So examples become:
mvn -pl maven-core -am
mvn -pl :maven-core -am
mvn -pl org.apache.maven:maven-core -am

How to build multimodule project faster in maven 2?

I have a multimodule maven project in which there are 7 several modules.
Every time I modify a code in one of the module and run mvn clean install, it takes some time and I think there might be a way to reduce time of builds since I only change small part of the code and I think it effects only limited area.
So I guess instead of running "mvn clean install" there is a better command option.
I am using maven 2.x.
Any better ideas?
TIA
The "clean" goal deletes all the target folders which contain the compiled classes, it should not be necessary to run that goal as part of the modify-compile-test cycle that you may run many times per day. In your case most of the time is probably spent re-compiling all the Java files, due to the class files having been deleted during the "clean" goal. So I would use "mvn install" when testing local changes, and "mvn clean install" only periodically, like when you get other developers changes to integrate with, or when testing immediately before a release.
I would suggest to use the following:
mvn -pl TheModuleYouHaveChanged -amd install
or maybe a
mvn -pl TheModuleYouHaveChanged -amd package
is enough.

How do I exclude certain modules from a Maven build using the commandline

Is there a way to exclude some modules from a big reactor build, similar to -pl ?
Here are a number of ways to do it persistently:
How to exclude a module from a Maven reactor build?
I want to do it from shell, or at least without modifying the poms, which I am not allowed to change.
Maven 3.2.1 has added this feature, you can use to specify the exact projects you want (or to exclude the projects you don't want) -pl or --projects Here's how to exclude two:
-pl "!<modulename>,!<modulename2>"
for exclude certain modules. This can be comma separated list of values that you want to include/exclude.
Update
For windows user following
> mvn clean [package|install] --projects \!groupId:artifactId
Another comment on the accepted answer, don't forget to escape the exclamation sign when running the command in bash:
> mvn clean install -pl \!module,\!module/submodule,\!groupId:artifactId
As Yogesh_D wrote it can be done with the -pl argument with maven 3.2.1+
Here's an example:
> mvn clean install -amd -pl !module,!module/submodule
You need to list every sub-module (and sub-sub-module etc) manually, it does not exclude them recursively. Use the slash for package separation. It's the folder path, not the group or artifact id.
I don't believe this is currently possible from the command line. There is an open feature request in maven3 for this very thing (https://issues.apache.org/jira/browse/MNG-5230).
Looks like your only option at this point is to modify the pom and create a new build profile that includes only the modules you want to build.
Instead of using exclamation ! sign you can use minus - sign.
mvn clean -pl -module1
You can also exclude multiple modules.
mvn clean -pl -module1,-module2
or
mvn clean -pl -module1 -pl -module2
Tested with:
Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-17T20:33:14+02:00)
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Excluded modules must be in reactor of current project, so we can't exclude no existing module.
mvn clean -pl -no-existing-module
currently will fail - https://issues.apache.org/jira/browse/MNG-7033
in 2019 it's
mvn install -pl !:module1
(Windows 10 cmd)
comma separated module name enclosed with double quotes.
eg::
mvn install -pl "!Module1, !Module2"

Resources