diff --git a/docs/manual/CoreTasks/signjar.html b/docs/manual/CoreTasks/signjar.html index 0eab81629..242db7805 100644 --- a/docs/manual/CoreTasks/signjar.html +++ b/docs/manual/CoreTasks/signjar.html @@ -12,7 +12,7 @@

Description

Signs JAR files with the jarsigner command line tool. It will take a named file in the jar attribute, and an optional -destDir or signedJar attribute. Nested filesets are also +destDir or signedJar attribute. Nested paths are also supported; here only an (optional) destDir is allowed. If a destination directory or explicit JAR file name is not provided, JARs are signed in place.

@@ -39,7 +39,7 @@ and lazy is false, the JAR is signed. jar the jar file to sign - Yes, unless nested filesets have + Yes, unless nested paths have been used. @@ -134,6 +134,11 @@ block Description Required + + path + path of JAR files to sign. since Ant 1.7 + No + fileset fileset of JAR files to sign. @@ -167,7 +172,9 @@ alias="apache-group" storepass="secret"/> alias="testonly" keystore="testkeystore" storepass="apacheant" preservelastmodified="true"> - <fileset dir="dist" includes="**/*.jar" /> + <path> + <fileset dir="dist" includes="**/*.jar" /> + </path> <flattenmapper /> </signjar> @@ -183,7 +190,9 @@ all be copied to this directory, not to subdirectories. storepass="apacheant" lazy="true" > - <fileset dir="dist" includes="**/*.jar" /> + <path> + <fileset dir="dist" includes="**/*.jar" /> + </path> </signjar>

diff --git a/src/etc/testcases/taskdefs/signjar.xml b/src/etc/testcases/taskdefs/signjar.xml index a483f76f4..6790ca109 100644 --- a/src/etc/testcases/taskdefs/signjar.xml +++ b/src/etc/testcases/taskdefs/signjar.xml @@ -118,6 +118,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -139,6 +165,15 @@ + + + + + + + + + @@ -154,6 +189,16 @@ + + + + + + + + + + @@ -211,6 +256,14 @@ + + + + + + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/AbstractJarSignerTask.java b/src/main/org/apache/tools/ant/taskdefs/AbstractJarSignerTask.java index 52cc52f29..c35940c33 100644 --- a/src/main/org/apache/tools/ant/taskdefs/AbstractJarSignerTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/AbstractJarSignerTask.java @@ -21,6 +21,7 @@ import org.apache.tools.ant.Task; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.util.JavaEnvUtils; import org.apache.tools.ant.types.FileSet; +import org.apache.tools.ant.types.Path; import org.apache.tools.ant.types.RedirectorElement; import org.apache.tools.ant.types.Environment; @@ -90,6 +91,13 @@ public abstract class AbstractJarSignerTask extends Task { public static final String ERROR_NO_SOURCE = "jar must be set through jar attribute " + "or nested filesets"; + /** + * Path holding all non-filesets of filesystem resources we want to sign. + * + * @since Ant 1.7 + */ + private Path path = null; + /** * Set the maximum memory to be used by the jarsigner process * @@ -181,6 +189,20 @@ public abstract class AbstractJarSignerTask extends Task { public void addSysproperty(Environment.Variable sysp) { sysProperties.addVariable(sysp); } + + /** + * Adds a path of files to sign. + * + * @param a path of files to sign. + * @since Ant 1.7 + */ + public Path createPath() { + if (path == null) { + path = new Path(getProject()); + } + return path.createPath(); + } + /** * init processing logic; this is retained through our execution(s) */ @@ -306,6 +328,7 @@ public abstract class AbstractJarSignerTask extends Task { //this lets us combine our logic for handling output directories, //mapping etc. FileSet sourceJar = new FileSet(); + sourceJar.setProject(getProject()); sourceJar.setFile(jar); sourceJar.setDir(jar.getParentFile()); sources.add(sourceJar); @@ -313,6 +336,31 @@ public abstract class AbstractJarSignerTask extends Task { return sources; } + /** + * clone our path and add all explicitly specified FileSets as + * well, patch in the jar attribute as a new fileset if it is + * defined. + * @return a path that contains all files to sign + * @since Ant 1.7 + */ + protected Path createUnifiedSourcePath() { + Path p = path == null ? new Path(getProject()) : (Path) path.clone(); + Vector s = createUnifiedSources(); + Enumeration e = s.elements(); + while (e.hasMoreElements()) { + p.add((FileSet) e.nextElement()); + } + return p; + } + + /** + * Has either a path or a fileset been specified? + * @since Ant 1.7 + */ + protected boolean hasResources() { + return path != null || filesets.size() > 0; + } + /** * add a value argument to a command * @param cmd command to manipulate diff --git a/src/main/org/apache/tools/ant/taskdefs/SignJar.java b/src/main/org/apache/tools/ant/taskdefs/SignJar.java index 220aed6b1..9b4fb60f0 100644 --- a/src/main/org/apache/tools/ant/taskdefs/SignJar.java +++ b/src/main/org/apache/tools/ant/taskdefs/SignJar.java @@ -19,13 +19,14 @@ package org.apache.tools.ant.taskdefs; import java.io.File; import java.io.IOException; -import java.util.Vector; +import java.util.Iterator; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.condition.IsSigned; -import org.apache.tools.ant.types.FileSet; +import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.types.resources.FileResource; import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.IdentityMapper; import org.apache.tools.ant.util.FileNameMapper; @@ -80,7 +81,7 @@ public class SignJar extends AbstractJarSignerTask { protected boolean lazy; /** - * the output directory when using filesets. + * the output directory when using paths. */ protected File destDir; @@ -111,7 +112,7 @@ public class SignJar extends AbstractJarSignerTask { /** * error string for unit test verification {@value} */ - public static final String ERROR_SIGNEDJAR_AND_FILESETS = "You cannot specify the signed JAR when using filesets"; + public static final String ERROR_SIGNEDJAR_AND_PATHS = "You cannot specify the signed JAR when using paths or filesets"; /** * error string for unit test verification: {@value} */ @@ -252,13 +253,12 @@ public class SignJar extends AbstractJarSignerTask { */ public void execute() throws BuildException { //validation logic - final boolean hasFileset = filesets.size() > 0; final boolean hasJar = jar != null; final boolean hasSignedJar = signedjar != null; final boolean hasDestDir = destDir != null; final boolean hasMapper = mapper != null; - if (!hasJar && !hasFileset) { + if (!hasJar && !hasResources()) { throw new BuildException(ERROR_NO_SOURCE); } if (null == alias) { @@ -274,8 +274,8 @@ public class SignJar extends AbstractJarSignerTask { } - if (hasFileset && hasSignedJar) { - throw new BuildException(ERROR_SIGNEDJAR_AND_FILESETS); + if (hasResources() && hasSignedJar) { + throw new BuildException(ERROR_SIGNEDJAR_AND_PATHS); } //this isnt strictly needed, but by being fussy now, @@ -297,9 +297,9 @@ public class SignJar extends AbstractJarSignerTask { } //the rest of the method treats single jar like - //a nested fileset with one file + //a nested path with one file - Vector sources = createUnifiedSources(); + Path sources = createUnifiedSourcePath(); //set up our mapping policy FileNameMapper destMapper; if (hasMapper) { @@ -310,34 +310,26 @@ public class SignJar extends AbstractJarSignerTask { } - //at this point the filesets are set up with lists of files, + //at this point the paths are set up with lists of files, //and the mapper is ready to map from source dirs to dest files //now we iterate through every JAR giving source and dest names - // deal with the filesets - for (int i = 0; i < sources.size(); i++) { - FileSet fs = (FileSet) sources.elementAt(i); - //get all included files in a fileset - DirectoryScanner ds = fs.getDirectoryScanner(getProject()); - String[] jarFiles = ds.getIncludedFiles(); - File baseDir = fs.getDir(getProject()); + // deal with the paths + Iterator iter = sources.iterator(); + while (iter.hasNext()) { + FileResource fr = (FileResource) iter.next(); //calculate our destination directory; it is either the destDir //attribute, or the base dir of the fileset (for in situ updates) - File toDir = hasDestDir ? destDir : baseDir; - - //loop through all jars in the fileset - for (int j = 0; j < jarFiles.length; j++) { - String jarFile = jarFiles[j]; - //determine the destination filename via the mapper - String[] destFilenames = destMapper.mapFileName(jarFile); - if (destFilenames == null || destFilenames.length != 1) { - //we only like simple mappers. - throw new BuildException(ERROR_BAD_MAP + jarFile); - } - File destFile = new File(toDir, destFilenames[0]); - File jarSource = new File(baseDir, jarFile); - signOneJar(jarSource, destFile); + File toDir = hasDestDir ? destDir : fr.getBaseDir(); + + //determine the destination filename via the mapper + String[] destFilenames = destMapper.mapFileName(fr.getName()); + if (destFilenames == null || destFilenames.length != 1) { + //we only like simple mappers. + throw new BuildException(ERROR_BAD_MAP + fr.getFile()); } + File destFile = new File(toDir, destFilenames[0]); + signOneJar(fr.getFile(), destFile); } } finally { endExecution(); diff --git a/src/testcases/org/apache/tools/ant/taskdefs/SignJarTest.java b/src/testcases/org/apache/tools/ant/taskdefs/SignJarTest.java index a5019e741..59b97420a 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/SignJarTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/SignJarTest.java @@ -86,7 +86,21 @@ public class SignJarTest extends BuildFileTest { public void testFilesetAndSignedJar() { expectBuildExceptionContaining("testFilesetAndSignedJar", "incompatible attributes", - SignJar.ERROR_SIGNEDJAR_AND_FILESETS); + SignJar.ERROR_SIGNEDJAR_AND_PATHS); + } + + public void testPath() { + executeTarget("testPath"); + } + + public void testPathAndJar() { + executeTarget("testPathAndJar"); + } + + public void testPathAndSignedJar() { + expectBuildExceptionContaining("testPathAndSignedJar", + "incompatible attributes", + SignJar.ERROR_SIGNEDJAR_AND_PATHS); } public void testSignedJar() { @@ -100,7 +114,13 @@ public class SignJarTest extends BuildFileTest { public void testDestDirAndSignedJar() { expectBuildExceptionContaining("testFilesetAndSignedJar", "incompatible attributes", - SignJar.ERROR_SIGNEDJAR_AND_FILESETS); + SignJar.ERROR_SIGNEDJAR_AND_PATHS); + } + + public void testDestDirAndSignedJar2() { + expectBuildExceptionContaining("testPathAndSignedJar", + "incompatible attributes", + SignJar.ERROR_SIGNEDJAR_AND_PATHS); } public void testDestDirFileset() { @@ -111,6 +131,14 @@ public class SignJarTest extends BuildFileTest { executeTarget("testMapperFileset"); } + public void testDestDirPath() { + executeTarget("testDestDirPath"); + } + + public void testMapperPath() { + executeTarget("testMapperPath"); + } + public void testMapperNoDest() { expectBuildExceptionContaining("testMapperNoDest", "two mappers", @@ -180,4 +208,8 @@ public class SignJarTest extends BuildFileTest { executeTarget("testVerifyFileset"); } + public void testVerifyPath() { + executeTarget("testVerifyPath"); + } + }