Ant task to save only certain classes in JAR - with dependency checking?

I would like to know if it is possible to write an Ant task (if necessary, using some other library) in order to reduce the JAR file so that it contains only certain classes and those on which they depend.

For example, I have a JAR containing classes A, B, C, and D, where A and B also expand. C. D is independent of others. What I would like to do is point out the Ant task, which I only need class B, and ultimately get a JAR file containing only B and C.

I saw this answer , which gives an easy way to say: “I do not want A and D,” and I believe that replacing “excludes” with the words “includes” will allow me to indicate “I want B and C” as follows:

<jar destfile="stripped.jar"> <zipfileset src="full.jar" includes="path/B.class;path/C.class"/> </jar> 

... but is there a way that supports dependency so that I can say “I want B”, but end up with a JAR that contains B and its dependencies?

+4
source share
2 answers

You can take a look at the obfuscation tools, they can also often delete dead code, that is, classes, methods and the like that are not referenced anywhere in the code.

For example, you can use ProGuard .

From the documentation:

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

You can customize it to take only a drying step. In your case, you should use the -keep option to tell ProGuard that you want to use class B, and end up with B and C, as you describe. This may not be exactly what you want, since ProGuard will also remove unused methods, etc., by changing the classes in your JAR file (I don’t know a single option to disable this). In addition, you need to tell ProGuard that it does not perform any other actions that it usually performs, such as obfuscation.

If ProGuard does not meet your needs, you can look for other tools for obfuscation and / or reduction. Hope this helps, and good luck!

+3
source

As user829876 suggests , ProGuard may be one option.

Another option that may be closer to what you are looking for is GenJar . For several years there has not been much activity, but it looks like he will do what you need. Below are examples here . There are not many alternatives .

Another tool you might want to learn is Autojar . It doesn't seem to have an Ant task, but you can probably use a Java task to make this happen.

I have never used these tools, but this should not force them to try and see what works for you. If necessary, sources are available for your customization.

+3
source

Source: https://habr.com/ru/post/1399142/


All Articles