List maven dependencies only used during test case run - java

Can list out the maven dependencies only used during test.
For example: mvn dependency:list
But only ones for test run

mvn dependency:list by default list dependencies with scope test.
This means that you will see dependencies needed for test.
For executing tests you need all your project dependencies.
On list you see scope for each listed dependencies, like:
[INFO] The following files have been resolved:
[INFO] org.apache.maven:maven-plugin-api:jar:3.2.5:provided
...
[INFO] org.codehaus.plexus:plexus-utils:jar:3.5.0:compile
...
[INFO] junit:junit:jar:4.13.2:test
You can include specific dependency scope, or exclude some of them.

Related

mvn -pl <name> -am command

I have the following command, which I'm using to build a specific microservice (<ms-name>):
./mvnw -DskipTests clean install -pl <ms-name> -am
clean, instal, and -DskipTest are clear. But I don't understand what -pl and -am are doing.
logs:
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] <utils-1> [jar]
[INFO] <utils-2> [jar]
[INFO] <ms-name> [jar]
and after it starts executing clean install on , , and in the end <ms-name>
I have checked the pom.xml but could not see the relations somewhere.
My question is: What are -pl and -am and where I can find this 'order' building relation between <ms-name>, <utils-1>, and <utils-2>?
From documentation,
-pl is,
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
-am is,
If project list is specified, also build projects required by the list
where I can find this 'order' building relation ..?
It is handled by reactor (and internal program within maven) which collects all the available modules to build, sorts the projects into the correct build order and builds the selected projects in order.
To get understand more about maven reactor, here is a StackOverflow answer which is more detailed enough.
What is the "reactor" in Maven?

How are maven plugin aliases mapped

I am trying to understand what mvn clean:clean actually does.
mvn -B help:describe -Dcmd=clean
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building sample-one 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.2:describe (default-cli) # sample-one ---
[INFO] 'clean' is a lifecycle with the following phases:
* pre-clean: Not defined
* clean: org.apache.maven.plugins:maven-clean-plugin:2.5:clean
* post-clean: Not defined
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.689 s
[INFO] Finished at: 2015-12-10T10:20:16-08:00
[INFO] Final Memory: 9M/245M
[INFO] ------------------------------------------------------------------------
It appears to me that mvn clean:clean is same as doing mvn org.apache.maven.plugins:maven-clean-plugin:2.5:clean. Therefore I am assuming the first clean in mvn clean:clean is just an alias for org.apache.maven.plugins:maven-clean-plugin:2.5. Similarly mvn maven-surefire-plugin:2.12.4:test is same as mvn surefire:test.
So somehow, maven-surefire-plugin:2.12.4 seems to refer to surefire and org.apache.maven.plugins:maven-clean-plugin:2.5 to clean.
When I look at the effective-pom, I see the following
maven-surefire-plugin
2.12.4
default-test
test
test
maven-clean-plugin
2.5
default-clean
clean
clean
As you can see, the pom doesnt seem to define alias. So following are my questions
Is my understanding about plugin aliases correct
If my understanding about aliases is correct - a) how and where are they defined? b) Is there a way to list all aliases.
From official Maven documentation about plugins development:
Shortening the Command Line
There are several ways to reduce the amount of required typing:
If you need to run the latest version of a plugin installed in your local repository, you can omit its version number. So just use mvn sample.plugin:hello-maven-plugin:sayhi to run your plugin.
You can assign a shortened prefix to your plugin, such as mvn hello:sayhi. This is done automatically if you follow the convention of using ${prefix}-maven-plugin (or maven-${prefix}-plugin if the plugin is part of the Apache Maven project). You may also assign one through additional configuration - for more information see Introduction to Plugin Prefix Mapping.
Finally, you can also add your plugin's groupId to the list of groupIds searched by default. To do this, you need to add the following to your ${user.home}/.m2/settings.xml file:
<pluginGroups>
<pluginGroup>sample.plugin</pluginGroup>
</pluginGroups>
At this point, you can run the mojo with mvn hello:sayhi.
So, alias are not defined in the pom file but part of built-in mechanism of maven. Further details are also provided in the official documentation about Plugin Prefix Resolution.

Maven: Build only one module out of multi-module project

I have a multi-module Maven project wherein I just want to release an update module rather than the entire project. This is due to some specific requirements, wherein a release of entire project is uncalled for - only a subset of the library needs the fixes and thus the release.
However, when I run a mvn release:prepare I get the following error Non-resolvable parent POM - I have setup the parent POM relationship in the module project with relativePath tag but that does not seem to work.
Is it possible to release only a module rather than releasing the entire project.
Thanks in advance.
EDIT
Parent pom
<groupId>com.domain</groupId>
<artifactId>project-parent</artifactId>
<version>0.5.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>library1</module>
<module>library2</module>
<module>library3</module>
</modules>
The module POMs are as under:
<parent>
<groupId>com.domain></groupId>
<artifactId>project-parent</artifactId>
<version>0.5.1-SNAPSHOT</version>
</parent>
<artifactId>library1</artifactId>
Now I just want to release the new version of library1 and not others
mvn -pl .,library1 clean install
The "." will build your parent pom.xml and "library1" is the module name you gave it.
If you don't want to build the parent pom.xml, you can also do:
mvn -pl library1 clean install
or in your case, maybe:
mvn -pl library1 deploy
I used "deploy" because "release" is not a Maven build lifecycle phase.
Try running the maven commands from the parent project/directory with the -pl <project name> switch.
the trick is both:
to repeat the -pl Param inside an additional -Darguments Param, to pass the -pl Param to the Child-Maven-Processes
the mysterious mavenExecutor, to be able to create the Child-Maven-Processes using the given -pl Param
This should work:
mvn -pl library1 -DmavenExecutorId=forked-path -Darguments="-pl library1"
I think maven is trying to tell you that the parent you defined is not released/stable. In your pom the parent version is "0.5.1-SNAPSHOT" which can not/should not work if you want to release a single library module as it would be pointing to something that is not uniquely defined. Is there a stable version you can point to instead? Otherwise maybe the -am switch works in combination with -pl?
Edit
I tried this just now with maven 3.2 and it does ask me for what version the parent should be set/resolved to. It is as I stated previously you need stable version numbers for the release to work
mvn release:prepare -pl library1
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building library1 1.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-release-plugin:2.3.2:prepare (default-cli) # library1 ---
[INFO] Verifying that there are no local modifications...
[INFO] ignoring changes on: **\release.properties, **\pom.xml.next, **\pom.xml.releaseBackup, **\pom.xml.backup, **\pom.xml.branch, **\pom.xml.tag [INFO] Executing: cmd.exe /X /C "git status"
[INFO] Working directory: C:\temp\mavenTest\library1
[INFO] Checking dependencies and plugins for snapshots ...
There are still some remaining snapshot dependencies.
: Do you want to resolve them now? (yes/no) no: : yes
Dependency type to resolve,: specify the selection number ( 0:All 1:Project Dependencies 2:Plugins 3:Reports 4:Extensions ): (0/1/2/3) 1: : 1
Dependency 'testme:parent' is a snapshot (1.1-SNAPSHOT)
: Which release version should it be set to? 1.1: :

Is there a maven equivalent of rpm --whatprovides for a jar file?

How do I use maven or another tool to find which dependency of a dependency which provides a particular jar? Sometimes they're three or four dependencies deep.
If you want to find out from where a transitive dependency is coming from for a given project, then the Maven Dependency Plugin is indeed your friend. Use it with the includes parameter that allows to specify a comma-separated list of artifacts to filter the serialized dependency tree by, or null not to filter the dependency tree. The artifact syntax is defined by StrictPatternIncludesArtifactFilter.
About the syntax, the javadoc writes:
The artifact pattern syntax is of the
form
[groupId]:[artifactId]:[type]:[version]
Where each pattern segment is optional
and supports full and partial
* wildcards. An empty
pattern segment is treated as an
implicit wildcard.
For example, org.apache.* would
match all artifacts whose group id
started with org.apache., and
:::*-SNAPSHOT would match all
snapshot artifacts.
Here is an example (I want to find from where the activation artifact is coming from on a project):
$ mvn dependency:tree -Dincludes=:activation::
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Java EE 6 Demo - Petstore - Domain
[INFO] task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree {execution: default-cli}]
[INFO] name.thivent.samples.javaee6.domain:domain:jar:1.0-SNAPSHOT
[INFO] \- org.hibernate:hibernate-validator:jar:4.0.2.GA:runtime
[INFO] \- javax.xml.bind:jaxb-api:jar:2.1:runtime
[INFO] \- javax.activation:activation:jar:1.1:runtime
[INFO] ------------------------------------------------------------------------
...
M2Eclipse provides a nice front-end to the dependency:tree if you are using it.
For something "closer" to rpm --whatprovides (i.e. without searching for a particular project), you would have to use a repository search engine. Here is an example for activation-1.1.jar (see the This artifact is used by ... section).
I suppose you are looking for:
mvn dependency:tree
Edit: There are more options available to analyze dependencies. Have a look at the documentation
Even for direct dependencies in Java it is at least a challenge. Not all dependencies are static and could be reflected from the class file. And even for those classes there's no way to get the correct library that is needed to meet that dependancy.
We have for example the Class.forName(String className) way of adding dynamic dependencies and the class name could be written in a resource or properties file or constructed at runtime. And the named class then could introduce a dependenciy to a different library.
I doubt that any tool is capable of resolving all those dependencies.
This isn't a direct answer but it might help if you are using Eclipse with a Maven plugin.
If I want to find where a class comes from, I do the following:
Select the class name and use the F3 shortcut to load the source code into the Editor window.
Use "Show In > Package Explorer" to locate the file in (typically) the Maven Dependencies section of the build path.
Look at the JAR filename of the enclosing dependency "folder", and read off the group id, artifact id and version.
(This might not be 100% reliable in a multi-module project if you use Eclipse Maven's nested module support. But I've not encountered problems in practice.)

In Maven 2, how do I know from which dependency comes a transitive dependency?

I would like to know which dependency described in my pom.xml brings a transitive dependency in my target directory.
To be more precise, I have the library "poi-2.5.1-final-20040804.jar" in my WEB-INF/lib directory and I would like to know which dependency in my pom.xml brings that.
To add to #David Crow, here's a dependency:tree example from the Maven site:
mvn dependency:tree -Dincludes=velocity:velocity
might output
[INFO] [dependency:tree]
[INFO] org.apache.maven.plugins:maven-dependency-plugin:maven-plugin:2.0-alpha-5-SNAPSHOT
[INFO] \- org.apache.maven.doxia:doxia-site-renderer:jar:1.0-alpha-8:compile
[INFO] \- org.codehaus.plexus:plexus-velocity:jar:1.1.3:compile
[INFO] \- velocity:velocity:jar:1.4:compile
Using the Maven Dependency Plugin:
mvn dependency:tree
If you use eclipse and the m2eclipse plugin then there is a graphical version of dependency tree where you can filter by scope etc.
If you run maven with "-x" switch, it will print out plenty of diagnostics, I guess the relevant dependency path can be picked up from there.
You can have many reports by
mvn site
One of them is the dependency report.
The dependency information is also included in the Project Information/Dependencies report if you have maven generate a site for the project, using mvn site.

Categories