We have a multiwar project and with various people working on the project a lot of times, lots of unused jars are still specified as dependencies, is there any way , to check which jars are not used at all, but nonetheless, are being referred to as a dependency?
I hope maven already has support for something like this, considering its such a powerful tool.
Run mvn dependency:analyze. It should do the work.
Related
I am very new to the industry, so apologies in advance for the very likely stupidity of the question.
In the team, we work with Intellij IDEA 13 as IDE, and use Maven 3 for our projects. We provide a few online services and portals, and I'm just starting to work on one: the project has several dependencies that are shared by other older projects, some are JAR archives, some are WARs...
To my questions re: how to edit those dependencies locally (e.g. editing a resources.properties was the case I had in mind) my tutor suggested turning the dependencies into snapshots and work with those.
What I managed to do was create a copy of the appropriate folders in my local repository and change wherever the version of the dependency was in the name or in the files, then modify my pom.xml files.
Now, this works perfectly if I open the JAR/WAR and edit some file, but I'd like to be able to do it from my IDE, also cause not being able to suggests I'm probably doing this in a wrong way. Do I need to somehow unpack the dependency to be able to do so? Is my entire approach wrong?
P.S.: I would ask someone in my office, but oddly enough none of those who could help are at work today anymore!
If none of your colleagues was able to help you, I am afraid there might be something else hidden.
However, let's try it!
I am guessing, here, that your resources.properties is a part of his own project. Project handled by Maven and expressed as a dependency in one of your main project.
I am also guessing that your main projects are WARs (Webapps mostly, services, portals) and the JARs are libraries, configurations, etc...)
Therefore, I am guessing that your webapps are referencing some libraries as Maven dependencies, to a specific version.
That said, IntelliJ (and other IDE) can easily handle modification of either JARs and WARs related to each other via Maven as long as the visioning is meaningful.
Note: Having -SNAPSHOT at the end of the version number tell to Maven NOT to cache the package. On the opposite, a definitive version number is considered as released and is only fetched from the cache. This is important because with a SNAPSHOT, you can publish an illimited number of time and it is guaranteed to have the latest version.
Note: Doing mvn clean install publish a package into your local Maven repository (generally located in ~/.m2) and is only available to you.
The general good practice is to have, in all the development branches of your DVCS, all your owned, often modified projects (Don't be too greedy, it depend on the situation) as SNAPSHOT. And during a release (Maven has a specific plugin for that) change all the versions to a final one, attributed in this precise moment (You never know if you will need a minimal version or a major).
Your code, then, has always the SNAPSHOT number of your expected next release.
Finally, I think that in your case, if you choose to change the pom.xml of one of your library for a SNAPSHOT, you should change the pom.xml of the root project to correspond.
If this dependency version is the same, then, you can add your library as a module within IntelliJ and the IDE will do the math to figure that the Maven dep and the Java module are the same entity.
I don't even know if that's help you (I'm not even sure if it's clear), but I hope it will make you ask more questions about what you need. Your co-workers will probably be able to help you more.
I have found useful tools to do this in eclipse like Classpath Helper, but is there any plugin or way in intellij idea to find unused jar files ?
I'm sure you know how to use the IDEA Dependency Analysis tool to find library dependencies, and while that could get you some of the way it's of course not enough.
The problem and fact is that there is no way of finding unused JARs by way of static analysis, since it is possible to refer to and create any class in any dependency by way of reflection/IoC/whatnot these days.
So, the best you can "hope" for is a tool that can tell which JARs are not explicitly referenced by your code - curiously that is absent from IDEA as far as I know (but I would love to be corrected on that point!)
Cheers,
You should really be using maven and determining each module's dependencies manually. Classpath Helper is an astonishingly bad way to tell what jars you're using.
What is the easiest way to see the libraries dependencies in Java project (eclipse)?
I am using Spring MVC and Hibernate so right now there are a lot of jar files and I even do not remember which one of them are responsible for what.
check out tattletale.
http://www.jboss.org/tattletale
See them how?
If you're using maven, use the dependency plugin's dependency:tree to get a hierarchical representation of what depends on what.
If you're not, ew; manual management of transitive dependencies sucks! You can use something like Dependency Finder or JDepend to provide similar info.
Not sure if this is what you mean, but to start with you can right-click the project (in Eclipse) and look at Properties -> Java Build Path. The Libraries tab should list what libraries you're using on your build path. (But you probably knew that.)
If it's a simple standalone project, you could of course always remove a library and see what interesting new errors pop up ;-)
For more complicated projects with interdependencies, it can take quite a bit of fiddling to get all your dependencies right. I generally recommend setting up a "core" project which holds (and exports) most of your third-party JARs (better yet, use user libraries, and putting that project on the build path of your other projects.
Edit after reading your comment: Ah, gotcha... you might be interested in the Plug-in Dependency Visualisation incubator project then - haven't used it myself but it sounds like it could do what you're after. Hope that helps!
In a bigger project we might be using tons of JARs. How do I find out which JARs are being used by a certain module/package in the project (not the whole project). Any tool, technique, etc?
Bigger projects typically use a build tool like maven or ant. maven has the maven dependency plugin to list the dependencies for a particular project which you invoke by mvn dependency:list. In case of ant, it depends on the way the build script is written.
Maybe you should give more details about your project environment and you may get better answers.
The various code obfuscators, reducers etc can help.
Check out:
http://jarg.sourceforge.net/
http://www.alphaworks.ibm.com/tech/jax/
http://www.e-t.com/jshrink.html
http://proguard.sourceforge.net/
http://www.fightingquaker.com/jaropt/
btw, this list was acquired from http://www.javakb.com/Uwe/Forum.aspx/java-programmer/34365/How-to-create-a-JAR-that-contains-only-the-class-files
In Java if you package the source code (.java) files into the jar along with classes (.class) most IDE's like eclipse will show the javadoc comments for code completion.
IIRC there are few open-source projects that do this like JMock.
Lets say I have cleanly separated my API code from implementation code so that I have something like myproject-api.jar and myproject-impl.jar is there any reason why I should not put the source code in my myproject-api.jar ?
Because of Performance? Size?
Why don't other projects do this?
EDIT: Other than the Maven download problem will it hurt anything to put my sources into the classes jar to support as many developers as possible (maven or not)?
Generally because of distribution reason:
if you keep separate binaries and sources, you can download only what you need.
For instance:
myproject-api.jar and myproject-impl.jar
myproject-api-src.jar and myproject-impl-src.jar
myproject-api-docs.zip and myproject-impl-docs.zip
Now, m2eclipse - Maven for Eclipse can download sources automatically as well
mvn eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true
Now, it can also generate the right pom to prevent distribution of the source or javadoc jar when anyone declare a dependency on your jar.
The OP comments:
also can't imagine download size being an issue (i mean it is 2010 a couple 100k should not be a problem).
Well actually it (i.e. "the size) is a problem.
Maven suffers already from the "downloading half the internet on first build" syndrome.
If that downloads also sources and/or javadocs, that begins to be really tiresome.
Plus, the "distribution" aspect includes the deployment: in a webapp server, there is no real advantage to deploy a jar with sources in it.
Finally, if you really need to associate sources with binaries, this SO question on Maven could help.
Using maven, attach the sources automatically like this:
http://maven.apache.org/plugins/maven-source-plugin/usage.html
and the javadocs like this:
http://maven.apache.org/plugins/maven-javadoc-plugin/jar-mojo.html
That way they will automatically be picked up by
mvn eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true
or by m2eclipse