From 8947bdbc21d3439529ce0f424f5224c500bc7958 Mon Sep 17 00:00:00 2001 From: Conor MacNeill Date: Thu, 6 Jul 2000 12:30:50 +0000 Subject: [PATCH] Add support for multiple source paths to Javac These can either be specified as path strings or nested elements. The path strings use the same separator conventions as other paths in Ant. When using nested elements, each component can be specified in a separate element. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267739 13f79535-47bb-0310-9956-ffa450edef68 --- docs/index.html | 49 ++++++- .../org/apache/tools/ant/taskdefs/Javac.java | 127 +++++++++++++++--- 2 files changed, 155 insertions(+), 21 deletions(-) diff --git a/docs/index.html b/docs/index.html index b80d23c62..7ba5db927 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1572,6 +1572,7 @@ the one that is currently running Ant.

fork="yes" jvmargs="-Xrunhprof:cpu=samples,file=log.txt,depth=3" /> +

Javac

Description

@@ -1614,7 +1615,7 @@ relative to the srcdir directory.

srcdir location of the java files. - Yes + Yes, unless nested <src> elements are present. destdir @@ -1695,6 +1696,30 @@ relative to the srcdir directory.

No + +

Parameters specified as nested elements

+Being a directory based task, Javac supports the <include> and +<exclude> nested elements common to a number of Ant tasks. Javac also supports a +nested <src> element. This is used to specify multiple source paths. +Multiple src elements may be present and each specifies a source path which will be +searched by Javac for classes to compile. + +

src

+Define a source path to be search for source files. +

Parameters

+ + + + + + + + + + + +
AttributeDescriptionRequired
srcA source path. It may contain multiple elements separated with either a ';' or a ':'Yes
+

Examples

  <javac srcdir="${src}"
          destdir="${build}"
@@ -1720,6 +1745,26 @@ Only files under mypackage/p1 and mypackage/p2 are
 used. Files in the mypackage/p1/testpackage directory are excluded
 form compilation and copy.

+
  <javac srcdir="${src}:${src2}"
+         destdir="${build}"
+         includes="mypackage/p1/**,mypackage/p2/**"
+         excludes="mypackage/p1/testpackage/**"
+         classpath="xyz.jar"
+         debug="on"
+  />
+ +

is the same as the previous example with the addition of a second source path, defined by +the propery src2. This can also be represented using nested elements as follows + +

  <javac destdir="${build}"
+         classpath="xyz.jar"
+         debug="on">
+    <src path="${src}" />
+    <src path="${src2}" />
+    <include name="mypackage/p1/**,mypackage/p2/**" />
+    <exclude name="mypackage/p1/testpackage/**" />
+  </javac>
+

Javadoc/Javadoc2

Description

@@ -2028,7 +2073,7 @@ instead.

-

Parameters specified as subelements

+

Parameters specified as nested elements

Two parameters of the Javadoc task may be specified as nested elements of the Javadoc task element: link and group. When present, there can be any number of each of these elements. diff --git a/src/main/org/apache/tools/ant/taskdefs/Javac.java b/src/main/org/apache/tools/ant/taskdefs/Javac.java index 99766aa7c..59dd8610b 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Javac.java +++ b/src/main/org/apache/tools/ant/taskdefs/Javac.java @@ -88,13 +88,26 @@ import java.util.*; public class Javac extends MatchingTask { + public class SourcePathElement { + private String path; + + public void setPath(String path) { + this.path = path; + } + + public String getPath() { + return path; + } + }; + /** * Integer returned by the "Modern" jdk1.3 compiler to indicate success. */ private static final int MODERN_COMPILER_SUCCESS = 0; - private File srcDir; + private Vector srcPathElements = new Vector(); + private Vector srcDirs= new Vector(); private File destDir; private String compileClasspath; private boolean debug = false; @@ -109,10 +122,48 @@ public class Javac extends MatchingTask { protected Hashtable filecopyList = new Hashtable(); /** - * Set the source dir to find the source Java files. + * Create a nested element for multiple source path + * support. + * + * @return a nexted src element. + */ + public SourcePathElement createSrc() { + SourcePathElement element = new SourcePathElement(); + srcPathElements.addElement(element); + return element; + } + + /** + * Add a single directory to the collection of directories that + * make up the source path. + * + * @param srcDirName the name of the directory to add to the list of source directories. + */ + private void addSrcDir(String srcDirName) { + srcDirs.addElement(project.resolveFile(srcDirName)); + } + + /** + * Add a set of source directories specified as path. + * + * @param srcPath the list of source directories. + */ + private void addSrcPath(String srcPath) { + // use a Path tokenizer to find the paths and add them to + // the vector of source paths. + PathTokenizer tokenizer = new PathTokenizer(srcPath); + while (tokenizer.hasMoreTokens()) { + addSrcDir(tokenizer.nextToken()); + } + } + + /** + * Set the source dirs to find the source Java files. */ - public void setSrcdir(String srcDirName) { - srcDir = project.resolveFile(srcDirName); + public void setSrcdir(String srcPath) { + // clean out the list of source dirs + srcDirs = new Vector(); + addSrcPath(srcPath); } /** @@ -188,24 +239,38 @@ public class Javac extends MatchingTask { public void execute() throws BuildException { // first off, make sure that we've got a srcdir and destdir - if (srcDir == null) { + // process the source elements into the srcDirs collection + for (Enumeration e = srcPathElements.elements(); e.hasMoreElements(); ) { + SourcePathElement element = (SourcePathElement)e.nextElement(); + addSrcPath(element.getPath()); + } + + if (srcDirs.size() == 0) { throw new BuildException("srcdir attribute must be set!"); } - if (!srcDir.exists()) { - throw new BuildException("srcdir does not exist!"); + + for (Enumeration e = srcDirs.elements(); e.hasMoreElements(); ) { + File srcDir = (File)e.nextElement(); + if (!srcDir.exists()) { + throw new BuildException("srcdir " + srcDir.getPath() + " does not exist!"); + } } + if (destDir == null) { throw new BuildException("destdir attribute must be set!"); } - // scan source and dest dirs to build up both copy lists and + // scan source directories and dest directory to build up both copy lists and // compile lists + resetFileLists(); + for (Enumeration e = srcDirs.elements(); e.hasMoreElements(); ) { + File srcDir = (File)e.nextElement(); + DirectoryScanner ds = this.getDirectoryScanner(srcDir); - DirectoryScanner ds = this.getDirectoryScanner(srcDir); - - String[] files = ds.getIncludedFiles(); + String[] files = ds.getIncludedFiles(); - scanDir(srcDir, destDir, files); + scanDir(srcDir, destDir, files); + } // compile the source files @@ -258,6 +323,14 @@ public class Javac extends MatchingTask { } } + /** + * Clear the list of files to be compiled and copied.. + */ + protected void resetFileLists() { + compileList.removeAllElements(); + filecopyList.clear(); + } + /** * Scans the directory looking for source files to be compiled and * support files to be copied. The results are returned in the @@ -266,9 +339,6 @@ public class Javac extends MatchingTask { protected void scanDir(File srcDir, File destDir, String files[]) { - compileList.removeAllElements(); - filecopyList.clear(); - long now = (new Date()).getTime(); for (int i = 0; i < files.length; i++) { @@ -372,6 +442,25 @@ public class Javac extends MatchingTask { } + /** + * Get the list of source directories separated by a platform specific + * path separator. + * + * @return the current source directories in a single path separated using the + * platform specific path separator. + */ + private String getSourcePath() { + String sourcePath = ""; + for (Enumeration e = srcDirs.elements(); e.hasMoreElements(); ) { + File srcDir = (File)e.nextElement(); + if (sourcePath.length() != 0) { + sourcePath += File.pathSeparator; + } + sourcePath += srcDir.getAbsolutePath(); + } + return sourcePath; + } + /** * Peforms a copmile using the classic compiler that shipped with * JDK 1.1 and 1.2. @@ -391,11 +480,11 @@ public class Javac extends MatchingTask { // Just add "sourcepath" to classpath ( for JDK1.1 ) if (Project.getJavaVersion().startsWith("1.1")) { argList.addElement(classpath + File.pathSeparator + - srcDir.getAbsolutePath()); + getSourcePath()); } else { argList.addElement(classpath); argList.addElement("-sourcepath"); - argList.addElement(srcDir.getAbsolutePath()); + argList.addElement(getSourcePath()); if (target != null) { argList.addElement("-target"); argList.addElement(target); @@ -471,7 +560,7 @@ public class Javac extends MatchingTask { argList.addElement("-classpath"); argList.addElement(classpath); argList.addElement("-sourcepath"); - argList.addElement(srcDir.getAbsolutePath()); + argList.addElement(getSourcePath()); if (target != null) { argList.addElement("-target"); argList.addElement(target); @@ -566,7 +655,7 @@ public class Javac extends MatchingTask { // Jikes has no option for source-path so we // will add it to classpath. classpath.append(File.pathSeparator); - classpath.append(srcDir.getAbsolutePath()); + classpath.append(getSourcePath()); Vector argList = new Vector();