Clean up obsolete .class files with Apache Ant

How to clean obsolete .class files from ${workdir} given set of existing .java files in ${srcdir} ? By default, I mean .class files that were generated from now-deleted .java files. I tried to come up with something with Ant mappers and file sets, etc., but I failed. Deleting all .class files older than their corresponding .java source files would also be acceptable.

+4
source share
3 answers

The problem is determining if the class file without the explicitly matching source file is really obsolete.

Try this in one file (A.java)

 public class A{} class B{} 

This will result in both A.class and B.class. Thus B.class looks deprecated due to missing java file. You will probably get similar problems with any inner classes.

It’s a safe bet if you want to make sure that the old class files lying around will not just delete them.

+2
source

I am sure there is an ant task to kill .classes older than .java ...

Depend sounds close and can actually do what you want, but that is not its target. However, given the importance of development, this may be the only thing that will actually work.

+1
source

Since it is simply impossible to detect what is outdated and what is not, most collectors have a clean goal (also part of clean buildings). A clean target, simply removes all files from the youre build directory. This directory is usually not converted (svn: ignore).

Not all files in the youre assembly will be the result of the compiler, for example .property files, these files can be saved in an alternative directory that will be copied to the assembly directory. For example, in a web application assembly, you can store these files in / web / WEB -INF / classes and Ant copy them to the assembly directory.

+1
source

All Articles