KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > make > task > JavacTask


1 /*
2  * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.make.task;
30
31 import java.util.logging.*;
32
33 import com.caucho.log.Log;
34
35 import com.caucho.vfs.Path;
36 import com.caucho.vfs.Vfs;
37
38 import com.caucho.java.JavaCompiler;
39
40 import com.caucho.make.Make;
41
42 /**
43  * Class loader which checks for changes in class files and automatically
44  * picks up new jars.
45  */

46 public class JavacTask implements Make {
47   private static final Logger log = Log.open(JavacTask.class);
48
49   private Path _srcdir;
50   private Path _dstdir;
51
52   public void setSrcdir(Path path)
53   {
54     _srcdir = path;
55   }
56
57   public void setDstdir(Path path)
58   {
59     _dstdir = path;
60   }
61
62   public void make()
63     throws Exception
64   {
65     Path dstDir = _dstdir;
66
67     if (dstDir == null)
68       dstDir = Vfs.lookup("WEB-INF/classes");
69         
70     Path srcDir = _srcdir;
71
72     if (srcDir == null)
73       srcDir = dstDir;
74
75     JavaCompiler compiler = JavaCompiler.create();
76     compiler.setClassDir(dstDir);
77
78     makeAll(compiler, srcDir, dstDir);
79   }
80
81   public void makeAll(JavaCompiler compiler, Path srcDir, Path dstDir)
82     throws Exception
83   {
84     String []list = srcDir.list();
85
86     for (int i = 0; i < list.length; i++) {
87       Path subSrc = srcDir.lookup(list[i]);
88
89       if (subSrc.isDirectory())
90         makeAll(compiler, subSrc, dstDir.lookup(list[i]));
91       else if (list[i].endsWith(".java"))
92         makeJava(compiler, subSrc, dstDir);
93     }
94   }
95
96   public void makeJava(JavaCompiler compiler, Path javaSrc, Path dstDir)
97     throws Exception
98   {
99     String tail = javaSrc.getTail();
100     Path classFile = dstDir.lookup(tail.substring(0, tail.length() - 5) + ".class");
101
102     if (javaSrc.getLastModified() <= classFile.getLastModified())
103       return;
104
105     compiler.compileIfModified(javaSrc.getPath(), null);
106   }
107 }
108
Popular Tags