You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by ma...@apache.org on 2010/08/11 23:38:00 UTC

svn commit: r984592 - in /ant/ivy/core/branches/2.2.x: ./ doc/use/ src/java/org/apache/ivy/ant/ src/java/org/apache/ivy/plugins/parser/m2/

Author: maartenc
Date: Wed Aug 11 21:37:59 2010
New Revision: 984592

URL: http://svn.apache.org/viewvc?rev=984592&view=rev
Log:
Merged changes for IVY-770 from trunk into 2.2.x branch.

Modified:
    ant/ivy/core/branches/2.2.x/CHANGES.txt
    ant/ivy/core/branches/2.2.x/doc/use/makepom.html
    ant/ivy/core/branches/2.2.x/src/java/org/apache/ivy/ant/IvyMakePom.java
    ant/ivy/core/branches/2.2.x/src/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorWriter.java
    ant/ivy/core/branches/2.2.x/src/java/org/apache/ivy/plugins/parser/m2/PomWriterOptions.java

Modified: ant/ivy/core/branches/2.2.x/CHANGES.txt
URL: http://svn.apache.org/viewvc/ant/ivy/core/branches/2.2.x/CHANGES.txt?rev=984592&r1=984591&r2=984592&view=diff
==============================================================================
--- ant/ivy/core/branches/2.2.x/CHANGES.txt (original)
+++ ant/ivy/core/branches/2.2.x/CHANGES.txt Wed Aug 11 21:37:59 2010
@@ -112,6 +112,7 @@ for detailed view of each issue, please 
 =====================================
 - IMPROVEMENT: Handle attributes in description subelements (IVY-1214) (thanks to Jean-Louis Boudart)
 - IMPROVEMENT: Use IvyAuthenticator only when it is really necessary (IVY-1211)
+- IMPROVEMENT: MakePom task handling of mulitple artifacts for Maven2 (IVY-707) (thanks to Jesper Pedersen)
 - IMPROVEMENT: ivy:makepom now has an option to disable the generation of an extra Ivy comment block in the POM
 - IMPROVEMENT: ivy:makepom now accepts a list of configurations to include (IVY-1005) (thanks to Jesper Pedersen)
 

Modified: ant/ivy/core/branches/2.2.x/doc/use/makepom.html
URL: http://svn.apache.org/viewvc/ant/ivy/core/branches/2.2.x/doc/use/makepom.html?rev=984592&r1=984591&r2=984592&view=diff
==============================================================================
--- ant/ivy/core/branches/2.2.x/doc/use/makepom.html (original)
+++ ant/ivy/core/branches/2.2.x/doc/use/makepom.html Wed Aug 11 21:37:59 2010
@@ -42,10 +42,18 @@ An example of use is to publish an Ivy m
         <td>Yes</td></tr>
     <tr><td>pomfile</td><td>the destination pom file to write</td>
         <td>Yes</td></tr>
-    <tr><td>conf</td><td>a comma separated list of the configurations to include in the generated pom. Wildcards are supported here. <span class="since">(since 2.2)</span></td><td>No, defaults to all configurations.</td></tr>
-<tr><td>settingsRef</td><td>A reference to the ivy settings that must be used by this task</td><td>No, 'ivy.instance' is taken by default.</td></tr>
+    <tr><td>artifactName</td><td>The name of the artifact which is represented by the generated pom file. <span class="since">(since 2.2)</span></td>
+        <td>No, defaults to the module name in the source ivy file.</td></tr>
+    <tr><td>artifactPackaging</td><td>The packaging of the artifact which is represented by the generated pom file. <span class="since">(since 2.2)</span></td>
+        <td>No, the artifact ext is taken by default. Defaults to 'pom' if no such artifact is defined.</td></tr>
+    <tr><td>conf</td><td>a comma separated list of the configurations to include in the generated pom. Wildcards are supported here. <span class="since">(since 2.2)</span></td>
+        <td>No, defaults to all configurations.</td></tr>
+    <tr><td>settingsRef</td><td>A reference to the ivy settings that must be used by this task</td>
+        <td>No, 'ivy.instance' is taken by default.</td></tr>
     <tr><td>printIvyInfo</td><td>Add some information about Ivy to the generated POM. <span class="since">(since 2.2)</span></td>
         <td>No, defaults to 'true'.</td></tr>
+    <tr><td>headerFile</td><td>the header of the generated pom file</td>
+        <td>No</td></tr>
 </tbody>
 </table>
 <h1>Child elements</h1>
@@ -57,18 +65,23 @@ An example of use is to publish an Ivy m
     <tr><td>mapping</td>
         <td>describes the mapping from an Ivy module configuration to a Maven POM scope.<br/>These elements takes two attributes: <ul><li>conf</li> the configuration to map<li>scope</li>the scope to which it should be mapped</ul></td>
         <td>0..n</td></tr>
+    <tr><td>dependency</td>
+        <td>describes extra dependencies that should be added to the generated Maven POM file.<br/>These elements takes the following attributes: <ul><li>group</li> the groupId. Default <i>organisation</i> as defined in <i>info</i><li>artifact</li> the name of the artifact<li>version</li> the version. Default <i>revision</i> as defined in <i>info</i><li>scope</li> the scope<li>optional</li> is the artifact optional. Default <i>false</i></ul></td>
+        <td>0..n</td></tr>
 </tbody>
 </table>
 
 <h1>Examples</h1>
 <code type="xml">
-<ivy:makepom ivyfile="${basedir}/path/to/ivy.xml" pomfile="${basedir}/path/to/module.pom">
+<ivy:makepom ivyfile="${basedir}/path/to/ivy.xml" pomfile="${basedir}/path/to/module.pom" conf="default,runtime">
    <mapping conf="default" scope="compile"/>
    <mapping conf="runtime" scope="runtime"/>
+   <dependency group="com.acme" artifact="acme-logging" version="1.0" optional="true"/>
 </ivy:makepom>
 </code>
-Converts ${basedir}/path/to/ivy.xml to a pom and writes the result to ${basedir}/path/to/module.pom. The configuration 'default' in the parsed ivy file will be mapped to the scope 'compile', the configuration 'runtime' will be mapped to 'runtime', and other configurations will be considered optional.
+Converts ${basedir}/path/to/ivy.xml to a pom and writes the result to ${basedir}/path/to/module.pom. The configuration 'default' in the parsed ivy file will be mapped to the scope 'compile', the configuration 'runtime' will be mapped to 'runtime', and other configurations will be excluded.
 
+The <i>com.acme.acme-logging</i> artifact with version 1.0 will be added as an optional dependency.
 	</textarea>
 <script type="text/javascript">xooki.postProcess();</script>
 </body>

Modified: ant/ivy/core/branches/2.2.x/src/java/org/apache/ivy/ant/IvyMakePom.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/branches/2.2.x/src/java/org/apache/ivy/ant/IvyMakePom.java?rev=984592&r1=984591&r2=984592&view=diff
==============================================================================
--- ant/ivy/core/branches/2.2.x/src/java/org/apache/ivy/ant/IvyMakePom.java (original)
+++ ant/ivy/core/branches/2.2.x/src/java/org/apache/ivy/ant/IvyMakePom.java Wed Aug 11 21:37:59 2010
@@ -25,12 +25,12 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
 import org.apache.ivy.plugins.parser.m2.PomModuleDescriptorWriter;
 import org.apache.ivy.plugins.parser.m2.PomWriterOptions;
-import org.apache.ivy.plugins.parser.m2.PomModuleDescriptorWriter.ConfigurationScopeMapping;
 import org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorParser;
 import org.apache.ivy.util.FileUtil;
 import org.apache.tools.ant.BuildException;
@@ -43,6 +43,7 @@ public class IvyMakePom extends IvyTask 
     public class Mapping {
         private String conf;
         private String scope;
+        
         public String getConf() {
             return conf;
         }
@@ -57,6 +58,49 @@ public class IvyMakePom extends IvyTask 
         }
     }
 
+    public class Dependency {
+        private String group = null;
+        private String artifact = null;
+        private String version = null;
+        private String scope = null;
+        private boolean optional = false;
+        
+        public String getGroup() {
+            return group;
+        }
+        public void setGroup(String group) {
+            this.group = group;
+        }
+        public String getArtifact() {
+            return artifact;
+        }
+        public void setArtifact(String artifact) {
+            this.artifact = artifact;
+        }
+        public String getVersion() {
+            return version;
+        }
+        public void setVersion(String version) {
+            this.version = version;
+        }
+        public String getScope() {
+            return scope;
+        }
+        public void setScope(String scope) {
+            this.scope = scope;
+        }
+        public boolean getOptional() {
+            return optional;
+        }
+        public void setOptional(boolean optional) {
+            this.optional = optional;
+        }      
+    }
+            
+    private String artifactName;
+            
+    private String artifactPackaging;
+
     private File pomFile = null;
 
     private File headerFile = null;
@@ -68,6 +112,8 @@ public class IvyMakePom extends IvyTask 
     private File ivyFile = null;
 
     private Collection mappings = new ArrayList();
+    
+    private Collection dependencies = new ArrayList();
 
     public File getPomFile() {
         return pomFile;
@@ -104,17 +150,39 @@ public class IvyMakePom extends IvyTask 
     public String getConf() {
         return conf;
     }
-   
+    
     public void setConf(String conf) {
         this.conf = conf;
     }
-   
+    
+    public String getArtifactName() {
+        return artifactName;
+    }
+
+    public void setArtifactName(String artifactName) {
+        this.artifactName = artifactName;
+    }
+
+    public String getArtifactPackaging() {
+        return artifactPackaging;
+    }
+
+    public void setArtifactPackaging(String artifactPackaging) {
+        this.artifactPackaging = artifactPackaging;
+    }
+
     public Mapping createMapping() {
         Mapping mapping = new Mapping();
         this.mappings.add(mapping);
         return mapping;
     }
     
+    public Dependency createDependency() {
+        Dependency dependency = new Dependency();
+        this.dependencies.add(dependency);
+        return dependency;
+    }
+    
     public void doExecute() throws BuildException {
         try {
             if (ivyFile == null) {
@@ -140,12 +208,14 @@ public class IvyMakePom extends IvyTask 
     
     private PomWriterOptions getPomWriterOptions() throws IOException {
         PomWriterOptions options = new PomWriterOptions();
-        options.setConfs(splitConfs(conf)).setPrintIvyInfo(isPrintIvyInfo());
+        options.setConfs(splitConfs(conf))
+               .setArtifactName(getArtifactName())
+               .setArtifactPackaging(getArtifactPackaging())
+               .setPrintIvyInfo(isPrintIvyInfo())
+               .setExtraDependencies(getDependencies());
         
-        if (mappings.isEmpty()) {
-            options.setMapping(PomModuleDescriptorWriter.DEFAULT_MAPPING);
-        } else {
-            options.setMapping(new ConfigurationScopeMapping(getMappingsMap()));
+        if (!mappings.isEmpty()) {
+            options.setMapping(new PomWriterOptions.ConfigurationScopeMapping(getMappingsMap()));
         }
         
         if (headerFile != null) {
@@ -163,4 +233,15 @@ public class IvyMakePom extends IvyTask 
         }
         return mappingsMap;
     }
+    
+    private List getDependencies() {
+        List result = new ArrayList();
+        for (Iterator iter = dependencies.iterator(); iter.hasNext();) {
+            Dependency dependency = (Dependency) iter.next();
+            result.add(new PomWriterOptions.ExtraDependency(dependency.getGroup(),
+                    dependency.getArtifact(), dependency.getVersion(), dependency.getScope(),
+                    dependency.getOptional()));
+        }
+        return result;
+    }
 }

Modified: ant/ivy/core/branches/2.2.x/src/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorWriter.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/branches/2.2.x/src/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorWriter.java?rev=984592&r1=984591&r2=984592&view=diff
==============================================================================
--- ant/ivy/core/branches/2.2.x/src/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorWriter.java (original)
+++ ant/ivy/core/branches/2.2.x/src/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorWriter.java Wed Aug 11 21:37:59 2010
@@ -24,18 +24,31 @@ import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
 
 import org.apache.ivy.Ivy;
 import org.apache.ivy.core.module.descriptor.Artifact;
 import org.apache.ivy.core.module.descriptor.DependencyDescriptor;
 import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
 import org.apache.ivy.core.module.id.ModuleRevisionId;
+import org.apache.ivy.plugins.parser.m2.PomWriterOptions.ConfigurationScopeMapping;
+import org.apache.ivy.plugins.parser.m2.PomWriterOptions.ExtraDependency;
 import org.apache.ivy.util.ConfigurationUtils;
-import org.apache.ivy.util.StringUtils;
 
 public final class PomModuleDescriptorWriter {
+    
+    private static final ConfigurationScopeMapping DEFAULT_MAPPING 
+            = new ConfigurationScopeMapping(new HashMap() {
+                {
+                    put("compile, runtime", "compile");
+                    put("runtime", "runtime");
+                    put("provided", "provided");
+                    put("test", "test");
+                    put("system", "system");
+                }
+            });
+
 
     private PomModuleDescriptorWriter() {
     }
@@ -65,7 +78,7 @@ public final class PomModuleDescriptorWr
             out.println("    xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 "
                     + "http://maven.apache.org/maven-v4_0_0.xsd\">\n");
             out.println("  <modelVersion>4.0.0</modelVersion>");
-            printModuleId(md, out);
+            printModuleId(md, out, options);
             printDependencies(md, out, options);
             out.println("</project>");
         } finally {
@@ -73,22 +86,29 @@ public final class PomModuleDescriptorWr
         }
     }
 
-    private static void printModuleId(ModuleDescriptor md, PrintWriter out) {
+    private static void printModuleId(ModuleDescriptor md, PrintWriter out, PomWriterOptions options) {
         ModuleRevisionId mrid = md.getModuleRevisionId();
         out.println("  <groupId>" + mrid.getOrganisation() + "</groupId>");
-        out.println("  <artifactId>" + mrid.getName() + "</artifactId>");
         
-        String type;
+        String artifactId = options.getArtifactName();
+        if (artifactId == null) {
+            artifactId = mrid.getName();
+        }
         
-        Artifact artifact = findArtifact(md);
-        if (artifact == null) {
-            // no suitable artifact found, default to 'pom'
-            type = "pom";
-        } else {
-            type = artifact.getType();
+        String packaging = options.getArtifactPackaging();
+        if (packaging == null) {
+            // find artifact to determine the packaging
+            Artifact artifact = findArtifact(md, artifactId);
+            if (artifact == null) {
+                // no suitable artifact found, default to 'pom'
+                packaging = "pom";
+            } else {
+                packaging = artifact.getType();
+            }
         }
-
-        out.println("  <packaging>" + type + "</packaging>");
+        
+        out.println("  <artifactId>" + artifactId + "</artifactId>");
+        out.println("  <packaging>" + packaging + "</packaging>");
         if (mrid.getRevision() != null) {
             out.println("  <version>" + mrid.getRevision() + "</version>");
         }
@@ -100,10 +120,10 @@ public final class PomModuleDescriptorWr
     /**
      * Returns the first artifact with the correct name and without a classifier.
      */
-    private static Artifact findArtifact(ModuleDescriptor md) {
+    private static Artifact findArtifact(ModuleDescriptor md, String artifactName) {
         Artifact[] artifacts = md.getAllArtifacts();
         for (int i = 0; i < artifacts.length; i++) {
-            if (artifacts[i].getName().equals(md.getModuleRevisionId().getName())
+            if (artifacts[i].getName().equals(artifactName)
                     && artifacts[i].getAttribute("classifier") == null) {
                 return artifacts[i];
             }
@@ -114,10 +134,43 @@ public final class PomModuleDescriptorWr
 
     private static void printDependencies(ModuleDescriptor md, PrintWriter out, 
             PomWriterOptions options) {
+        List extraDeps = options.getExtraDependencies();
         DependencyDescriptor[] dds = getDependencies(md, options);
-        if (dds.length > 0) {
-            ConfigurationScopeMapping mapping = options.getMapping();
+
+        if (!extraDeps.isEmpty() || (dds.length > 0)) {
             out.println("  <dependencies>");
+
+            // print the extra dependencies first
+            for (Iterator it = extraDeps.iterator(); it.hasNext(); ) {
+                PomWriterOptions.ExtraDependency dep = (ExtraDependency) it.next();
+
+                out.println("    <dependency>");
+                String groupId = dep.getGroup();
+                if (groupId == null) {
+                    groupId = md.getModuleRevisionId().getOrganisation();
+                }
+                out.println("      <groupId>" + groupId + "</groupId>");
+                out.println("      <artifactId>" + dep.getArtifact() + "</artifactId>");
+                String version = dep.getVersion();
+                if (version == null) {
+                    version = md.getModuleRevisionId().getRevision();
+                }
+                out.println("      <version>" + version + "</version>");
+                if (dep.getScope() != null) {
+                    out.println("      <scope>" + dep.getScope() + "</scope>");
+                }
+                if (dep.isOptional()) {
+                    out.println("      <optional>true</optional>");
+                }
+                out.println("    </dependency>");
+            }
+            
+            // now print the dependencies listed in the ModuleDescriptor
+            ConfigurationScopeMapping mapping = options.getMapping();
+            if (mapping == null) {
+                mapping = DEFAULT_MAPPING;
+            }
+            
             for (int i = 0; i < dds.length; i++) {
                 ModuleRevisionId mrid = dds[i].getDependencyRevisionId();
                 out.println("    <dependency>");
@@ -152,36 +205,4 @@ public final class PomModuleDescriptorWr
         
         return (DependencyDescriptor[]) result.toArray(new DependencyDescriptor[result.size()]);
     }
-    
-    public static final ConfigurationScopeMapping DEFAULT_MAPPING 
-        = new ConfigurationScopeMapping(new HashMap() {
-            {
-                put("compile, runtime", "compile");
-                put("runtime", "runtime");
-                put("provided", "provided");
-                put("test", "test");
-                put("system", "system");
-            }
-        });
-
-    public static class ConfigurationScopeMapping {
-        private Map/*<String,String>*/ scopes;
-        
-        public ConfigurationScopeMapping(Map/*<String,String>*/ scopesMapping) {
-            this.scopes = new HashMap(scopesMapping);
-        }
-
-        /**
-         * Returns the scope mapped to the given configuration array.
-         * 
-         * @param confs the configurations for which the scope should be returned
-         * @return the scope to which the conf is mapped
-         */
-        public String getScope(String[] confs) {
-            return (String) scopes.get(StringUtils.join(confs, ", "));
-        }
-        public boolean isOptional(String[] confs) {
-            return getScope(confs) == null;
-        }
-    }
 }

Modified: ant/ivy/core/branches/2.2.x/src/java/org/apache/ivy/plugins/parser/m2/PomWriterOptions.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/branches/2.2.x/src/java/org/apache/ivy/plugins/parser/m2/PomWriterOptions.java?rev=984592&r1=984591&r2=984592&view=diff
==============================================================================
--- ant/ivy/core/branches/2.2.x/src/java/org/apache/ivy/plugins/parser/m2/PomWriterOptions.java (original)
+++ ant/ivy/core/branches/2.2.x/src/java/org/apache/ivy/plugins/parser/m2/PomWriterOptions.java Wed Aug 11 21:37:59 2010
@@ -17,9 +17,15 @@
  */
 package org.apache.ivy.plugins.parser.m2;
 
-import org.apache.ivy.plugins.parser.m2.PomModuleDescriptorWriter.ConfigurationScopeMapping;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.ivy.util.StringUtils;
 
 public class PomWriterOptions {
+
     private String[] confs;
     
     private String licenseHeader;
@@ -27,6 +33,12 @@ public class PomWriterOptions {
     private ConfigurationScopeMapping mapping;
     
     private boolean printIvyInfo = true;
+    
+    private String artifactName;
+    
+    private String artifactPackaging;
+    
+    private List/*<ExtraDependency>*/ extraDependencies = new ArrayList();
 
     public String[] getConfs() {
         return confs;
@@ -47,9 +59,6 @@ public class PomWriterOptions {
     }
 
     public ConfigurationScopeMapping getMapping() {
-        if (mapping == null) {
-            return PomModuleDescriptorWriter.DEFAULT_MAPPING;
-        }
         return mapping;
     }
 
@@ -67,6 +76,85 @@ public class PomWriterOptions {
         return this;
     }
     
+    public List/*<ExtraDependency>*/ getExtraDependencies() {
+        return extraDependencies;
+    }
+
+    public PomWriterOptions setExtraDependencies(List/*<ExtraDependency>*/ extraDependencies) {
+        this.extraDependencies = extraDependencies;
+        return this;
+    }
     
+    public String getArtifactName() {
+        return artifactName;
+    }
+
+    public PomWriterOptions setArtifactName(String artifactName) {
+        this.artifactName = artifactName;
+        return this;
+    }
+
+    public String getArtifactPackaging() {
+        return artifactPackaging;
+    }
+
+    public PomWriterOptions setArtifactPackaging(String artifactPackaging) {
+        this.artifactPackaging = artifactPackaging;
+        return this;
+    }
+
+    public static class ConfigurationScopeMapping {
+        private Map/*<String,String>*/ scopes;
+        
+        public ConfigurationScopeMapping(Map/*<String,String>*/ scopesMapping) {
+            this.scopes = new HashMap(scopesMapping);
+        }
+
+        /**
+         * Returns the scope mapped to the given configuration array.
+         * 
+         * @param confs the configurations for which the scope should be returned
+         * @return the scope to which the conf is mapped
+         */
+        public String getScope(String[] confs) {
+            return (String) scopes.get(StringUtils.join(confs, ", "));
+        }
+        public boolean isOptional(String[] confs) {
+            return getScope(confs) == null;
+        }
+    }
+
+    public static class ExtraDependency {
+            private String group;
+            private String artifact;
+            private String version;
+            private String scope;
+            private boolean optional;
+            
+            public ExtraDependency(String group, String artifact, String version, String scope, boolean optional) {
+                this.group = group;
+                this.artifact = artifact;
+                this.version = version;
+                this.scope = scope;
+                this.optional = optional;
+            }
+            
+            public String getGroup() {
+                return group;
+            }
+            public String getArtifact() {
+                return artifact;
+            }
+            public String getVersion() {
+                return version;
+            }
+            public String getScope() {
+                return scope;
+            }
+            public boolean isOptional() {
+                return optional;
+            }
+        }
+
     
 }