From 3f4cb68defbf5f5b4c642c2e5cebded4172707d1 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Tue, 15 Jun 2010 15:46:16 +0000 Subject: [PATCH] resolve properties defined via -propertyfile against each other. PR 18732 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@954939 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 6 ++ src/main/org/apache/tools/ant/Main.java | 19 ++++-- .../apache/tools/ant/PropertyFileCLITest.java | 62 +++++++++++++++++++ 3 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 src/tests/junit/org/apache/tools/ant/PropertyFileCLITest.java diff --git a/WHATSNEW b/WHATSNEW index 63d177f26..9e3fc05e6 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -78,6 +78,12 @@ Other changes: * You can now specify a list of methods to run in a JUnit test case. Bugzilla Report 34748. + * properties in files read because of the -propertyfile command line + option will get now resolved against other properties that are + defined before the project starts executing (those from the same or + other propertfiles or defined via the -D option). + Bugzilla Report 18732. + Changes from Ant 1.8.0 TO Ant 1.8.1 =================================== diff --git a/src/main/org/apache/tools/ant/Main.java b/src/main/org/apache/tools/ant/Main.java index b0d67488f..9ffb2629e 100644 --- a/src/main/org/apache/tools/ant/Main.java +++ b/src/main/org/apache/tools/ant/Main.java @@ -37,6 +37,7 @@ import java.util.Vector; import org.apache.tools.ant.input.DefaultInputHandler; import org.apache.tools.ant.input.InputHandler; import org.apache.tools.ant.launch.AntMain; +import org.apache.tools.ant.property.ResolvePropertyMap; import org.apache.tools.ant.util.ClasspathUtils; import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.ProxySetup; @@ -762,12 +763,20 @@ public class Main implements AntMain { project.init(); + // resolve properties + PropertyHelper propertyHelper + = (PropertyHelper) PropertyHelper.getPropertyHelper(project); + HashMap props = new HashMap(definedProps); + new ResolvePropertyMap(project, propertyHelper, + propertyHelper.getExpanders()) + .resolveAllProperties(props, null); + // set user-define properties - Enumeration e = definedProps.keys(); - while (e.hasMoreElements()) { - String arg = (String) e.nextElement(); - String value = (String) definedProps.get(arg); - project.setUserProperty(arg, value); + for (Iterator e = props.entrySet().iterator(); e.hasNext(); ) { + Map.Entry ent = (Map.Entry) e.next(); + String arg = (String) ent.getKey(); + Object value = ent.getValue(); + project.setUserProperty(arg, String.valueOf(value)); } project.setUserProperty(MagicNames.ANT_FILE, diff --git a/src/tests/junit/org/apache/tools/ant/PropertyFileCLITest.java b/src/tests/junit/org/apache/tools/ant/PropertyFileCLITest.java new file mode 100644 index 000000000..042aacd98 --- /dev/null +++ b/src/tests/junit/org/apache/tools/ant/PropertyFileCLITest.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant; + +import java.io.File; +import java.io.FileWriter; +import java.io.PrintStream; +import junit.framework.TestCase; + +public class PropertyFileCLITest extends TestCase { + + public void testPropertyResolution() throws Exception { + File props = File.createTempFile("propertyfilecli", ".properties"); + props.deleteOnExit(); + FileWriter fw = new FileWriter(props); + fw.write("w=world\nmessage=Hello, ${w}\n"); + fw.close(); + File build = File.createTempFile("propertyfilecli", ".xml"); + build.deleteOnExit(); + fw = new FileWriter(build); + fw.write("${message}"); + fw.close(); + PrintStream sysOut = System.out; + StringBuffer sb = new StringBuffer(); + try { + PrintStream out = + new PrintStream(new BuildFileTest.AntOutputStream(sb)); + System.setOut(out); + Main m = new NoExitMain(); + m.startAnt(new String[] { + "-propertyfile", props.getAbsolutePath(), + "-f", build.getAbsolutePath() + }, null, null); + } finally { + System.setOut(sysOut); + } + String log = sb.toString(); + assertTrue("expected log to contain 'Hello, world' but was " + log, + log.indexOf("Hello, world") > -1); + } + + private static class NoExitMain extends Main { + protected void exit(int exitCode) { + } + } +}