Cleaning up stale .class files using Apache Ant

2019-06-24 05:14发布

问题:

How do I clean up stale .class files out of ${workdir} given set of existing .java files in ${srcdir}? By stale I mean .class files that were generated from now removed .java files. I have tried coming up with something using Ant mappers and filesets etc. but I failed. Removing all .class files older than their respective source .java files would be acceptable, too.

回答1:

The problem is determining whether a class file without an obviously corresponding source file is really stale.

Try this in a single file (A.java)

public class A{}
class B{}

This will result in both A.class and B.class. So, B.class would seem stale because of the missing java file. You'd probably get similar issues with any inner classes.

Safest bet if you want to make sure there's no old class files lying around would be just to delete them all.



回答2:

I'm pretty sure there's an ant task to kill .classes older than the .java...

Depend sounds close, and may actually do what you want, but this isn't its intended purpose. Given developmentalinsanity's answer however, this may be the only thing that will Actually Work.



回答3:

As it’s just not possible to detect what’s stale and what’s not, most builds have a clean target (that’s also part of cleanbuild). The clean target, just removes all files from you’re build directory. This directory normally is unversioned (svn:ignore).

Not all files in you’re build will be the result of the compiler, for example .property files, these files can be stored in an alternative directory that will be copies in to the build directory. For example in a web application build you can store those files in /web/WEB-INF/classes and let Ant copy them into the build directory.



标签: java ant