How to copy maven dependency list from Eclipse as text - java

In Eclipse, when I open my pom.xml file and go to the "Dependency Hierarchy" tab, it shows two windows: Dependency Hierarchy and Resolved Dependencies.
Is there a way to copy/paste or export the list of dependencies shown in the Resolved Dependencies window into a text file, or a way to have maven print out an equivalent list on the command line?
Background: our team has been told we need to provide a list of all our dependencies and their versions, and it'd be a lot faster if there's a way to easily get a textual list of them vs having to manually type out each dependency and its version one by one into a spreadsheet. We have a lot of services and they all have a ton of dependencies when you need to list out the entire dependency tree.

You can try mvn dependency:tree -Doutput=<path_to_file> to get dependencies in a file.

It's more than just a list, but if you generate a Site it's all nicely documented. Just run
mvn site:site
inside the directory of your POM. You'll find the Site under target/site.
Little suggestion:
If your company wants to track the dependencies of applications, you shouldn't rely on lists or the like. Have a look into things like the dependency-check-maven plugin to scan for vulnerabilities during development and SBOM to track dependencies later.

Related

How to find java classes that needs a dependency in maven pom.xml file

I have a Maven based project, and I use IntelliJ. The pom.xml file probably contains dependencies that I don't need. How can I find which Java files (in particular the import statements) that need a specific dependency in the pom.xml file? Alternatively, how can I find which dependencies I don't need in the pom.xml?
I have tried to comment out a dependency from pom.xml, build the project and look what breaks. In at least one case, I saw no compile time problems, but there was a runtime problem. This method is also more effort than I want.
I have also tried to find information in the IntelliJ Project explorer, section "External Libraries". But the items listed there are not always present in the pom.xml file. Each versioned item there expands to a tree with a jar file on top, and I can ask IntelliJ about the usage of the contained items. I have found the usage of some packages contained in jar files, but the number of packages to investigate simply becomes too large.
Here is a dependency that I want to know if I need or not:
<!-- https://mvnrepository.com/artifact/com.googlecode.soundlibs/mp3spi -->
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>mp3spi</artifactId>
<version>1.9.5.4</version>
</dependency>
This particular dependency results in three items in the External Libraries list (there are two sub dependencies apparently). Asking IntelliJ for usage of these libraries, I can't find any usage in my own files. But if I remove the dependency from the pom.xml file, I get runtime problems.
Maven offers you the dependency:analyze goal which gives you the artifacts that are declared in your pom but not used by any part of your source code. Beware, though, that there may be dependencies that are only used at runtime.

OpenIMAJ Jar Files

I am new to openIMAJ and I want to process some pictures using it. There are a lot of tutorial available but they all tell using Maven. Does anyone know from where I can download the jar files of openIMAJ to directly use in my Java project?
Thanks!
For all the features of OpenIMAJ there are more than 50 Jar file that you need to Download individually. Also if you somehow manage to find all those files manually on internet you may end up mixing some or other version which will make some classes incompatible. Although I would strongly suggest you to try understand what maven is and its capabilities, you can follow the below steps if you dont want to add dependencies using maven or even don't even want to know what it is.
1.) Go to Help Menu -> Open MarketPlace.
2.) Search for maven and download the plugin.
3.) In file menu create new-> new maven project
4.) Select your workspace and click next
5.) In the select an Archetype window look for add archetype and enter the following details
GroupID: org.openimaj
ArtifactID: openimaj-quickstart-archetype
version: 1.1
URL: http://maven.openimaj.org/
6.) Click next and give details of your own project and click finish
All the jars will be downloaded which you can see in your project structure. You can now stop worrying about maven and start concentrating on openimaj.
OpenIMAJ is rather complex and contains a lot of modules that you probably don't need to use in your project (i.e. if your making something to do image processing, you probably don't care to much about audio analysis, or content analysis of web-pages). In addition each of the OpenIMAJ modules has dependencies on numerous other projects (which themselves have dependencies, and so on). For these reasons, it isn't really all that practical to provide direct downloads of all the modules and their dependencies as it would take an incredible effort for the user to try and figure out which bits are needed and which bits are not.
The ideal way to specify which bits of OpenIMAJ you need is to use an automatic dependency management system; this does not need to be Maven however - any Maven compatible dependency manager will work (i.e. Ivy, SBT, Grape, etc...). There are examples of the snippets you need to add to your build system configuration for these on the OpenIMAJ front page in the box on the right (you might need to scroll down a bit).
If you really do want to manually include the relevant jar files in your existing project, I'd recommend the following approach, which uses Maven to build a customised set of jars based on the exact OpenIMAJ modules you need:
Run mvn -DarchetypeRepository=http://maven.openimaj.org -DarchetypeArtifactId=openimaj-quickstart-archetype -DarchetypeGroupId=org.openimaj -DarchetypeVersion=1.2.1 -DartifactId=oi-deps -DgroupId=oi-deps -Dversion=1.0-SNAPSHOT -DinteractiveMode=false archetype:generate to create a basic OpenIMAJ project (called oi-deps in this case).
Go to the oi-deps directory: cd oi-deps
Edit the pom.xml file to include only the bits of OpenIMAJ you need by removing any unnecessary <dependency> sections.
Run mvn dependency:copy-dependencies. This will create a target/dependencies folder that contains all the jars you need to add to your project.
I also didn't find any 'download all' site. And I think the reason is that there are a lot of dependences in some jar files.
I extracted some jars recently to use in a project without maven but that was quite wiry.
And I was only able to do this using another project with maven.
I think maven is good stuff and easy to use within ie. netbeans. So give it a try.

How to find which dependency current jar file is referred?

In Eclipse, expand current project in project explorer,
click Java Resources -> libraries -> Maven Dependencies
And I found there are two jar files which in different version, i.e.:
commons-lang-2.1.jar
and
commons-lang3-3.1.jar
but from pom.xml, I cannot tell where commons-lang-2.1.jar come from as it must required by one of the artifacts. But it is too much trouble to check pom file from each artifact...
I heard of dependency-tree can work something similar but don't know how to make it work under this situation. Any hint is appreciated.
mvn dependency:tree
Look for the two instances of commons-lang and see what requires them.
That said, the two versions aren't compatible, and can live in the same application:
http://commons.apache.org/proper/commons-lang/article3_0.html
See this answer of mine regarding deciphering the tree output. The nutshell is that indented libraries are dependencies of the non-indented library above it: a dependency tree.
execute
mvn dependency:tree
if you have very giant dependency tree and hard to read you can use
mvn dependency:tree -Dincludes=org.apache.commons:commons-lang3
to just include occurrence of this artifact
from your pom.xml it will list out all the dependencies being pulled directly or indirectly, and put the <exclusion> to avoid consumption of non desired library

How check if dependency is used by any of my classes and list them in Maven?

I want to do some clean up in POM.XML. How can I check which dependencies aren't used at all by my code and if one is used then how tell which one is it?
Of course I don't want to simply delete dependency and then search for errors in IDE, because that could take ages when pom has got about 80+ dependencies.
I'm using SpringSource Tools Suite version of Eclipse.
Greetz
Have a look at the Maven Dependency Plugin. When running, it should list the dependencies that you declared but do not use, but also which intransitive dependencies you use without explicitly declaring them. Note that it you use reflection, the report may not be accurate.

Migrating from ant to maven in Netbeans

Our software is written in Java and comprise many (7) projects.
These projects are Netbeans ant projects.
I'm considering to converting them to maven2.
Where can I find some hints for doing such thing?
Don't read that book. It will only make you confused. Read this book instead: "Maven - The definitive guide" http://www.sonatype.com/books/maven-book/reference/ .
Also, the maven site has a lot of information, but the structure is terrible so you'll need to use google to navigate in it.
Here is my suggestion:
Do this by hand, not with "automagic" "help" from the IDE. Maven integration doesn't work that good yet, not in any IDE.
Make sure you program project is divided into modules under a common umbrella module, so that each module produces a single binary artifact (jar, war,...) possibly accompanied by the javadoc of the source code behind that artifact, a zip with the source code etc. The basic principle is that each module produces a single artifact, containing all the non-test-code under that module. You can do this while the project is still built by ant.
Each module should conform to the standard maven directory layout. The build destination is under [module]/target/[output-type, e.g. "classes"]. The source code is under [module]/src/main/[src-type e.g. "java"] and [module]/test/[src-type]. The artifact consists of all the code under src/main, and none of the code under src/test, as it built to the target directories. You can do this while the is still built by ant.
Start by transforming the sub-module that has no dependencies on other modules in the project.
Now you can create the parent maven module pom.xml with artifact type "pom", consisting of one of the modules below. Make a child module for the first submodule (the one with only external dependencies), using the umbrella module as "parent". Remember that you need to specify version for the parent. Remember to add the child module as a "module" in the parent too. Always use ${project.version} as version in the child modules when you create multi-module projects like this. All modules under a parent must be released simultaneously in a single operation, and if you use this setting maven will make sure the version fields stay the same across all modules and gets updated everywhere during the release. This may make it difficult to re-use the existing numbering scheme, but that doesn't matter. You are never going to run out of version numbers anyway.
Add the necessary dependencies, and make sure you can build the parent and the child module together using the command "mvn clean install" from the parent module.
Proceed with the rest of the modules the same way. Dependencies to other modules under the same parent project should also use ${project.version} as the "version" they are depending on, meaning "the same version as this". NOTE THAT in order to build, the module you are depending on must be built using "mvn install", so that it gets deployed to you local (computer) repository. Otherwise the depending module will not be able to find the classes. There are NO source-code dependencies between modules in maven, only dependencies to built and packed versions installed in local and remote repositories. This can be very confusing if you come from ant-projects. Build from the root module until you get comfortable with this. It takes two days.
Don't use maven integration in IDEs. It is a bad idea. Use "mvn idea:idea" or "mvn eclipse:eclipse" to set up your workspace as a non-maven ordinary IDE project. The inter-module dependencies mechanisms in maven and the IDE aren't identical and will never be. Also, if you have several mavenized projects with dependencies in between, you want to have several of these in your workspace with dependencies set up between. You can do this with mvn idea:idea / eclipse:eclipse if you create a separate maven project file called "workspace.xml" (or whatever) in the same directory as parent module, set up as a multi-module project containing modules "." and "../otherproject" (only one-way reference here, no parent ref back). If you run "mvn idea:idea / eclipse:eclipse -f workspace.xml" you get a workspace with all these modules linked together. No IDE integration lets you do that. This sound like a lot of extra work, but the workspace.xml-file is really small. It doesn't have to contain all that dependency stuff and all that, only the reference to the modules you want to bind together in your IDE.
I did a succeful migration of NetBeans Ant project to Maven project using the instruccions by Joseph Mocker here: http://forums.netbeans.org/ptopic55953.html
I cite the important part:
close the project
rename the build.xml, nbproject files/folders to something so NB won't recognize them.
close and restart NB (so any memory cache knowledge of the project is gone)
copy in an empty pom from some other project.
open the project back up in NB (NB should now identify it as a maven project)
rearrange the files to follow the maven way (™)
This won't be an easy task since Maven2 expects the files to be organized in a specific way. Anyway Better Builds with Maven is a free book that should get you started. It will help you understand Maven and it also has a chapter on migration.
I discovered that the migration is not necessary. The real requirements that I need was automatic download of dependencies (libraries).
This is also achieved by Ivy which nonetheless uses maven repositories.
I solved converting project from ant to ant+ivy with IvyBeans.
I have built a script to migrate Ant builds to Maven. You can find more information here:
https://github.com/ewhauser/ant2maven
It won't help you with fixing your directory structure and or any additional Ant tasks, but it removes a lot of the tedious steps to get started.

Categories

Resources