ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From maart...@apache.org
Subject svn commit: r610562 - in /ant/ivy/core/trunk: ./ src/java/org/apache/ivy/ant/ src/java/org/apache/ivy/core/ src/java/org/apache/ivy/core/retrieve/ src/java/org/apache/ivy/core/settings/ test/java/org/apache/ivy/ant/
Date Wed, 09 Jan 2008 21:03:31 GMT
Author: maartenc
Date: Wed Jan  9 13:02:58 2008
New Revision: 610562

URL: http://svn.apache.org/viewvc?rev=610562&view=rev
Log:
NEW: Add support for importing environment variables (IVY-608)

Modified:
    ant/ivy/core/trunk/CHANGES.txt
    ant/ivy/core/trunk/src/java/org/apache/ivy/ant/IvyAntVariableContainer.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/core/IvyPatternHelper.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/core/retrieve/RetrieveEngineSettings.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvySettings.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvyVariableContainer.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvyVariableContainerImpl.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/XmlSettingsParser.java
    ant/ivy/core/trunk/test/java/org/apache/ivy/ant/IvyConfigureTest.java
    ant/ivy/core/trunk/test/java/org/apache/ivy/ant/IvyTaskTest.java

Modified: ant/ivy/core/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/CHANGES.txt?rev=610562&r1=610561&r2=610562&view=diff
==============================================================================
--- ant/ivy/core/trunk/CHANGES.txt (original)
+++ ant/ivy/core/trunk/CHANGES.txt Wed Jan  9 13:02:58 2008
@@ -59,6 +59,7 @@
    version in SVN
 =====================================
 - NEW: Introduce RepositoryManagementEngine (IVY-665 - not completed yet)
+- NEW: Add support for importing environment variables (IVY-608)
 
 - IMPROVEMENT: Make IBiblio resolver compatible with maven proxy (IVY-466)
 - IMPROVEMENT: Use namespace aware validation (IVY-553)

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/ant/IvyAntVariableContainer.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/ant/IvyAntVariableContainer.java?rev=610562&r1=610561&r2=610562&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/ant/IvyAntVariableContainer.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/ant/IvyAntVariableContainer.java Wed Jan  9
13:02:58 2008
@@ -26,6 +26,7 @@
 import org.apache.ivy.core.settings.IvyVariableContainerImpl;
 import org.apache.ivy.util.Message;
 import org.apache.tools.ant.Project;
+import org.apache.tools.ant.taskdefs.Property;
 
 class IvyAntVariableContainer extends IvyVariableContainerImpl implements IvyVariableContainer
{
 
@@ -48,13 +49,6 @@
         return r;
     }
 
-    public Map getVariables() {
-        Map r = new HashMap(super.getVariables());
-        r.putAll(project.getProperties());
-        r.putAll(overwrittenProperties);
-        return r;
-    }
-
     public void setVariable(String varName, String value, boolean overwrite) {
         if (overwrite) {
             Message.debug("setting '" + varName + "' to '" + value + "'");
@@ -88,6 +82,14 @@
             if (id != null) {
                 setPropertyIfNotSet((String) entry.getKey() + "." + id, (String) entry.getValue());
             }
+        }
+        
+        if (getEnvironmentPrefix() != null) {
+            Property propTask = new Property();
+            propTask.setProject(project);
+            propTask.setEnvironment(getEnvironmentPrefix());
+            propTask.init();
+            propTask.execute();
         }
     }
 

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/core/IvyPatternHelper.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/core/IvyPatternHelper.java?rev=610562&r1=610561&r2=610562&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/core/IvyPatternHelper.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/core/IvyPatternHelper.java Wed Jan  9 13:02:58
2008
@@ -31,7 +31,8 @@
 import org.apache.ivy.core.module.descriptor.Artifact;
 import org.apache.ivy.core.module.descriptor.DefaultArtifact;
 import org.apache.ivy.core.module.id.ModuleRevisionId;
-import org.apache.ivy.plugins.resolver.DependencyResolver;
+import org.apache.ivy.core.settings.IvyVariableContainer;
+import org.apache.ivy.core.settings.IvyVariableContainerImpl;
 import org.apache.ivy.util.Message;
 
 /**
@@ -151,15 +152,15 @@
         return substituteTokens(pattern, tokens);
     }
 
-    public static String substitute(String pattern, Map variables, Map tokens) {
-        return substituteTokens(substituteVariables(pattern, variables), tokens);
+    public static String substituteVariables(String pattern, Map variables) {
+        return substituteVariables(pattern, new IvyVariableContainerImpl(variables), new
Stack());
     }
 
-    public static String substituteVariables(String pattern, Map variables) {
+    public static String substituteVariables(String pattern, IvyVariableContainer variables)
{
         return substituteVariables(pattern, variables, new Stack());
     }
 
-    private static String substituteVariables(String pattern, Map variables, Stack substituting)
{
+    private static String substituteVariables(String pattern, IvyVariableContainer variables,
Stack substituting) {
         // if you supply null, null is what you get
         if (pattern == null) {
             return null;
@@ -170,7 +171,7 @@
         StringBuffer sb = new StringBuffer();
         while (m.find()) {
             String var = m.group(1);
-            String val = (String) variables.get(var);
+            String val = (String) variables.getVariable(var);
             if (val != null) {
                 int index = substituting.indexOf(var);
                 if (index != -1) {
@@ -325,10 +326,10 @@
     }
 
     public static String substituteParams(String pattern, Map params) {
-        return substituteParams(pattern, params, new Stack());
+        return substituteParams(pattern, new IvyVariableContainerImpl(params), new Stack());
     }
 
-    private static String substituteParams(String pattern, Map params, Stack substituting)
{
+    private static String substituteParams(String pattern, IvyVariableContainer params, Stack
substituting) {
         // TODO : refactor this with substituteVariables
         // if you supply null, null is what you get
         if (pattern == null) {
@@ -340,7 +341,7 @@
         StringBuffer sb = new StringBuffer();
         while (m.find()) {
             String var = m.group(1);
-            String val = (String) params.get(var);
+            String val = (String) params.getVariable(var);
             if (val != null) {
                 int index = substituting.indexOf(var);
                 if (index != -1) {
@@ -361,21 +362,6 @@
         m.appendTail(sb);
 
         return sb.toString();
-    }
-
-    public static void main(String[] args) {
-        String pattern = "[organisation]/[module]/build/archives/[type]s/" 
-                + "[artifact]-[revision].[ext]";
-        System.out.println("pattern= " + pattern);
-        System.out.println("resolved= "
-                + substitute(pattern, "apache", "Test", "1.0", "test", "jar", "jar"));
-
-        Map variables = new HashMap();
-        variables.put("test", "mytest");
-        variables.put("test2", "${test}2");
-        pattern = "${test} ${test2} ${nothing}";
-        System.out.println("pattern= " + pattern);
-        System.out.println("resolved= " + substituteVariables(pattern, variables));
     }
 
     /**

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/core/retrieve/RetrieveEngineSettings.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/core/retrieve/RetrieveEngineSettings.java?rev=610562&r1=610561&r2=610562&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/core/retrieve/RetrieveEngineSettings.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/core/retrieve/RetrieveEngineSettings.java Wed
Jan  9 13:02:58 2008
@@ -17,14 +17,13 @@
  */
 package org.apache.ivy.core.retrieve;
 
-import java.util.Map;
-
+import org.apache.ivy.core.settings.IvyVariableContainer;
 import org.apache.ivy.plugins.parser.ParserSettings;
 
 public interface RetrieveEngineSettings extends ParserSettings {
 
     boolean isCheckUpToDate();
 
-    Map getVariables();
+    IvyVariableContainer getVariables();
 
 }

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvySettings.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvySettings.java?rev=610562&r1=610561&r2=610562&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvySettings.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvySettings.java Wed Jan  9 13:02:58
2008
@@ -574,7 +574,7 @@
      * @return the string where all current ivy variables have been substituted by their
value
      */
     public String substitute(String str) {
-        return IvyPatternHelper.substituteVariables(str, getVariables());
+        return IvyPatternHelper.substituteVariables(str, variableContainer);
     }
 
     /**
@@ -583,8 +583,8 @@
      * 
      * @return
      */
-    public Map getVariables() {
-        return variableContainer.getVariables();
+    public IvyVariableContainer getVariables() {
+        return variableContainer;
     }
 
     public Class typeDef(String name, String className) {

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvyVariableContainer.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvyVariableContainer.java?rev=610562&r1=610561&r2=610562&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvyVariableContainer.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvyVariableContainer.java Wed
Jan  9 13:02:58 2008
@@ -17,7 +17,6 @@
  */
 package org.apache.ivy.core.settings;
 
-import java.util.Map;
 
 /**
  * Store and provide access to the ivy variables.
@@ -26,11 +25,18 @@
  */
 public interface IvyVariableContainer extends Cloneable {
 
-    public abstract void setVariable(String varName, String value, boolean overwrite);
+    public void setVariable(String varName, String value, boolean overwrite);
 
-    public abstract Map getVariables();
-
-    public abstract String getVariable(String name);
+    public String getVariable(String name);
 
+    /**
+     * Specifies the prefix used to indicate a variable is an environment
+     * variable. If the prefix doesn't end with a '.', it will be added
+     * automatically.
+     * 
+     * @param prefix the prefix to use for the environment variables
+     */
+    public void setEnvironmentPrefix(String prefix);
+    
     public Object clone();
 }

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvyVariableContainerImpl.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvyVariableContainerImpl.java?rev=610562&r1=610561&r2=610562&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvyVariableContainerImpl.java
(original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/IvyVariableContainerImpl.java
Wed Jan  9 13:02:58 2008
@@ -25,8 +25,17 @@
 
 public class IvyVariableContainerImpl implements IvyVariableContainer {
 
-    private HashMap variables = new HashMap();
+    private Map variables;
+    private String envPrefix;
 
+    public IvyVariableContainerImpl() {
+        this.variables = new HashMap();
+    }
+    
+    public IvyVariableContainerImpl(Map variables) {
+        this.variables = variables;
+    }
+    
     /*
      * (non-Javadoc)
      * 
@@ -40,21 +49,27 @@
         } else {
             Message.debug("'" + varName + "' already set: discarding '" + value + "'");
         }
-
+    }
+    
+    public void setEnvironmentPrefix(String prefix) {
+        if ((prefix != null) && !prefix.endsWith(".")) {
+            this.envPrefix = prefix + ".";
+        } else {
+            this.envPrefix = prefix;
+        }
     }
 
     private String substitute(String value) {
-        return IvyPatternHelper.substituteVariables(value, getVariables());
+        return IvyPatternHelper.substituteVariables(value, this);
     }
 
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.ivy.core.settings.IvyVariableContainer#getVariables()
-     */
-    public Map getVariables() {
+    protected Map getVariables() {
         return variables;
     }
+    
+    protected String getEnvironmentPrefix() {
+        return envPrefix;
+    }
 
     /*
      * (non-Javadoc)
@@ -62,8 +77,14 @@
      * @see org.apache.ivy.core.settings.IvyVariableContainer#getVariable(java.lang.String)
      */
     public String getVariable(String name) {
-        String val = (String) variables.get(name);
-        return val == null ? val : substitute(val);
+        String val = null;
+        if ((envPrefix != null) && name.startsWith(envPrefix)) {
+            val = System.getenv(name.substring(envPrefix.length()));
+        } else {
+            val = (String) variables.get(name);
+        }
+        
+        return val;
     }
 
     public Object clone() {
@@ -73,7 +94,7 @@
         } catch (CloneNotSupportedException e) {
             throw new RuntimeException("unable to clone a " + this.getClass());
         }
-        clone.variables = (HashMap) variables.clone();
+        clone.variables = new HashMap(this.variables);
         return clone;
     }
 }

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/XmlSettingsParser.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/XmlSettingsParser.java?rev=610562&r1=610561&r2=610562&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/XmlSettingsParser.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/core/settings/XmlSettingsParser.java Wed Jan
 9 13:02:58 2008
@@ -221,24 +221,32 @@
                         .booleanValue());
             } else if ("properties".equals(qName)) {
                 String propFilePath = (String) attributes.get("file");
-                String override = (String) attributes.get("override");
-                try {
-                    Message.verbose("loading properties: " + propFilePath);
-                    ivy.loadProperties(new File(propFilePath), override == null ? true :
Boolean
-                            .valueOf(override).booleanValue());
-                } catch (Exception fileEx) {
-                    Message.verbose("failed to load properties as file: trying as url: "
-                            + propFilePath);
+                String environmentPrefix = (String) attributes.get("environment");
+                if (propFilePath != null) {
+                    String override = (String) attributes.get("override");
                     try {
-                        ivy.loadProperties(new URL(propFilePath), override == null ? true
: Boolean
+                        Message.verbose("loading properties: " + propFilePath);
+                        ivy.loadProperties(new File(propFilePath), override == null ? true
: Boolean
                                 .valueOf(override).booleanValue());
-                    } catch (Exception urlEx) {
-                        throw new IllegalArgumentException(
-                            "unable to load properties from "
-                            + propFilePath
-                            + ". Tried both as an url and a file, with no success. File exception:
"
-                            + fileEx + ". URL exception: " + urlEx);
+                    } catch (Exception fileEx) {
+                        Message.verbose("failed to load properties as file: trying as url:
"
+                                + propFilePath);
+                        try {
+                            ivy.loadProperties(new URL(propFilePath), override == null ?
true : Boolean
+                                    .valueOf(override).booleanValue());
+                        } catch (Exception urlEx) {
+                            throw new IllegalArgumentException(
+                                    "unable to load properties from "
+                                            + propFilePath
+                                            + ". Tried both as an url and a file, with no
success. File exception: "
+                                            + fileEx + ". URL exception: " + urlEx);
+                        }
                     }
+                } else if (environmentPrefix != null) {
+                    ivy.getVariableContainer().setEnvironmentPrefix(environmentPrefix);
+                } else {
+                    throw new IllegalArgumentException("Didn't find a 'file' or 'environment'
attribute " +
+                    		"on the 'properties' element");
                 }
             } else if ("include".equals(qName)) {
                 IvyVariableContainer variables = (IvyVariableContainer) ivy.getVariableContainer()

Modified: ant/ivy/core/trunk/test/java/org/apache/ivy/ant/IvyConfigureTest.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/test/java/org/apache/ivy/ant/IvyConfigureTest.java?rev=610562&r1=610561&r2=610562&view=diff
==============================================================================
--- ant/ivy/core/trunk/test/java/org/apache/ivy/ant/IvyConfigureTest.java (original)
+++ ant/ivy/core/trunk/test/java/org/apache/ivy/ant/IvyConfigureTest.java Wed Jan  9 13:02:58
2008
@@ -92,12 +92,12 @@
 
         assertEquals(new File("build/cache"), settings.getDefaultCache());
         assertEquals(new File("test/repositories/ivysettings.xml").getAbsolutePath(), settings
-                .getVariables().get("ivy.settings.file"));
+                .getVariables().getVariable("ivy.settings.file"));
         assertEquals(new File("test/repositories/ivysettings.xml").toURL().toExternalForm(),
-            settings.getVariables().get("ivy.settings.url"));
-        assertEquals(new File("test/repositories").getAbsolutePath(), settings.getVariables().get(
+            settings.getVariables().getVariable("ivy.settings.url"));
+        assertEquals(new File("test/repositories").getAbsolutePath(), settings.getVariables().getVariable(
             "ivy.settings.dir"));
-        assertEquals("myvalue", settings.getVariables().get("myproperty"));
+        assertEquals("myvalue", settings.getVariables().getVariable("myproperty"));
     }
 
     /*@SuppressWarnings*/
@@ -114,9 +114,9 @@
         IvySettings settings = configure.getIvyInstance().getSettings();
 
         assertEquals(new File("build/cache"), settings.getDefaultCache());
-        assertEquals(confUrl, settings.getVariables().get("ivy.settings.url"));
-        assertEquals(confDirUrl, settings.getVariables().get("ivy.settings.dir"));
-        assertEquals("myvalue", settings.getVariables().get("myproperty"));
+        assertEquals(confUrl, settings.getVariables().getVariable("ivy.settings.url"));
+        assertEquals(confDirUrl, settings.getVariables().getVariable("ivy.settings.dir"));
+        assertEquals("myvalue", settings.getVariables().getVariable("myproperty"));
     }
 
     public void testAntProperties() throws Exception {
@@ -129,7 +129,7 @@
         IvySettings settings = configure.getIvyInstance().getSettings();
         assertNotNull(settings);
 
-        assertEquals("myvalue", settings.getVariables().get("myproperty"));
+        assertEquals("myvalue", settings.getVariables().getVariable("myproperty"));
         assertEquals("myvalue", settings.getDefaultCache().getName());
     }
 
@@ -160,7 +160,7 @@
         assertNotNull(settings);
 
         assertEquals("lib/test/[artifact]-[revision].[ext]", 
-            settings.getVariables().get("ivy.retrieve.pattern"));
+            settings.getVariables().getVariable("ivy.retrieve.pattern"));
     }
 
 }

Modified: ant/ivy/core/trunk/test/java/org/apache/ivy/ant/IvyTaskTest.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/test/java/org/apache/ivy/ant/IvyTaskTest.java?rev=610562&r1=610561&r2=610562&view=diff
==============================================================================
--- ant/ivy/core/trunk/test/java/org/apache/ivy/ant/IvyTaskTest.java (original)
+++ ant/ivy/core/trunk/test/java/org/apache/ivy/ant/IvyTaskTest.java Wed Jan  9 13:02:58 2008
@@ -49,14 +49,14 @@
         assertEquals(new File("build/cache"), settings.getDefaultCache());
         // The next test doesn't always works on windows (mix C: and c: drive)
         assertEquals(new File("test/repositories/ivysettings.xml").getAbsolutePath().toUpperCase(),
-            new File((String) settings.getVariables().get("ivy.settings.file")).getAbsolutePath()
+            new File((String) settings.getVariables().getVariable("ivy.settings.file")).getAbsolutePath()
                     .toUpperCase());
         assertEquals(new File("test/repositories/ivysettings.xml").toURL().toExternalForm()
-                .toUpperCase(), ((String) settings.getVariables().get("ivy.settings.url"))
+                .toUpperCase(), ((String) settings.getVariables().getVariable("ivy.settings.url"))
                 .toUpperCase());
         assertEquals(new File("test/repositories").getAbsolutePath().toUpperCase(),
-            ((String) settings.getVariables().get("ivy.settings.dir")).toUpperCase());
-        assertEquals("myvalue", settings.getVariables().get("myproperty"));
+            ((String) settings.getVariables().getVariable("ivy.settings.dir")).toUpperCase());
+        assertEquals("myvalue", settings.getVariables().getVariable("myproperty"));
     }
 
     public void testReferencedSettings() throws MalformedURLException {
@@ -84,12 +84,12 @@
 
         assertEquals(new File("build/cache"), settings.getDefaultCache());
         assertEquals(new File("test/repositories/ivysettings.xml").getAbsolutePath(), settings
-                .getVariables().get("ivy.settings.file"));
+                .getVariables().getVariable("ivy.settings.file"));
         assertEquals(new File("test/repositories/ivysettings.xml").toURL().toExternalForm(),
-            settings.getVariables().get("ivy.settings.url"));
-        assertEquals(new File("test/repositories").getAbsolutePath(), settings.getVariables().get(
+            settings.getVariables().getVariable("ivy.settings.url"));
+        assertEquals(new File("test/repositories").getAbsolutePath(), settings.getVariables().getVariable(
             "ivy.settings.dir"));
-        assertEquals("myvalue", settings.getVariables().get("myproperty"));
+        assertEquals("myvalue", settings.getVariables().getVariable("myproperty"));
 
     }
 



Mime
View raw message