Maven - See dependency tree without building project - java

I want to see the dependency tree of a project without actually downloading those dependencies.
I have a project whose build fails because there is some dependency which is not present in central repo, however it is not a direct dependency and I am not aware which one of my dependencies refers to it.
Now when I run mvn dependency:tree command, it builds the project and hence fails.
One way to do it is keep a dummy jar in local repo with the same name. It will not try to download the dependency and generate the entire tree. However is there any other way to do this ?

If you are using eclipse there is a "Maven POM Editor", which shows not only the maven XML, but also a dependency hierarchy view.
A working build is not necessary for it, just a correct POM XML file.
It should get installed, when you install the eclipse m2e plugin.
The update site is http://download.eclipse.org/technology/m2e/releases.

Maybe Maven dependency:analyze-only
mvn dependency:analyze-only
Analyzes the dependencies of this project and determines which are: used and declared; used and undeclared; unused and declared. This goal is intended to be used in the build lifecycle, thus it assumes that the test-compile phase has been executed - use the dependency:analyze goal instead when running standalone.
or dependency:resolve:
mvn dependency:resolve

mvn dependency:tree -DoutputFile=tree.txt

Related

How to find the dependencies of a maven plugin goal

I'm trying to find out where the dependencies are coming from for a particular plugin goal. I know the dependencies exist because Maven downloaded them when I ran the goal for the first time. And they're not (directly) dependencies of my project, because I ran mvn clean install first, and these dependencies weren't downloaded then.
In this specific case, I'm trying to figure out what the dependencies are when I run mvn sonar:sonar, but I expect the answer will be general purpose. For instance, even though I've built this project a number of times, when I ran that goal Maven downloaded a bunch of new jars like maven-antrun-plugin.
Here are things I've tried:
mvn dependency:tree shows the dependencies for the project, but not for the plugin goal (it doesn't include anything related to SonarQube in the list).
mvn org.apache.maven.plugins:maven-help-plugin:3.2.0:effective-pom -Dverbose=true also doesn't include anything related to SonarQube.
mvn -X sonar:sonar prints out what looks like a dependency graph, but it's missing the jars that Maven downloaded the first time I ran the sonar:sonar goal.
mvn -X dependency:resolve-plugins seems to be meant to download the dependencies of plugins, but does not capture the sonar:sonar dependencies. If I clear out my Maven cache, run mvn dependency:resolve-plugins, and then run mvn sonar:sonar, Maven has to download jars.
Use your IDE to navigate to the POM of the plugin project and then look at the dependency tree.
Or do this manually by copying the plugin POM to your file system and running mvn dependency:tree.
The dependencies downloaded by maven are generally stored locally on the pc at the address C:\Users\yourUser/.m2 this regardless of your projects and so that you do not have to reload dependencies that you are already using in other projects.
I hope I have understood your question and that my answer is useful to you, greetings.

in which maven life cycle, dependencies get downloaded from maven central repository to my local .m2 repository

Generally i follow below maven commands to build and run my project.
mvn clean
mvn clean verify
or
mvn clean install
mvn spring-boot:run
My doubt is in which maven life cycle, dependencies get downloaded from maven central repository to my local .m2 repository.
I went through below mentioned maven life cycle but no where i found that in this steps dependency gets downloaded.
validate
compile
test
package
verify
install
deploy
Please explain it would be really helpful.
When you create a maven project, before validate there is step 'prepare-resources' which copies resources. Also when you do maven clean it will download dependencies. Read this link for more details
https://www.tutorialspoint.com/maven/maven_build_life_cycle.htm
The other answer here of prepare-resources is incorrect. That may be confusing the downloading of Maven plugins and their dependencies, but not the project's dependencies.
They actually are downloaded during the compile lifecycle.
Here is an example of a project where the only dependency is GSON, and I just finished running the process-resources lifecycle, the one that immediately precedes compile lifecycle. The only things present in my .m2/repository directory are things required by the default Maven plugins. Note that there is no com folder, which is where GSON would have been downloaded to.
After running mvn compile, the next lifecycle, a lot more dependencies are downloaded, including GSON:

Triggering build of another project in maven

I just starting using maven in my new project.
I am trying to create artifacts(java files) of a project A into another project B in order to resolve their cyclic dependency.
If I run the whole build for the first time,its working fine. The jar of B contains classes of both project A and B.
However, if I make changes to only project B , and run the build, only project B is running and build is failing . Thats because sine no changes are done to project A, maven is not running it and the artifacts are also not getting generated.
Can anyone advise how can I trigger the build of a project even though no changes are done to it.
exec mvn with clean phase! i.e.
mvn clean package
Phases are actually mapped to underlying goals. The specific goals executed per phase is dependant upon the packaging type of the project. For example, package executes jar:jar if the project type is a JAR, and war:war if the project type is - you guessed it - a WAR.
An interesting thing to note is that phases and goals may be executed in sequence.
mvn clean dependency:copy-dependencies package
This command will clean the project, copy dependencies, and package the project
mvn site
This phase generates a site based upon information on the project's pom. You can look at the documentation generated under target/site.

How do I create a JAR with m2e (m2eclipse)?

I'm new to Maven and m2e. It frustrates me that I have to ask this question, but the sparse m2e documentation and Google are failing me.
How do get m2e to build a JAR? I understand that this should happen during the maven package phase, but m2e doesn't seem to do this as part of the build process and I can't find a way to explicitly execute the package phase in Eclipse (nor any other phases that aren't part of the default build).
Thanks.
As long as you have your POM.xml file with the following parameters:
<modelVersion>[a model number eg 4.0.0]</modelVersion>
<groupId>[a group id eg com.myapp]</groupId>
<artifactId>[a unique artifact id within your packages eg myapp]</artifactId>
<version>[the version number eg 1.0-SNAPSHOT]</version>
<packaging>jar</packaging>
<name>[the name eg myapp]</name>
then you just need to run maven build with the goals clean install to create a jar file from your project. You can run maven build by right clinking on the project and going to run > maven build ...
The jar will be created in [project dir]/target
Although "Run As maven install" would do the trick, it can be good
to know that m2e will perform the equivalent of the package phase when doing "Export... Jar/War/EAR file". It seems to understand the plugin configurations too, at least a little bit, and at least for EARs...
As it will resolve artifacts using projects and the m2 repository,
it will also work for "unrelated" modules, as the dependency that resolves to a project is good enough for eclipse to package.
(That is, you don't have to install the unrelated dependency separately, it will be built automatically from the eclipse project.)
I'm not sure I would deploy anything it builds though :-)

multi-module maven project

If I have 6 modules in my project is it possible to build only one out of six ? without commenting out others ?
EDIT
Submodule will not work itselft because or parent tags. I need to install the parent first to make it build. how can I do it without installing parent
is it possible to build only one out of six ? without commenting out others ?
My understanding is that you want to launch maven from the aggregating project (i.e. a reactor build) but only build one module. This is possible using the -pl, --projects project list option (see advanced reactor options):
mvn --projects my-submodule install
This is a very powerful option, especially when combined with --aslo-make (to also build the projects on which the listed modules depend) or the --also-make-dependents (to also build the projects that depends on the listed modules). On the basis of your update, you might want this actually:
mvn --projects my-submodule --also-make install
Launching Maven from the directory of the module you want to build is of course an option but this won't allow you to do the things mentioned above nor to build a subset of all modules. For such use cases, the advanced reactor options are the way to go.
Opening a command shell, navigating to the submodule directory and executing mvn install (or whatever your preferred lifecycle is) should do the trick.
You can simply build the module by going in this module directory and run the mvn clean install.
However, note that with this method, the dependencies with the others modules will be taken from your local repository (or the entreprise repository).
Let's take a simple example:
project
+ commons
+ business
Now, imagine that you build, on the root directory the whole project, using the mvn clean install command. Consider that all your modules are in version 1.0.
Now, you move to version 1.1. If you run the mvn clean install on the business project only, it will try to get the 1.1 of module commons. You will then have an error, as Maven will not find any version 1.1 in your local repository.
Within Eclipse, assuming you have m2eclipse installed: Right-click on the module in question and choose Run As -> Maven package.

Categories

Resources