Index: src/etc/testcases/taskdefs/optional/propertyfile.xml =================================================================== RCS file: /home/cvspublic/ant/src/etc/testcases/taskdefs/optional/propertyfile.xml,v retrieving revision 1.4 diff -u -r1.4 propertyfile.xml --- src/etc/testcases/taskdefs/optional/propertyfile.xml 11 Jul 2003 21:25:35 -0000 1.4 +++ src/etc/testcases/taskdefs/optional/propertyfile.xml 23 Oct 2003 16:45:20 -0000 @@ -84,6 +84,12 @@ + + + + + + Index: src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java =================================================================== RCS file: /home/cvspublic/ant/src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java,v retrieving revision 1.32 diff -u -r1.32 PropertyFile.java --- src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java 17 Sep 2003 20:11:43 -0000 1.32 +++ src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java 23 Oct 2003 16:45:20 -0000 @@ -71,6 +71,7 @@ import java.util.Properties; import java.util.Vector; import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; import org.apache.tools.ant.types.EnumeratedAttribute; @@ -171,8 +172,17 @@ public void execute() throws BuildException { checkParameters(); readFile(); - executeOperation(); - writeFile(); + boolean modified = executeOperation(); + String path = propertyfile.getAbsolutePath(); + if (!propertyfile.exists()) { + log("Creating new property file: " + path); + writeFile(); + } else if (modified) { + log("Updating property file: " + path); + writeFile(); + } else { + log("No change made to " + path, Project.MSG_VERBOSE); + } } public Entry createEntry() { @@ -181,11 +191,13 @@ return e; } - private void executeOperation() throws BuildException { + private boolean executeOperation() throws BuildException { + boolean didSomething = false; for (Enumeration e = entries.elements(); e.hasMoreElements();) { Entry entry = (Entry) e.nextElement(); - entry.executeOn(properties); + didSomething |= entry.executeOn(properties); } + return didSomething; } private void readFile() throws BuildException { @@ -193,8 +205,6 @@ properties = new Properties(); try { if (propertyfile.exists()) { - log("Updating property file: " - + propertyfile.getAbsolutePath()); FileInputStream fis = null; try { fis = new FileInputStream(propertyfile); @@ -205,18 +215,6 @@ fis.close(); } } - } else { - log("Creating new property file: " - + propertyfile.getAbsolutePath()); - FileOutputStream out = null; - try { - out = new FileOutputStream(propertyfile.getAbsolutePath()); - out.flush(); - } finally { - if (out != null) { - out.close(); - } - } } } catch (IOException ioe) { throw new BuildException(ioe.toString()); @@ -351,7 +349,12 @@ field = unit.getCalendarField(); } - protected void executeOn(Properties props) throws BuildException { + /** + * Process this entry. + * @param props the list of properties to work on + * @return true if a change was made + */ + protected boolean executeOn(Properties props) throws BuildException { checkParameters(); // type may be null because it wasn't set @@ -379,6 +382,8 @@ // Insert as a string by default props.put(key, newValue); + + return !newValue.equals(oldValue); } /** Index: src/testcases/org/apache/tools/ant/taskdefs/optional/PropertyFileTest.java =================================================================== RCS file: /home/cvspublic/ant/src/testcases/org/apache/tools/ant/taskdefs/optional/PropertyFileTest.java,v retrieving revision 1.9 diff -u -r1.9 PropertyFileTest.java --- src/testcases/org/apache/tools/ant/taskdefs/optional/PropertyFileTest.java 7 Sep 2003 09:40:40 -0000 1.9 +++ src/testcases/org/apache/tools/ant/taskdefs/optional/PropertyFileTest.java 23 Oct 2003 16:45:20 -0000 @@ -175,6 +175,29 @@ } */ + /** + * Confirm that if the task would not actually change any properties, + * the file is not touched. + */ + public void testNoOp() { + File tpf = new File(testPropsFilePath); + assertTrue(tpf.exists()); + long mtime1 = tpf.lastModified(); + long size1 = tpf.length(); + assertTrue(mtime1 != 0L); + assertTrue(size1 != 0L); + try { + // Make sure some measurable time has elapsed. + Thread.sleep(100); + } catch (InterruptedException e) {} + executeTarget("testNoOp"); + assertTrue(tpf.exists()); + long mtime2 = tpf.lastModified(); + long size2 = tpf.length(); + assertEquals("Timestamp of " + testPropsFilePath + " was not changed", mtime1, mtime2); + assertEquals("Size of " + testPropsFilePath + " was not changed", size1, size2); + } + private Properties getTestProperties() throws Exception { Properties testProps = new Properties(); FileInputStream propsFile = new FileInputStream(testPropsFilePath);