Hi!

The project I am working at have some old code which does not respect
common java source code organization with directory per package: some
top level packages are skipped. So for example class
com.somecompany.somepackage.someclass is located in
src/somepackage/somclass.java not
src/com/somecompany/somepackage/someclass.java.

This indeed confuse JavaC task, since it could not find appropriate
.class files. I know the right way to handle this problem would be to
reorganize the code to use right directory structure, but it is
impossible to do yet because of some organisational issues.

I've modified JavaC task. Now you can specify <mapper> element
within it which will control how .java files are mapped to .class files.
If one it ommited it behaves exactly like it was behaving before:
mapping *.java to *.class.

So for example the problem I've described above is solved by following 
construct:

<javac debug="on" deprecation="on" srcdir="${src}" destdir="${dst}">
<mapper type="glob" from="*.java" to="${dst}/com/somecompany/*.class"/>

The problem seems to be pretty common. It is even mentioned in ANT FAQ 
under "why my .class files are always recompiled" and I've seen some
similar questions in mailing lists.

The patch is attached. I think this is pretty generic modification which
I suggest to include into future versions of JavaC task. Meanwhile, for
internal use, I am going to make custom task, extending JavaC with this
new syntax.

Sincerely,
Vadim


-- 
"La perfection est atteinte non quand il ne reste rien a ajouter, mais
quand il ne reste rien a enlever."  (Antoine de Saint-Exupery)

diff -ur jakarta-ant-1.4.1.orig/src/main/org/apache/tools/ant/taskdefs/Javac.java jakarta-ant-1.4.1/src/main/org/apache/tools/ant/taskdefs/Javac.java
--- jakarta-ant-1.4.1.orig/src/main/org/apache/tools/ant/taskdefs/Javac.java	Thu Oct 11 06:58:29 2001
+++ jakarta-ant-1.4.1/src/main/org/apache/tools/ant/taskdefs/Javac.java	Fri Feb  8 21:23:54 2002
@@ -122,6 +122,7 @@
 
     protected boolean failOnError = true;
     protected File[] compileList = new File[0];
+    protected Mapper mapperElement = null;
 
     private String source;
     
@@ -572,16 +573,41 @@
         compileList = new File[0];
     }
 
+
+    public Mapper createMapper() throws BuildException {
+        if (mapperElement != null) {
+            throw new BuildException("Cannot define more than one mapper",
+                                     location);
+        }
+        mapperElement = new Mapper(project);
+        return mapperElement;
+    }
+
+    /**
+     * Returns file mapper to be used. It is either mapper defined by
+     * user, or default glob mapper *.java - *.class
+     */
+    public FileNameMapper getMapper() 
+    {
+        if(mapperElement != null) 
+        {
+            return mapperElement.getImplementation();
+        } else 
+        {
+            GlobPatternMapper gm = new GlobPatternMapper();
+            gm.setFrom("*.java");
+            gm.setTo("*.class");
+            return gm;
+        }
+    }
+
     /**
      * Scans the directory looking for source files to be compiled.
      * The results are returned in the class variable compileList
      */
     protected void scanDir(File srcDir, File destDir, String files[]) {
-        GlobPatternMapper m = new GlobPatternMapper();
-        m.setFrom("*.java");
-        m.setTo("*.class");
         SourceFileScanner sfs = new SourceFileScanner(this);
-        File[] newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m);
+        File[] newFiles = sfs.restrictAsFiles(files, srcDir, destDir, getMapper());
 
         if (newFiles.length > 0) {
             File[] newCompileList = new File[compileList.length +

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to