How do I automate the assembly of a Java class and all the classes it depends on?

I guess this is the question of the next question of question 1522329 .

This question was talking about getting a list of all the classes used at runtime using the java -verbose: class option.

What I'm interested in is automating the build of the JAR file that contains my classes and all the other classes they rely on. Typically this will be where I am using code from a third party open source "client logic" product, but they did not provide a clean set of client API objects. Their full set of code goes to the server, but I only need the required client bits.

This may seem like a common problem, but I haven't seen anything (for example in Eclipse) that helps with this. Did I miss something?

Of course, I can still do it manually: by biting the bullet and including all third-party code in a massive JAR (insulting my purist sensitivity) / walkthrough / probe and error / -verbose: class type (but the latter won't work where, say, my code works as part of a J2EE servlet and so I only want to see this for a given Tomcat webapp and ideally only for the classes associated with my classes in it).

+2


source to share


3 answers


I would recommend using a build system like Ant or Maven. Maven is designed with Java in mind and this is what I use pretty much exclusively. You can even bundle Maven (using the build plugin) all the dependent classes into one big jar file, so you don't have to worry about dependencies.

http://maven.apache.org/

Edit:



As far as servlet is concerned, you can also define what dependencies you want to package along with your jar, and if you are building a standalone application you can force the jar tool to create an executable jar.

Note: yes, I'm a bit of a Maven proponent as it made the project easier to work with. No, I personally don't work on a project. :)

+5


source


Take a look at ProGuard .



ProGuard is a free Java file compression tool, optimizer, obfuscator and preperser. It detects and removes unused classes, fields, methods and attributes. It optimizes the bytecode and removes unused instructions. It renames the rest of the classes, fields and methods using short meaningless names. Finally, it predefines the rendered code for Java 6 or Java Micro Edition.

+2


source


What you want is not only the inclusion of the classes you rely on, but also the classes you rely on. And so on, etc.

So this is not a build issue, but a more dependent one. To answer your question, you can solve this with Maven (apparently) or Ant + Ivy.

I work with Ivy and sometimes I create a "ueber-jar" using the zipgroupfileset functionality of the Ant Jar task. Not very elegant would say some, but it happened after 10 seconds :-)

+1


source







All Articles