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/09/15 23:22:19 UTC
svn commit: r997508 - in /ant/ivy/core/trunk: ./ doc/use/
src/java/org/apache/ivy/ant/ src/java/org/apache/ivy/plugins/parser/m2/
Author: maartenc
Date: Wed Sep 15 21:22:18 2010
New Revision: 997508
URL: http://svn.apache.org/viewvc?rev=997508&view=rev
Log:
NEW: the ivy:makepom Ant task can use a custom template file to generate the pom.xml
Modified:
ant/ivy/core/trunk/CHANGES.txt
ant/ivy/core/trunk/doc/use/makepom.html
ant/ivy/core/trunk/src/java/org/apache/ivy/ant/IvyMakePom.java
ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorWriter.java
ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/parser/m2/PomWriterOptions.java
Modified: ant/ivy/core/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/CHANGES.txt?rev=997508&r1=997507&r2=997508&view=diff
==============================================================================
--- ant/ivy/core/trunk/CHANGES.txt (original)
+++ ant/ivy/core/trunk/CHANGES.txt Wed Sep 15 21:22:18 2010
@@ -116,6 +116,7 @@ for detailed view of each issue, please
- DOCUMENTATION: Grammar, spelling, and clarity of Settings File documentation (IVY-1216) (thanks to Steve Miller)
- DOCUMENTATION: Grammar, spelling, and clarity of Tutorial documentation (IVY-1222) (thanks to Steve Miller)
+- NEW: the ivy:makepom Ant task can use a custom template file to generate the pom.xml
- NEW: Ivy can now generate OpenPGP compatible ASCII armored detached signatures when publishing artifacts.
- IMPROVEMENT: the <artifact> child of ivy:publish now accepts any attribute
Modified: ant/ivy/core/trunk/doc/use/makepom.html
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/doc/use/makepom.html?rev=997508&r1=997507&r2=997508&view=diff
==============================================================================
--- ant/ivy/core/trunk/doc/use/makepom.html (original)
+++ ant/ivy/core/trunk/doc/use/makepom.html Wed Sep 15 21:22:18 2010
@@ -32,6 +32,45 @@ An example of use is to publish an Ivy m
<em>Note that all Ivy features are not supported by maven poms, so the converted pom may not resolve to the exact same dependencies as the original ivy file.</em>
+<span class="since">since 2.2</span>
+It is possible to specify a template file defining the structure of the generated POM. The following processing is done on this template:
+<ul>
+<li>properties like <i>${property.name}</i> are replaced if they are defined in Ant or by the ivy:makepom task (see below for the standard properties)</li>
+<li>lines containg the string <i>SKIP_LINE</i> are skipped.</li>
+<li>the defined dependencies will be added to the first <dependencies> element encountered in the pom template. If the template doesn't contain a <dependencies> element, it is generated a the end of the pom.
+</ul>
+
+The ivy:makepom task defines following properties that can be used in the template. <ul>
+<li><b>ivy.pom.groupId</b>: defaults to the organisation as defined in the ivy.xml file</li>
+<li><b>ivy.pom.artifactId</b>: defaults to the value of the 'atifactName' attribute of this task, or the name of the module as defined in the ivy.xml file</li>
+<li><b>ivy.pom.packaging</b>: defaults to the value of the 'artifactPackaging' attribute of this task, or the extenstion of the artifact</li>
+<li><b>ivy.pom.version</b>: defaults to the revision as defined in the ivy.xml file</li>
+<li><b>ivy.pom.name</b>: defaults to 'SKIP_LINE'</li>
+<li><b>ivy.pom.description</b>: defaults to the value of the 'description' attribute of this task, or 'SKIP_LINE' when not specified</li>
+<li><b>ivy.pom.url</b>: defaults to the homepage as defined in the ivy.xml file</li>
+<li><b>ivy.pom.license</b>: the content of the specified headerFile, or 'SKIP_LINE' if not specified</li>
+<li><b>ivy.pom.header</b>: some Ivy information, or 'SKIP_LINE' if the 'printIvyInfo' attribute is set to false.</li>
+</ul>
+Note that each property can be given a value manually in the Ant build file. In that case, Ivy will use the value specified in the build file instead of the default value.
+
+The default template that ships with Ivy looks like this:
+<code type="xml">
+${ivy.pom.license}
+${ivy.pom.header}
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>${ivy.pom.groupId}</groupId>
+ <artifactId>${ivy.pom.artifactId}</artifactId>
+ <packaging>${ivy.pom.packaging}</packaging>
+ <version>${ivy.pom.version}</version>
+ <name>${ivy.pom.name}</name>
+ <description>${ivy.pom.description}</description>
+ <url>${ivy.pom.url}</url>
+</project>
+</code>
+
<h1>Attributes</h1>
<table class="ant">
<thead>
Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/ant/IvyMakePom.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/ant/IvyMakePom.java?rev=997508&r1=997507&r2=997508&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/ant/IvyMakePom.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/ant/IvyMakePom.java Wed Sep 15 21:22:18 2010
@@ -105,6 +105,8 @@ public class IvyMakePom extends IvyTask
private File headerFile = null;
+ private File templateFile = null;
+
private boolean printIvyInfo = true;
private String conf;
@@ -141,6 +143,14 @@ public class IvyMakePom extends IvyTask
this.headerFile = headerFile;
}
+ public File getTemplateFile() {
+ return templateFile;
+ }
+
+ public void setTemplateFile(File templateFile) {
+ this.templateFile = templateFile;
+ }
+
public String getDescription() {
return description;
}
@@ -223,7 +233,8 @@ public class IvyMakePom extends IvyTask
.setArtifactPackaging(getArtifactPackaging())
.setPrintIvyInfo(isPrintIvyInfo())
.setDescription(getDescription())
- .setExtraDependencies(getDependencies());
+ .setExtraDependencies(getDependencies())
+ .setTemplate(getTemplateFile());
if (!mappings.isEmpty()) {
options.setMapping(new PomWriterOptions.ConfigurationScopeMapping(getMappingsMap()));
Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorWriter.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorWriter.java?rev=997508&r1=997507&r2=997508&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorWriter.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/parser/m2/PomModuleDescriptorWriter.java Wed Sep 15 21:22:18 2010
@@ -18,26 +18,36 @@
package org.apache.ivy.plugins.parser.m2;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
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.IvyContext;
+import org.apache.ivy.core.IvyPatternHelper;
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.core.settings.IvySettings;
+import org.apache.ivy.core.settings.IvyVariableContainer;
import org.apache.ivy.plugins.parser.m2.PomWriterOptions.ConfigurationScopeMapping;
import org.apache.ivy.plugins.parser.m2.PomWriterOptions.ExtraDependency;
import org.apache.ivy.util.ConfigurationUtils;
public final class PomModuleDescriptorWriter {
+ private static final String SKIP_LINE = "SKIP_LINE";
+
private static final ConfigurationScopeMapping DEFAULT_MAPPING
= new ConfigurationScopeMapping(new HashMap() {
{
@@ -55,40 +65,84 @@ public final class PomModuleDescriptorWr
public static void write(ModuleDescriptor md, File output, PomWriterOptions options)
throws IOException {
+ LineNumberReader in;
+ if (options.getTemplate() == null) {
+ in = new LineNumberReader(new InputStreamReader(PomModuleDescriptorWriter.class.getResourceAsStream("pom.template")));
+ } else {
+ in = new LineNumberReader(new InputStreamReader(new FileInputStream(options.getTemplate())));
+ }
+
if (output.getParentFile() != null) {
output.getParentFile().mkdirs();
}
- PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(output),
- "UTF-8"));
+ PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(output), "UTF-8"));
try {
- out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
+ IvySettings settings = IvyContext.getContext().getSettings();
+ IvyVariableContainer variables = new IvyVariableContainerWrapper(settings.getVariableContainer());
+
+ variables.setVariable("ivy.pom.license", SKIP_LINE, true);
+ variables.setVariable("ivy.pom.header", SKIP_LINE, true);
+ variables.setVariable("ivy.pom.groupId", SKIP_LINE, true);
+ variables.setVariable("ivy.pom.artifactId", SKIP_LINE, true);
+ variables.setVariable("ivy.pom.version", SKIP_LINE, true);
+ variables.setVariable("ivy.pom.packaging", SKIP_LINE, true);
+ variables.setVariable("ivy.pom.name", SKIP_LINE, true);
+ variables.setVariable("ivy.pom.description", SKIP_LINE, true);
+ variables.setVariable("ivy.pom.url", SKIP_LINE, true);
+
if (options.getLicenseHeader() != null) {
- out.print(options.getLicenseHeader());
+ variables.setVariable("ivy.pom.license", options.getLicenseHeader(), true);
}
if (options.isPrintIvyInfo()) {
- out.println("<!--");
- out.println(" Apache Maven 2 POM generated by Apache Ivy");
- out.println(" " + Ivy.getIvyHomeURL());
- out.println(" Apache Ivy version: " + Ivy.getIvyVersion()
- + " " + Ivy.getIvyDate());
- out.println("-->");
- }
- out.println("<project xmlns=\"http://maven.apache.org/POM/4.0.0\" "
- + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
- 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, options);
- printDependencies(md, out, options);
- out.println("</project>");
+ String header = "<!--\n"
+ + " Apache Maven 2 POM generated by Apache Ivy\n"
+ + " " + Ivy.getIvyHomeURL() + "\n"
+ + " Apache Ivy version: " + Ivy.getIvyVersion()
+ + " " + Ivy.getIvyDate() + "\n"
+ + "-->";
+ variables.setVariable("ivy.pom.header", header, true);
+ }
+
+ setModuleVariables(md, variables, options);
+
+ boolean dependenciesPrinted = false;
+
+ int lastIndent = 0;
+ int indent = 0;
+ String line = in.readLine();
+ while (line != null) {
+ line = IvyPatternHelper.substituteVariables(line, variables);
+ if (line.indexOf(SKIP_LINE) != -1) {
+ // skip this line
+ line = in.readLine();
+ continue;
+ }
+
+ lastIndent = indent;
+ indent = line.indexOf('<');
+
+ if (!dependenciesPrinted && line.indexOf("</dependencies>") != -1) {
+ printDependencies(md, out, options, indent, false);
+ dependenciesPrinted = true;
+ }
+
+ if (!dependenciesPrinted && line.indexOf("</project>") != -1) {
+ printDependencies(md, out, options, lastIndent, true);
+ dependenciesPrinted = true;
+ }
+
+ out.println(line);
+ line = in.readLine();
+ }
} finally {
+ in.close();
out.close();
}
}
- private static void printModuleId(ModuleDescriptor md, PrintWriter out, PomWriterOptions options) {
+ private static void setModuleVariables(ModuleDescriptor md, IvyVariableContainer variables, PomWriterOptions options) {
ModuleRevisionId mrid = md.getModuleRevisionId();
- out.println(" <groupId>" + mrid.getOrganisation() + "</groupId>");
+ variables.setVariable("ivy.pom.groupId", mrid.getOrganisation(), true);
String artifactId = options.getArtifactName();
if (artifactId == null) {
@@ -107,16 +161,16 @@ public final class PomModuleDescriptorWr
}
}
- out.println(" <artifactId>" + artifactId + "</artifactId>");
- out.println(" <packaging>" + packaging + "</packaging>");
+ variables.setVariable("ivy.pom.artifactId", artifactId, true);
+ variables.setVariable("ivy.pom.packaging", packaging, true);
if (mrid.getRevision() != null) {
- out.println(" <version>" + mrid.getRevision() + "</version>");
+ variables.setVariable("ivy.pom.version", mrid.getRevision(), true);
}
if (options.getDescription() != null) {
- out.println(" <description>" + options.getDescription() + "</description>");
+ variables.setVariable("ivy.pom.description", options.getDescription(), true);
}
if (md.getHomePage() != null) {
- out.println(" <url>" + md.getHomePage() + "</url>");
+ variables.setVariable("ivy.pom.url", md.getHomePage(), true);
}
}
@@ -134,38 +188,54 @@ public final class PomModuleDescriptorWr
return null;
}
+
+ private static void indent(PrintWriter out, int indent) {
+ for (int i = 0; i < indent; i++) {
+ out.print(' ');
+ }
+ }
private static void printDependencies(ModuleDescriptor md, PrintWriter out,
- PomWriterOptions options) {
+ PomWriterOptions options, int indent, boolean printDependencies) {
List extraDeps = options.getExtraDependencies();
DependencyDescriptor[] dds = getDependencies(md, options);
if (!extraDeps.isEmpty() || (dds.length > 0)) {
- out.println(" <dependencies>");
-
+ if (printDependencies) {
+ indent(out, indent);
+ out.println("<dependencies>");
+ }
+
// print the extra dependencies first
for (Iterator it = extraDeps.iterator(); it.hasNext(); ) {
PomWriterOptions.ExtraDependency dep = (ExtraDependency) it.next();
- out.println(" <dependency>");
+ indent(out, indent * 2);
+ 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>");
+ indent(out, indent * 3);
+ out.println("<groupId>" + groupId + "</groupId>");
+ indent(out, indent * 3);
+ out.println("<artifactId>" + dep.getArtifact() + "</artifactId>");
String version = dep.getVersion();
if (version == null) {
version = md.getModuleRevisionId().getRevision();
}
- out.println(" <version>" + version + "</version>");
+ indent(out, indent * 3);
+ out.println("<version>" + version + "</version>");
if (dep.getScope() != null) {
- out.println(" <scope>" + dep.getScope() + "</scope>");
+ indent(out, indent * 3);
+ out.println("<scope>" + dep.getScope() + "</scope>");
}
if (dep.isOptional()) {
- out.println(" <optional>true</optional>");
+ indent(out, indent * 3);
+ out.println("<optional>true</optional>");
}
- out.println(" </dependency>");
+ indent(out, indent * 2);
+ out.println("</dependency>");
}
// now print the dependencies listed in the ModuleDescriptor
@@ -176,20 +246,31 @@ public final class PomModuleDescriptorWr
for (int i = 0; i < dds.length; i++) {
ModuleRevisionId mrid = dds[i].getDependencyRevisionId();
- out.println(" <dependency>");
- out.println(" <groupId>" + mrid.getOrganisation() + "</groupId>");
- out.println(" <artifactId>" + mrid.getName() + "</artifactId>");
- out.println(" <version>" + mrid.getRevision() + "</version>");
+ indent(out, indent * 2);
+ out.println("<dependency>");
+ indent(out, indent * 3);
+ out.println("<groupId>" + mrid.getOrganisation() + "</groupId>");
+ indent(out, indent * 3);
+ out.println("<artifactId>" + mrid.getName() + "</artifactId>");
+ indent(out, indent * 3);
+ out.println("<version>" + mrid.getRevision() + "</version>");
String scope = mapping.getScope(dds[i].getModuleConfigurations());
if (scope != null) {
- out.println(" <scope>" + scope + "</scope>");
+ indent(out, indent * 3);
+ out.println("<scope>" + scope + "</scope>");
}
if (mapping.isOptional(dds[i].getModuleConfigurations())) {
- out.println(" <optional>true</optional>");
+ indent(out, indent * 3);
+ out.println("<optional>true</optional>");
}
- out.println(" </dependency>");
+ indent(out, indent * 2);
+ out.println("</dependency>");
+ }
+
+ if (printDependencies) {
+ indent(out, indent);
+ out.println("</dependencies>");
}
- out.println(" </dependencies>");
}
}
@@ -208,4 +289,41 @@ public final class PomModuleDescriptorWr
return (DependencyDescriptor[]) result.toArray(new DependencyDescriptor[result.size()]);
}
+
+ /**
+ * Wraps an {@link IvyVariableContainer} delegating most method calls to the wrapped instance,
+ * except for a set of variables which are only stored locally in the wrapper, and not
+ * propagated to the wrapped instance.
+ */
+ private static final class IvyVariableContainerWrapper implements IvyVariableContainer {
+
+ private final IvyVariableContainer variables;
+
+ private Map localVariables = new HashMap();
+
+ private IvyVariableContainerWrapper(IvyVariableContainer variables) {
+ this.variables = variables;
+ }
+
+ public void setVariable(String varName, String value, boolean overwrite) {
+ localVariables.put(varName, value);
+ }
+
+ public void setEnvironmentPrefix(String prefix) {
+ variables.setEnvironmentPrefix(prefix);
+ }
+
+ public String getVariable(String name) {
+ String result = variables.getVariable(name);
+ if (result == null) {
+ result = (String) localVariables.get(name);
+ }
+ return result;
+ }
+
+ public Object clone() {
+ throw new UnsupportedOperationException();
+ }
+ }
+
}
Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/parser/m2/PomWriterOptions.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/parser/m2/PomWriterOptions.java?rev=997508&r1=997507&r2=997508&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/parser/m2/PomWriterOptions.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/parser/m2/PomWriterOptions.java Wed Sep 15 21:22:18 2010
@@ -17,6 +17,7 @@
*/
package org.apache.ivy.plugins.parser.m2;
+import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -39,6 +40,17 @@ public class PomWriterOptions {
private List/*<ExtraDependency>*/ extraDependencies = new ArrayList();
private String description;
+
+ private File template;
+
+ public File getTemplate() {
+ return template;
+ }
+
+ public PomWriterOptions setTemplate(File template) {
+ this.template = template;
+ return this;
+ }
public String[] getConfs() {
return confs;
@@ -55,6 +67,9 @@ public class PomWriterOptions {
public PomWriterOptions setLicenseHeader(String licenseHeader) {
this.licenseHeader = licenseHeader;
+ if (this.licenseHeader != null) {
+ this.licenseHeader = this.licenseHeader.trim();
+ }
return this;
}