Author: xavier
Date: Mon Jul  9 06:15:08 2007
New Revision: 554634

URL: http://svn.apache.org/viewvc?view=rev&rev=554634
Log:
IMPROVEMENT: expose Ivy variables as Ant properties (IVY-564)

Added:
    
incubator/ivy/core/trunk/test/java/org/apache/ivy/ant/IvyAntSettingsTest.java   
(with props)
Modified:
    incubator/ivy/core/trunk/CHANGES.txt
    incubator/ivy/core/trunk/doc/use/settings.html
    incubator/ivy/core/trunk/src/java/org/apache/ivy/ant/IvyAntSettings.java
    
incubator/ivy/core/trunk/src/java/org/apache/ivy/ant/IvyAntVariableContainer.java
    incubator/ivy/core/trunk/test/java/org/apache/ivy/ant/IvyConfigureTest.java
    
incubator/ivy/core/trunk/test/java/org/apache/ivy/ant/ivysettings-props.properties

Modified: incubator/ivy/core/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/incubator/ivy/core/trunk/CHANGES.txt?view=diff&rev=554634&r1=554633&r2=554634
==============================================================================
--- incubator/ivy/core/trunk/CHANGES.txt (original)
+++ incubator/ivy/core/trunk/CHANGES.txt Mon Jul  9 06:15:08 2007
@@ -54,6 +54,7 @@
 - NEW: A checkstyle report is generated (IVY-483) (thanks to Jan Materne)
 - NEW: Hide private or specific conf when publishing (IVY-77)
 
+- IMPROVEMENT: Expose Ivy variables as Ant Properties (IVY-564)
 - IMPROVEMENT: Change default cache location (IVY-530)
 - IMPROVEMENT: Upgraded VFS dependency to 1.0 and removed dependency on 
VFS-sandbox (IVY-498)
 - IMPROVEMENT: Use maven2 repository to download dependencies

Modified: incubator/ivy/core/trunk/doc/use/settings.html
URL: 
http://svn.apache.org/viewvc/incubator/ivy/core/trunk/doc/use/settings.html?view=diff&rev=554634&r1=554633&r2=554634
==============================================================================
--- incubator/ivy/core/trunk/doc/use/settings.html (original)
+++ incubator/ivy/core/trunk/doc/use/settings.html Mon Jul  9 06:15:08 2007
@@ -26,12 +26,22 @@
        <textarea id="xooki-source">
 <span class="since">(since 2.0)</span>
 
-The settings declaration is used to configure ivy with an xml settings 
file.<br/><br/>
+The settings declaration is used to configure ivy with an xml settings file.
 
-See <a href="../doc/configuration.html">Settings Files</a> for details about 
the settings file itself.<br/><br/>
+See <a href="../doc/configuration.html">Settings Files</a> for details about 
the settings file itself.
 
-Multiple settings can be defined in a build script.  Every task can reference 
his own settings.<br/><br/>
+Multiple settings can be defined in a build script.  Every task can reference 
its own settings.
 
+All Ivy variables set during the settings are available in the Ant project as 
long as they were not set in Ant before (Ant properties are immutable). 
+Moreover, the variables are exposed under two names: the variable name, and 
the variable name suffixed by dot + the settings id (if any). 
+For instance, if you load a settings with the id 'myid', and define a variable 
my.variable=my.value in the Ivy settings, both my.variable and my.variable.myid 
will now be available as properties in Ant and equal to 'my.value'. If you 
later load another settings with the id 'yourid', and in this settings assign 
the variable 'my.variable' the value 'your.value', in the Ant project you will 
have:
+<code>
+my.variable=my.value
+my.variable.myid=my.value
+my.variable.yourid=your.value
+</code>
+
+<h2>Attributes</h2>
 <table class="ant">
 <thead>
     <tr><th class="ant-att">Attribute</th><th 
class="ant-desc">Description</th><th class="ant-req">Required</th></tr>

Modified: 
incubator/ivy/core/trunk/src/java/org/apache/ivy/ant/IvyAntSettings.java
URL: 
http://svn.apache.org/viewvc/incubator/ivy/core/trunk/src/java/org/apache/ivy/ant/IvyAntSettings.java?view=diff&rev=554634&r1=554633&r2=554634
==============================================================================
--- incubator/ivy/core/trunk/src/java/org/apache/ivy/ant/IvyAntSettings.java 
(original)
+++ incubator/ivy/core/trunk/src/java/org/apache/ivy/ant/IvyAntSettings.java 
Mon Jul  9 06:15:08 2007
@@ -94,6 +94,8 @@
     private String userName = null;
 
     private String passwd = null;
+    
+    private String id = null;
 
     /**
      * Returns the default ivy settings of this classloader. If it doesn't 
exist yet, a new one is
@@ -190,6 +192,14 @@
     public void setUrl(String confUrl) throws MalformedURLException {
         this.url = new URL(confUrl);
     }
+    
+    /*
+     * This is usually not necessary to define a reference in Ant, but it's 
the only
+     * way to know the id of the settings, which we use to set ant properties.
+     */
+    public void setId(String id) {
+        this.id = id;
+    }
 
     /*
      * public void execute() throws BuildException { 
@@ -240,6 +250,7 @@
                 }
                 ivy.configure(url);
             }
+            ivyAntVariableContainer.updateProject(id);
         } catch (ParseException e) {
             throw new BuildException("impossible to configure ivy:settings 
with given "
                     + (file != null ? "file: " + file : "url :" + url) + " :" 
+ e, e);

Modified: 
incubator/ivy/core/trunk/src/java/org/apache/ivy/ant/IvyAntVariableContainer.java
URL: 
http://svn.apache.org/viewvc/incubator/ivy/core/trunk/src/java/org/apache/ivy/ant/IvyAntVariableContainer.java?view=diff&rev=554634&r1=554633&r2=554634
==============================================================================
--- 
incubator/ivy/core/trunk/src/java/org/apache/ivy/ant/IvyAntVariableContainer.java
 (original)
+++ 
incubator/ivy/core/trunk/src/java/org/apache/ivy/ant/IvyAntVariableContainer.java
 Mon Jul  9 06:15:08 2007
@@ -18,7 +18,9 @@
 package org.apache.ivy.ant;
 
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
+import java.util.Map.Entry;
 
 import org.apache.ivy.core.settings.IvyVariableContainer;
 import org.apache.ivy.core.settings.IvyVariableContainerImpl;
@@ -59,6 +61,39 @@
             overwrittenProperties.put(varName, value);
         } else {
             super.setVariable(varName, value, overwrite);
+        }
+    }
+
+    /**
+     * Updates the Ant Project used in this container with variables set in 
Ivy.
+     * 
+     * All variables defined in Ivy will be set in the Ant project under two 
names:
+     * <ul>
+     * <li>the name of the variable</li>
+     * <li>the name of the variable suffxied with a dot + the given id, 
+     * if the given id is not null</li>
+     * </ul>
+     * 
+     * @param
+     *      id  The identifier of the settings in which the variables have 
been set, which
+     *          should be used as property names suffix
+     */
+    public void updateProject(String id) {
+        Map r = new HashMap(super.getVariables());
+        r.putAll(overwrittenProperties);
+        for (Iterator it = r.entrySet().iterator(); it.hasNext();) {
+            Entry entry = (Entry) it.next();
+            
+            setPropertyIfNotSet((String) entry.getKey(), (String) 
entry.getValue());
+            if (id != null) {
+                setPropertyIfNotSet((String) entry.getKey() + "." + id, 
(String) entry.getValue());
+            }
+        }
+    }
+
+    private void setPropertyIfNotSet(String property, String value) {
+        if (project.getProperty(property) == null) {
+            project.setProperty(property, value);
         }
     }
 }

Added: 
incubator/ivy/core/trunk/test/java/org/apache/ivy/ant/IvyAntSettingsTest.java
URL: 
http://svn.apache.org/viewvc/incubator/ivy/core/trunk/test/java/org/apache/ivy/ant/IvyAntSettingsTest.java?view=auto&rev=554634
==============================================================================
--- 
incubator/ivy/core/trunk/test/java/org/apache/ivy/ant/IvyAntSettingsTest.java 
(added)
+++ 
incubator/ivy/core/trunk/test/java/org/apache/ivy/ant/IvyAntSettingsTest.java 
Mon Jul  9 06:15:08 2007
@@ -0,0 +1,50 @@
+/*
+ *  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.ivy.ant;
+
+import org.apache.tools.ant.Project;
+
+import junit.framework.TestCase;
+
+public class IvyAntSettingsTest extends TestCase {
+    private IvyAntSettings antSettings;
+
+    protected void setUp() throws Exception {
+        Project project = new Project();
+        project.setProperty("myproperty", "myvalue");
+
+        antSettings = new IvyAntSettings();
+        antSettings.setProject(project);
+    }
+
+    public void testExposeAntProperties() throws Exception {
+        String confUrl = 
IvyConfigureTest.class.getResource("ivysettings-props.xml")
+                .toExternalForm();
+        antSettings.setUrl(confUrl);
+        antSettings.setId("this.id");
+
+        assertNotNull(antSettings.getConfiguredIvyInstance());
+
+        assertEquals("value", 
+            antSettings.getProject().getProperty("ivy.test.variable"));
+        assertEquals("value", 
+            antSettings.getProject().getProperty("ivy.test.variable.this.id"));
+    }
+
+
+}

Propchange: 
incubator/ivy/core/trunk/test/java/org/apache/ivy/ant/IvyAntSettingsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
incubator/ivy/core/trunk/test/java/org/apache/ivy/ant/IvyConfigureTest.java
URL: 
http://svn.apache.org/viewvc/incubator/ivy/core/trunk/test/java/org/apache/ivy/ant/IvyConfigureTest.java?view=diff&rev=554634&r1=554633&r2=554634
==============================================================================
--- incubator/ivy/core/trunk/test/java/org/apache/ivy/ant/IvyConfigureTest.java 
(original)
+++ incubator/ivy/core/trunk/test/java/org/apache/ivy/ant/IvyConfigureTest.java 
Mon Jul  9 06:15:08 2007
@@ -33,27 +33,27 @@
  * IvyConfigure will be removed, this class should be renamed 
AntIvySettingsTest
  */
 public class IvyConfigureTest extends TestCase {
-    private IvyConfigure _configure;
+    private IvyConfigure configure;
 
     protected void setUp() throws Exception {
         Project project = new Project();
         project.setProperty("myproperty", "myvalue");
 
-        _configure = new IvyConfigure();
-        _configure.setProject(project);
+        configure = new IvyConfigure();
+        configure.setProject(project);
     }
 
     private Ivy getIvyInstance() {
-        return IvyAntSettings.getDefaultInstance(_configure.getProject())
+        return IvyAntSettings.getDefaultInstance(configure.getProject())
                 .getConfiguredIvyInstance();
     }
 
     public void testDefault() throws Exception {
         // by default configure look in the current directory for an 
ivysettings.xml file...
         // but Ivy itself has one, and we don't want to use it
-        _configure.getProject()
+        configure.getProject()
                 .setProperty("ivy.settings.file", 
"no/settings/will/use/default.xml");
-        _configure.execute();
+        configure.execute();
 
         IvySettings settings = getIvyInstance().getSettings();
         assertNotNull(settings.getDefaultResolver());
@@ -68,10 +68,10 @@
     public void testDefault14() throws Exception {
         // by default configure look in the current directory for an 
ivysettings.xml file...
         // but Ivy itself has one, and we don't want to use it
-        _configure.getProject()
+        configure.getProject()
                 .setProperty("ivy.settings.file", 
"no/settings/will/use/default.xml");
-        _configure.getProject().setProperty("ivy.14.compatible", "true");
-        _configure.execute();
+        configure.getProject().setProperty("ivy.14.compatible", "true");
+        configure.execute();
 
         IvySettings settings = getIvyInstance().getSettings();
         assertNotNull(settings.getDefaultResolver());
@@ -81,11 +81,11 @@
     }
 
     public void testFile() throws Exception {
-        _configure.setFile(new File("test/repositories/ivysettings.xml"));
+        configure.setFile(new File("test/repositories/ivysettings.xml"));
 
-        _configure.execute();
+        configure.execute();
 
-        Ivy ivy = _configure.getIvyInstance();
+        Ivy ivy = configure.getIvyInstance();
         assertNotNull(ivy);
         IvySettings settings = ivy.getSettings();
         assertNotNull(settings);
@@ -106,11 +106,11 @@
         if (confDirUrl.endsWith("/")) {
             confDirUrl = confDirUrl.substring(0, confDirUrl.length() - 1);
         }
-        _configure.setUrl(confUrl);
+        configure.setUrl(confUrl);
 
-        _configure.execute();
+        configure.execute();
 
-        IvySettings settings = _configure.getIvyInstance().getSettings();
+        IvySettings settings = configure.getIvyInstance().getSettings();
 
         assertEquals(new File("build/cache"), settings.getDefaultCache());
         assertEquals(confUrl, settings.getVariables().get("ivy.settings.url"));
@@ -121,29 +121,42 @@
     public void testAntProperties() throws Exception {
         String confUrl = 
IvyConfigureTest.class.getResource("ivysettings-test.xml")
                 .toExternalForm();
-        _configure.setUrl(confUrl);
+        configure.setUrl(confUrl);
 
-        _configure.execute();
+        configure.execute();
 
-        IvySettings settings = _configure.getIvyInstance().getSettings();
+        IvySettings settings = configure.getIvyInstance().getSettings();
         assertNotNull(settings);
 
         assertEquals("myvalue", settings.getVariables().get("myproperty"));
         assertEquals("myvalue", settings.getDefaultCache().getName());
     }
 
+    public void testExposeAntProperties() throws Exception {
+        String confUrl = 
IvyConfigureTest.class.getResource("ivysettings-props.xml")
+                .toExternalForm();
+        configure.setUrl(confUrl);
+        
+        configure.execute();
+
+        assertNotNull(configure.getIvyInstance());
+
+        assertEquals("value", 
+            configure.getProject().getProperty("ivy.test.variable"));
+    }
+
     public void testOverrideVariables() throws Exception {
         String confUrl = 
IvyConfigureTest.class.getResource("ivysettings-props.xml")
                 .toExternalForm();
-        _configure.setUrl(confUrl);
+        configure.setUrl(confUrl);
 
-        _configure.execute();
+        configure.execute();
 
-        IvySettings settings = _configure.getIvyInstance().getSettings();
+        IvySettings settings = configure.getIvyInstance().getSettings();
         assertNotNull(settings);
 
-        assertEquals("lib/test/[artifact]-[revision].[ext]", 
settings.getVariables().get(
-            "ivy.retrieve.pattern"));
+        assertEquals("lib/test/[artifact]-[revision].[ext]", 
+            settings.getVariables().get("ivy.retrieve.pattern"));
     }
 
 }

Modified: 
incubator/ivy/core/trunk/test/java/org/apache/ivy/ant/ivysettings-props.properties
URL: 
http://svn.apache.org/viewvc/incubator/ivy/core/trunk/test/java/org/apache/ivy/ant/ivysettings-props.properties?view=diff&rev=554634&r1=554633&r2=554634
==============================================================================
--- 
incubator/ivy/core/trunk/test/java/org/apache/ivy/ant/ivysettings-props.properties
 (original)
+++ 
incubator/ivy/core/trunk/test/java/org/apache/ivy/ant/ivysettings-props.properties
 Mon Jul  9 06:15:08 2007
@@ -17,3 +17,4 @@
 #       * under the License.
 #       ***************************************************************
 ivy.retrieve.pattern = lib/test/[artifact]-[revision].[ext]
+ivy.test.variable = value
\ No newline at end of file


Reply via email to