Browse Source

Made tasks able to use the new pattern based directory scanning, while

retaining backwards compatibility.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267585 13f79535-47bb-0310-9956-ffa450edef68
master
Arnout J. Kuiper 25 years ago
parent
commit
1844a85f5f
4 changed files with 335 additions and 119 deletions
  1. +100
    -28
      src/main/org/apache/tools/ant/taskdefs/Copydir.java
  2. +16
    -8
      src/main/org/apache/tools/ant/taskdefs/Jar.java
  3. +85
    -25
      src/main/org/apache/tools/ant/taskdefs/Javac.java
  4. +134
    -58
      src/main/org/apache/tools/ant/taskdefs/Zip.java

+ 100
- 28
src/main/org/apache/tools/ant/taskdefs/Copydir.java View File

@@ -67,11 +67,12 @@ import java.util.*;

public class Copydir extends Task {

public File srcDir;
public File destDir;

private File srcDir;
private File destDir;
private String[] includes;
private String[] excludes;
private boolean useDefaultExcludes = true;
private Hashtable filecopyList = new Hashtable();
private Vector ignoreList = new Vector();

public void setSrc(String src) {
srcDir = project.resolveFile(src);
@@ -81,8 +82,85 @@ public class Copydir extends Task {
destDir = project.resolveFile(dest);
}
/**
* Sets the set of include patterns. Patterns may be separated by a comma
* or a space.
*
* @param includes the string containing the include patterns
*/
public void setIncludes(String includes) {
if (includes != null && includes.length() > 0) {
Vector tmpIncludes = new Vector();
StringTokenizer tok = new StringTokenizer(includes, ", ", false);
while (tok.hasMoreTokens()) {
String pattern = tok.nextToken().trim();
if (pattern.length() > 0) {
tmpIncludes.addElement(pattern);
}
}
this.includes = new String[tmpIncludes.size()];
for (int i = 0; i < tmpIncludes.size(); i++) {
this.includes[i] = (String)tmpIncludes.elementAt(i);
}
} else {
this.includes = null;
}
}

/**
* Sets the set of exclude patterns. Patterns may be separated by a comma
* or a space.
*
* @param excludes the string containing the exclude patterns
*/
public void setExcludes(String excludes) {
if (excludes != null && excludes.length() > 0) {
Vector tmpExcludes = new Vector();
StringTokenizer tok = new StringTokenizer(excludes, ", ", false);
while (tok.hasMoreTokens()) {
String pattern = tok.nextToken().trim();
if (pattern.length() > 0) {
tmpExcludes.addElement(pattern);
}
}
this.excludes = new String[tmpExcludes.size()];
for (int i = 0; i < tmpExcludes.size(); i++) {
this.excludes[i] = (String)tmpExcludes.elementAt(i);
}
} else {
this.excludes = null;
}
}

/**
* Sets whether default exclusions should be used or not.
*
* @param useDefaultExcludes "true" or "on" when default exclusions should
* be used, "false" or "off" when they
* shouldn't be used.
*/
public void setDefaultexcludes(String useDefaultExcludes) {
this.useDefaultExcludes = Project.toBoolean(useDefaultExcludes);
}

public void execute() throws BuildException {
scanDir(srcDir, destDir);
if (srcDir == null) {
throw new BuildException("srcdir attribute must be set!");
}
if (!srcDir.exists()) {
throw new BuildException("srcdir does not exist!");
}
DirectoryScanner ds = new DirectoryScanner();
ds.setBasedir(srcDir);
ds.setIncludes(includes);
ds.setExcludes(excludes);
if (useDefaultExcludes) {
ds.addDefaultExcludes();
}
ds.scan();
String[] files = ds.getIncludedFiles();
scanDir(srcDir, destDir, files);
if (filecopyList.size() > 0) {
project.log("Copying " + filecopyList.size() + " files to "
+ destDir.getAbsolutePath());
@@ -115,38 +193,32 @@ public class Copydir extends Task {
@author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a>
*/
public void setIgnore(String ignoreString) {
ignoreString = ignoreString;
project.log("The ignore attribute is deprecated. "+
"Please use the excludes attribute.",
Project.MSG_WARN);
if (ignoreString != null && ignoreString.length() > 0) {
StringTokenizer tok =
new StringTokenizer(ignoreString, ", ", false);
Vector tmpExcludes = new Vector();
StringTokenizer tok = new StringTokenizer(ignoreString, ", ", false);
while (tok.hasMoreTokens()) {
ignoreList.addElement ( tok.nextToken().trim() );
tmpExcludes.addElement("**/"+tok.nextToken().trim()+"/**");
}
this.excludes = new String[tmpExcludes.size()];
for (int i = 0; i < tmpExcludes.size(); i++) {
this.excludes[i] = (String)tmpExcludes.elementAt(i);
}
} else {
this.excludes = null;
}
}

private void scanDir(File from, File to) {
String[] list = from.list(new DesirableFilter());
if (list == null) {
project.log("Source directory " + srcDir.getAbsolutePath()
+ " does not exist.", "copydir", Project.MSG_WARN);
return;
}
for (int i = 0; i < list.length; i++) {
String filename = list[i];
private void scanDir(File from, File to, String[] files) {
for (int i = 0; i < files.length; i++) {
String filename = files[i];
File srcFile = new File(from, filename);
File destFile = new File(to, filename);
if ( ! ignoreList.contains(filename) ) {
if (srcFile.isDirectory()) {
scanDir(srcFile, destFile);
} else {
if (srcFile.lastModified() > destFile.lastModified()) {
filecopyList.put(srcFile.getAbsolutePath(),
if (srcFile.lastModified() > destFile.lastModified()) {
filecopyList.put(srcFile.getAbsolutePath(),
destFile.getAbsolutePath());
}
}
} else {
project.log("Copydir Ignored: " + filename, Project.MSG_VERBOSE);
}
}
}


+ 16
- 8
src/main/org/apache/tools/ant/taskdefs/Jar.java View File

@@ -87,7 +87,7 @@ public class Jar extends Zip {
if (manifest != null) {
ZipEntry ze = new ZipEntry("META-INF/");
zOut.putNextEntry(ze);
zipFile(manifest, zOut, "META-INF/MANIFEST.MF");
super.zipFile(manifest, zOut, "META-INF/MANIFEST.MF");
} else {
ZipEntry ze = new ZipEntry("META-INF/");
zOut.putNextEntry(ze);
@@ -100,14 +100,22 @@ public class Jar extends Zip {
}

protected void zipDir(File dir, ZipOutputStream zOut, String vPath)
throws IOException
throws IOException
{
// First add directory to zip entry
if( ! "META-INF/".equals(vPath) ) {
// we already added a META-INF
ZipEntry ze = new ZipEntry(vPath);
zOut.putNextEntry(ze);
// First add directory to zip entry
if(!vPath.equals("META-INF/")) {
// we already added a META-INF
ZipEntry ze = new ZipEntry(vPath);
zOut.putNextEntry(ze);
}
}

protected void zipFile(File file, ZipOutputStream zOut, String vPath)
throws IOException
{
// We already added a META-INF/MANIFEST.MF
if (!vPath.equals("META-INF/MANIFEST.MF")) {
super.zipFile(file, zOut, vPath);
}
super.zipDir(dir, zOut, vPath);
}
}

+ 85
- 25
src/main/org/apache/tools/ant/taskdefs/Javac.java View File

@@ -98,6 +98,9 @@ public class Javac extends Task {
private String target;
private String bootclasspath;
private String extdirs;
private String[] includes;
private String[] excludes;
private boolean useDefaultExcludes = true;

private Vector compileList = new Vector();
private Hashtable filecopyList = new Hashtable();
@@ -193,22 +196,91 @@ public class Javac extends Task {
}

/**
* Executes the task.
* Sets the set of include patterns. Patterns may be separated by a comma
* or a space.
*
* @param includes the string containing the include patterns
*/
public void setIncludes(String includes) {
if (includes != null && includes.length() > 0) {
Vector tmpIncludes = new Vector();
StringTokenizer tok = new StringTokenizer(includes, ", ", false);
while (tok.hasMoreTokens()) {
tmpIncludes.addElement(tok.nextToken().trim());
}
this.includes = new String[tmpIncludes.size()];
for (int i = 0; i < tmpIncludes.size(); i++) {
this.includes[i] = (String)tmpIncludes.elementAt(i);
}
} else {
this.includes = null;
}
}

public void execute() throws BuildException {
/**
* Sets the set of exclude patterns. Patterns may be separated by a comma
* or a space.
*
* @param excludes the string containing the exclude patterns
*/
public void setExcludes(String excludes) {
if (excludes != null && excludes.length() > 0) {
Vector tmpExcludes = new Vector();
StringTokenizer tok = new StringTokenizer(excludes, ", ", false);
while (tok.hasMoreTokens()) {
tmpExcludes.addElement(tok.nextToken().trim());
}
this.excludes = new String[tmpExcludes.size()];
for (int i = 0; i < tmpExcludes.size(); i++) {
this.excludes[i] = (String)tmpExcludes.elementAt(i);
}
} else {
this.excludes = null;
}
}

/**
* Sets whether default exclusions should be used or not.
*
* @param useDefaultExcludes "true" or "on" when default exclusions should
* be used, "false" or "off" when they
* shouldn't be used.
*/
public void setDefaultexcludes(String useDefaultExcludes) {
this.useDefaultExcludes = Project.toBoolean(useDefaultExcludes);
}

/**
* Executes the task.
*/
public void execute() throws BuildException {
// first off, make sure that we've got a srcdir and destdir

if (srcDir == null || destDir == null ) {
String msg = "srcDir and destDir attributes must be set!";
throw new BuildException(msg);
if (srcDir == null) {
throw new BuildException("srcdir attribute must be set!");
}
if (!srcDir.exists()) {
throw new BuildException("srcdir 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
// compile lists

scanDir(srcDir, destDir);
DirectoryScanner ds = new DirectoryScanner();
ds.setBasedir(srcDir);
ds.setIncludes(includes);
ds.setExcludes(excludes);
if (useDefaultExcludes) {
ds.addDefaultExcludes();
}
ds.scan();

String[] files = ds.getIncludedFiles();

scanDir(srcDir, destDir, files);

// compile the source files

@@ -262,33 +334,21 @@ public class Javac extends Task {
* support files to be copied.
*/

private void scanDir(File srcDir, File destDir) {

String[] list = srcDir.list(new DesirableFilter());
int len = (list==null ? 0 : list.length);
for (int i = 0; i < len; i++) {
String filename = list[i];
File srcFile = new File(srcDir, filename);
File destFile = new File(destDir, filename);
if (srcFile.isDirectory()) {
// it's a dir, scan that recursively
scanDir(srcFile, destFile);
} else {
// it's a file, see if we compile it or just copy it
if (filename.endsWith(".java")) {
File classFile =
new File(destDir,
filename.substring(0,
filename.indexOf(".java"))
private void scanDir(File srcDir, File destDir, String files[]) {
for (int i = 0; i < files.length; i++) {
File srcFile = new File(srcDir, files[i]);
if (files[i].endsWith(".java")) {
File classFile = new File(destDir, files[i].substring(0,
files[i].indexOf(".java"))
+ ".class");
if (srcFile.lastModified() > classFile.lastModified()) {
compileList.addElement(srcFile.getAbsolutePath());
}
} else {
File destFile = new File(destDir, files[i]);
if (srcFile.lastModified() > destFile.lastModified()) {
filecopyList.put(srcFile.getAbsolutePath(),
destFile.getAbsolutePath());
}
}
}
}


+ 134
- 58
src/main/org/apache/tools/ant/taskdefs/Zip.java View File

@@ -74,10 +74,10 @@ public class Zip extends Task {

private File zipFile;
private File baseDir;
private Vector items = new Vector();
private String[] includes;
private String[] excludes;
private boolean useDefaultExcludes = true;
private File manifest;
private Vector ignoreList = new Vector();
private boolean allItems = false;
protected String archiveType = "zip";
/**
@@ -107,15 +107,28 @@ public class Zip extends Task {
ignore lists are easier than include lists. ;-)
*/
public void setItems(String itemString) {
if ( itemString.equals("*") ) {
allItems = true;
project.log("The items attribute is deprecated. "+
"Please use the includes attribute.",
Project.MSG_WARN);
if (itemString == null || itemString.equals("*")) {
includes = new String[1];
includes[0] = "**";
} else {
StringTokenizer tok = new StringTokenizer(itemString, ",", false);
Vector tmpIncludes = new Vector();
StringTokenizer tok = new StringTokenizer(itemString, ", ");
while (tok.hasMoreTokens()) {
items.addElement(tok.nextToken().trim());
String pattern = tok.nextToken().trim();
if (pattern.length() > 0) {
tmpIncludes.addElement(pattern+"/**");
}
}
this.includes = new String[tmpIncludes.size()];
for (int i = 0; i < tmpIncludes.size(); i++) {
this.includes[i] = (String)tmpIncludes.elementAt(i);
}
}
}

/**
List of filenames and directory names to not
include in the final .jar file. They should be either
@@ -130,49 +143,127 @@ public class Zip extends Task {
@author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a>
*/
public void setIgnore(String ignoreString) {
ignoreString = ignoreString;
if (ignoreString != null && ignoreString.length() > 0) {
StringTokenizer tok =
new StringTokenizer(ignoreString, ", ", false);
project.log("The ignore attribute is deprecated. "+
"Please use the excludes attribute.",
Project.MSG_WARN);
if (ignoreString == null) {
this.excludes = null;
} else {
Vector tmpExcludes = new Vector();
StringTokenizer tok = new StringTokenizer(ignoreString, ", ");
while (tok.hasMoreTokens()) {
ignoreList.addElement ( tok.nextToken().trim() );
String pattern = tok.nextToken().trim();
if (pattern.length() > 0) {
tmpExcludes.addElement("**/"+pattern+"/**");
}
}
this.excludes = new String[tmpExcludes.size()];
for (int i = 0; i < tmpExcludes.size(); i++) {
this.excludes[i] = (String)tmpExcludes.elementAt(i);
}
}
}
/**
* Sets the set of include patterns. Patterns may be separated by a comma
* or a space.
*
* @param includes the string containing the include patterns
*/
public void setIncludes(String includes) {
if (includes == null) {
this.includes = null;
} else {
Vector tmpIncludes = new Vector();
StringTokenizer tok = new StringTokenizer(includes, ", ");
while (tok.hasMoreTokens()) {
String pattern = tok.nextToken().trim();
if (pattern.length() > 0) {
tmpIncludes.addElement(pattern);
}
}
this.includes = new String[tmpIncludes.size()];
for (int i = 0; i < tmpIncludes.size(); i++) {
this.includes[i] = (String)tmpIncludes.elementAt(i);
}
}
}

/**
* Sets the set of exclude patterns. Patterns may be separated by a comma
* or a space.
*
* @param excludes the string containing the exclude patterns
*/
public void setExcludes(String excludes) {
if (excludes == null) {
this.excludes = null;
} else {
Vector tmpExcludes = new Vector();
StringTokenizer tok = new StringTokenizer(excludes, ", ", false);
while (tok.hasMoreTokens()) {
String pattern = tok.nextToken().trim();
if (pattern.length() > 0) {
tmpExcludes.addElement(pattern);
}
}
this.excludes = new String[tmpExcludes.size()];
for (int i = 0; i < tmpExcludes.size(); i++) {
this.excludes[i] = (String)tmpExcludes.elementAt(i);
}
}
}

/**
* Sets whether default exclusions should be used or not.
*
* @param useDefaultExcludes "true" or "on" when default exclusions should
* be used, "false" or "off" when they
* shouldn't be used.
*/
public void setDefaultexcludes(String useDefaultExcludes) {
this.useDefaultExcludes = Project.toBoolean(useDefaultExcludes);
}

public void execute() throws BuildException {
project.log("Building " + archiveType + ": " + zipFile.getAbsolutePath());
project.log("Building "+ archiveType +": "+ zipFile.getAbsolutePath());

if (baseDir == null) {
throw new BuildException("basedir attribute must be set!");
}
if (!baseDir.exists()) {
throw new BuildException("basedir does not exist!");
}

DirectoryScanner ds = new DirectoryScanner();
ds.setBasedir(baseDir);
ds.setIncludes(includes);
ds.setExcludes(excludes);
if (useDefaultExcludes) {
ds.addDefaultExcludes();
}
ds.scan();

String[] files = ds.getIncludedFiles();
String[] dirs = ds.getIncludedDirectories();

try {
ZipOutputStream zOut = new ZipOutputStream(new FileOutputStream(zipFile));
initZipOutputStream(zOut);
if ( allItems ) {
String[] lst = baseDir.list();
for (int i=0;i<lst.length;i++) {
items.addElement(lst[i]);
}
initZipOutputStream(zOut);

for (int i = 0; i < dirs.length; i++) {
File f = new File(baseDir,dirs[i]);
String name = dirs[i].replace(File.separatorChar,'/')+"/";
zipDir(f, zOut, name);
}

// add items
Enumeration e = items.elements();
while (e.hasMoreElements()) {
String s = (String)e.nextElement();
// check to make sure item is not in ignore list
// shouldn't be ignored here, but just want to make sure
if (! ignoreList.contains(s)) {
File f = new File(baseDir, s);
if (f.isDirectory()) {
zipDir(f, zOut, s + "/");
} else {
zipFile(f, zOut, s);
}
} else {
project.log("Ignoring: " + s, Project.MSG_WARN);
}
for (int i = 0; i < files.length; i++) {
File f = new File(baseDir,files[i]);
String name = files[i].replace(File.separatorChar,'/');
zipFile(f, zOut, name);
}
// close up

// close up
zOut.close();
} catch (IOException ioe) {
String msg = "Problem creating " + archiveType + " " + ioe.getMessage();
@@ -181,29 +272,14 @@ public class Zip extends Task {
}

protected void initZipOutputStream(ZipOutputStream zOut)
throws IOException, BuildException
throws IOException, BuildException
{
zOut.setMethod(ZipOutputStream.DEFLATED);
zOut.setMethod(ZipOutputStream.DEFLATED);
}

protected void zipDir(File dir, ZipOutputStream zOut, String vPath)
throws IOException
{
String[] list = dir.list();
for (int i = 0; i < list.length; i++) {
String f = list[i];
// check to make sure item is not in ignore list
if (! ignoreList.contains(f)) {
File file = new File(dir, f);
if (file.isDirectory()) {
zipDir(file, zOut, vPath + f + "/");
} else {
zipFile(file, zOut, vPath + f);
}
} else {
project.log("Ignoring: " + f, Project.MSG_WARN);
}
}
}

protected void zipFile(InputStream in, ZipOutputStream zOut, String vPath)
@@ -211,7 +287,7 @@ public class Zip extends Task {
{
ZipEntry ze = new ZipEntry(vPath);
zOut.putNextEntry(ze);
byte[] buffer = new byte[8 * 1024];
int count = 0;
do {
@@ -219,7 +295,7 @@ public class Zip extends Task {
count = in.read(buffer, 0, buffer.length);
} while (count != -1);
}
protected void zipFile(File file, ZipOutputStream zOut, String vPath)
throws IOException
{


Loading…
Cancel
Save