I have a project which has a large number of classes(>1000) which are archived into 7 jars. All these jars are built separately (using ant). If some one has changed one of these classes, there may be numerous other classes that depends on this one, and all those will fail runtime. How can i check the dependency to a class file from other jars?
Thank you.
Your best bet is to use an IDE like Eclipse or NetBeans. There are many ways that an IDE can help you with this issue.
If you don't have the source code or source jars for the projects, you should create some integration tests that can assist you in regression testing (actually, most will say that you should do this anyway). You can start by looking at JUnit. After a change, if any test fails, you know that you will need to look into the usage of the class/API.
If you utilize something like NetBeans you could have all the projects open and use 'Find Usages'.
You could use the Class Dependency Analyzer tool.
Related
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.
While downloading Google Guice I noticed two main "types" of artifacts available on their downloads page:
guice-3.0.zip; and
guice-3.0-src.zip
Upon downloading them both and inspecting their contents, they seem to be two totally different "perspectives" of the Guice 3.0 release.
The guice-3.0.zip just contains the Guice jar and its dependencies. The guice-3.0-src.zip, however, did not contain the actual Guice jar, but it did contain all sorts of other goodness: javadocs, examples, etc.
So it got me thinking: there must be different "configurations" of jars that get released inside Java projects. Crossing this idea with what little I know from build tools like Ivy (which has the concept of artifact configurations) and Maven (which has the concept of artifact scopes), I am wondering what the relation is between artifact configuration/scope and the end deliverable (the jar).
Let's say I was making a utility jar called my-utils.jar. In its Ivy descriptor, I could cite log4j as a compile-time dependency, and junit as a test dependency. I could then specify which of these two "configurations" to resolve against at buildtime.
What I want to know is: what is the "mapping" between these configurations and the content of the jars that are produced in the end result?
For instance, I might package all of my compile configuration dependencies wind up in the main my-utils.jar, but would there ever be a reason to package my test dependencies into a my-utils-test.jar? And what kind of dependencies would go in the my-utils-src.jar?
I know these are a lot of tiny questions, so I guess you can sum everything up as follows:
For a major project, what are the typical varieties of jars that get released (such as guice-3.0.zip vs guice-3.0-src.zip, etc.), what are the typical contents of each, and how do they map back to the concept of Ivy configurations or Maven scopes?
The one you need to run is guice-3.0.zip. It has the .class files in the correct package structure.
The other JAR, guice-3.0-src.zip, has the .java source files and other things that you might find useful. A smart IDE, like IntelliJ, can use the source JAR to allow you to step into the Guice code with a debugger and see what's going on.
You can also learn a lot by reading the Guice source code. It helps to see how developers who are smarter than you and me write code.
I'd say that the best example I've found is the Efficient Java Matrix Library at Google Code. That has an extensive JUnit test suite that's available along with the source, the docs, and everything else that you need. I think it's most impressive. I'd like to emulate it myself.
I develop a little java utility library using maven. Now I'd like to add some demo / sample code to show how to use the library.
Where is the best place to put it?
In a sub-package with the other code. I don't like this since it means the demos will be included in the library jar file.
In a new maven artifact. That works, but I'd prever to have the demos closer connected to the library source.
As a sub-artifact. Haven't tried this yet. Seems to make everything a bit complex for something that should be simple.
Is there any common pattern to do this?
If it's some sample code snippets that run by themselves and just demonstrate how to use the library, then write them as unit tests, in the same module.
If it's more like a separate demo application (that a user might even interact with), then create a separate artifact. It's the standard way of doing it. If you really want to you could put it in the same module, but in a different source directory, but that's just making it harder on yourself.
Your library and you demo should probably share a parent module (of type "pom", not "jar" like the others), giving you a multi-module project. Then you can build both by launching maven from this parent module.
If you want to release your library and demo together (you can, but you don't have to), you can do that from the parent too.
In other words, it's not because they are separate modules, packaged in different artifacts, that they cannot be closely connected anymore. The different modules of a multi-module project still form one whole project.
You hadn't written what kind of utility library it is, but if it's something like apache commons, then most of the demos can be written as JUnit tests, which are placed in the same artifact. Good designed JUnit tests both tests your code and provide example how to use your utilities.
I prefer a new maven artifact, it can make your own artifact clean
I would recommend create a maven multi-module project where one module is the core and one module is the demo code. That way the user can choose if he wants to create both modules (they become separate artifacts) or if he just wants the core.
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 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