Permalink
Browse files

Support for plugin-overrides (pluginManagement). Now roundtripping Gu…

…ice parent pom as well
  • Loading branch information...
1 parent 8e50cf7 commit 6847a281cbcf534c68035ddc26c28594e26fd4a1 @dhanji dhanji committed May 23, 2012
@@ -25,6 +25,7 @@
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.xml.Xpp3Dom;
+import org.sonatype.maven.polyglot.atom.parsing.Token;
import org.sonatype.maven.polyglot.io.ModelWriterSupport;
import java.io.IOException;
@@ -59,7 +60,7 @@ public void write(final Writer output, final Map<String, Object> options, final
modules(pw, model);
pw.println();
pluginManagement(pw, model);
- plugins(pw, model);
+ plugins(pw, "plugin", model);
pw.flush();
pw.close();
@@ -197,22 +198,22 @@ private void deps(PrintWriter pw, String elementName, List<Dependency> deps) {
private void pluginManagement(PrintWriter pw, Model model) {
if (model.getBuild() != null && model.getBuild().getPluginManagement() != null) {
- plugins(pw, model.getBuild().getPluginManagement().getPlugins());
+ plugins(pw, Token.PLUGIN_OVERRIDE_KEYWORD, model.getBuild().getPluginManagement().getPlugins());
}
}
- private void plugins(PrintWriter pw, Model model) {
+ private void plugins(PrintWriter pw, String element, Model model) {
if (model.getBuild() != null && !model.getBuild().getPlugins().isEmpty()) {
- plugins(pw, model.getBuild().getPlugins());
+ plugins(pw, element, model.getBuild().getPlugins());
}
}
// need to write nested objects
- private void plugins(PrintWriter pw, List<Plugin> plugins) {
+ private void plugins(PrintWriter pw, String element, List<Plugin> plugins) {
if (!plugins.isEmpty()) {
for (int i = 0; i < plugins.size(); i++) {
Plugin plugin = plugins.get(i);
- pw.println("\nplugin");
+ pw.println("\n" + element);
pw.print(indent + "id: " + plugin.getGroupId() + ":" + plugin.getArtifactId());
if (plugin.getVersion() != null)
@@ -150,7 +150,8 @@ private Project project(Repositories repositories) {
// modules
chewEols();
chewIndents();
- List<Plugin> plugins = plugins();
+ List<Plugin> pluginOverrides = plugins(Kind.PLUGIN_OVERRIDE);
+ List<Plugin> plugins = plugins(Kind.PLUGIN);
return new Project(projectId,
parent,
@@ -162,6 +163,7 @@ private Project project(Repositories repositories) {
overrides,
deps,
modules,
+ pluginOverrides,
plugins,
dirs,
scm);
@@ -307,12 +309,12 @@ else if ("url".equals(label))
/**
* Additional plugins and their configuration.
*/
- private List<Plugin> plugins() {
+ private List<Plugin> plugins(Kind keyword) {
List<Plugin> plugins = new ArrayList<Plugin>();
chewEols();
Plugin plugin;
- while ((plugin = plugin()) != null) {
+ while ((plugin = plugin(keyword)) != null) {
plugins.add(plugin);
chewEols();
@@ -321,16 +323,15 @@ else if ("url".equals(label))
return plugins;
}
- private Plugin plugin() {
- if (match(Kind.PLUGIN) == null)
+ private Plugin plugin(Kind keyword) {
+ if (match(keyword) == null)
return null;
if (match(Kind.EOL) == null) {
parseException("Expected newline after 'plugin' keyword");
}
Plugin plugin = new Plugin();
- List<Token> propKey;
chewIndents();
if (match(Kind.ID, Kind.COLON) == null) {
@@ -355,7 +356,7 @@ private Plugin plugin() {
plugin.setVersion(pluginId.getVersion());
Map<String, Object> config;
- if ((config = configurationMap()) == null)
+ if ((config = configurationMap()) == null || config.isEmpty())
return plugin;
// Transform the parsed config map into maven's XPP3 Dom thing.
@@ -20,13 +20,26 @@
private List<Id> deps;
private List<Id> overrides;
private List<String> modules;
+ private List<Plugin> pluginOverrides;
private List<Plugin> plugins;
private Map<String, String> dirs;
private static final String MAVEN_CENTRAL_URL = "http://repo1.maven.org/maven2";
private final ScmElement scm;
- public Project(Id projectId, Parent parent, String packaging, List<Property> properties, Repositories repositories, String description, String url, List<Id> overrides, List<Id> deps,
- List<String> modules, List<Plugin> plugins, Map<String, String> dirs, ScmElement scm) {
+ public Project(Id projectId,
+ Parent parent,
+ String packaging,
+ List<Property> properties,
+ Repositories repositories,
+ String description,
+ String url,
+ List<Id> overrides,
+ List<Id> deps,
+ List<String> modules,
+ List<Plugin> pluginOverrides,
+ List<Plugin> plugins,
+ Map<String, String> dirs,
+ ScmElement scm) {
this.projectId = projectId;
this.parent = parent;
this.packaging = packaging;
@@ -37,6 +50,7 @@ public Project(Id projectId, Parent parent, String packaging, List<Property> pro
this.overrides = overrides;
this.deps = deps;
this.modules = modules;
+ this.pluginOverrides = pluginOverrides;
this.plugins = plugins;
this.dirs = dirs;
this.scm = scm;
@@ -58,6 +72,10 @@ public String getPackaging() {
return plugins;
}
+ public List<Plugin> getPluginOverrides() {
+ return pluginOverrides;
+ }
+
public List<String> getModules() {
return modules;
}
@@ -117,14 +135,7 @@ public Model toMavenModel() {
}
// Add jar repository urls.
- if (null == repositories) {
- // Add maven central if no repos exist.
- Repository repository = new Repository();
- repository.setId("Maven Central");
- repository.setUrl(MAVEN_CENTRAL_URL);
- model.addRepository(repository);
-
- } else {
+ if (null != repositories) {
for (String repoUrl : repositories.getRepositories()) {
Repository repository = new Repository();
repository.setId(Integer.toString(repoUrl.hashCode()));
@@ -173,6 +184,12 @@ public Model toMavenModel() {
model.setModules(modules);
}
+ if (pluginOverrides != null) {
+ PluginManagement management = new PluginManagement();
+ management.setPlugins(pluginOverrides);
+ model.getBuild().setPluginManagement(management);
+ }
+
if (plugins != null) {
model.getBuild().setPlugins(plugins);
}
@@ -9,6 +9,7 @@
* @author dhanji@gmail.com (Dhanji R. Prasanna)
*/
public class Token {
+ public static final String PLUGIN_OVERRIDE_KEYWORD = "plugin-override";
public final String value;
public final Kind kind;
@@ -69,9 +70,8 @@ public Token(String value, Kind kind) {
OVERRIDES,
PROPS,
MODULES,
- PLUGIN;
- //DOLLAR,
- //PROJECT_DOT_VERSION;
+ PLUGIN,
+ PLUGIN_OVERRIDE;
private static final Map<String, Kind> TOKEN_MAP = new HashMap<String, Kind>();
@@ -111,6 +111,7 @@ public Token(String value, Kind kind) {
TOKEN_MAP.put("scm", SCM);
TOKEN_MAP.put("modules", MODULES);
TOKEN_MAP.put("plugin", PLUGIN);
+ TOKEN_MAP.put(PLUGIN_OVERRIDE_KEYWORD, PLUGIN_OVERRIDE);
TOKEN_MAP.put("||", OR);
@@ -24,7 +24,6 @@ public Tokenizer(String input) {
static {
- DELIMITERS['-'] = SEQUENCE_TOKEN;
DELIMITERS['+'] = SEQUENCE_TOKEN;
DELIMITERS['/'] = SEQUENCE_TOKEN;
DELIMITERS['*'] = SEQUENCE_TOKEN;
@@ -0,0 +1,115 @@
+package org.sonatype.maven.polyglot.atom;
+
+import org.apache.maven.model.Dependency;
+import org.apache.maven.model.Model;
+import org.apache.maven.model.Plugin;
+import org.apache.maven.model.io.ModelReader;
+import org.apache.maven.model.io.ModelWriter;
+import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
+import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
+import org.codehaus.plexus.util.xml.Xpp3Dom;
+import org.sonatype.guice.bean.containers.InjectedTestCase;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.HashMap;
+
+public class AtomModelWithGuiceTest extends InjectedTestCase {
+
+ @Inject
+ @Named("${basedir}/src/test/poms")
+ private File poms;
+
+ public void testAtomModelWriter() throws Exception {
+ File pom = new File(poms, "guice-pom.xml");
+ MavenXpp3Reader xmlModelReader = new MavenXpp3Reader();
+ Model xmlModel = xmlModelReader.read(new FileInputStream(pom));
+ //
+ testMavenModelForCompleteness(xmlModel);
+ //
+ // Write out the Atom POM
+ //
+ ModelWriter writer = new AtomModelWriter();
+ StringWriter w = new StringWriter();
+ writer.write(w, new HashMap<String, Object>(), xmlModel);
+
+ // Let's take a look at see what's there
+ System.out.println(w.toString());
+
+ //
+ // Read in the Atom POM
+ //
+ ModelReader atomModelReader = new AtomModelReader();
+ StringReader r = new StringReader(w.toString());
+ Model atomModel = atomModelReader.read(r, new HashMap<String, Object>());
+ //
+ // Test for fidelity
+ //
+ assertNotNull(atomModel);
+ testMavenModelForCompleteness(atomModel);
+
+ w = new StringWriter();
+ MavenXpp3Writer xmlWriter = new MavenXpp3Writer();
+ xmlWriter.write(w, xmlModel);
+ System.out.println(w.toString());
+
+ assertEquals(xmlModel.getModules(), atomModel.getModules());
+ assertEquals(xmlModel.getRepositories().size(), atomModel.getRepositories().size());
+ }
+
+ void testMavenModelForCompleteness(Model model) {
+ //
+ // parent
+ //
+ assertEquals("com.google.inject:guice-parent:3.0-SNAPSHOT", model.getGroupId() + ":" + model.getArtifactId() + ":" + model.getVersion());
+ //
+ // id
+ //
+// assertEquals("org.sonatype.oss:oss-parent:6", model.getParent().getGroupId() + ":" + model.getParent().getArtifactId() + ":" + model.getParent().getVersion());
+ //
+ // properties
+ //
+// assertEquals("pom", model.getPackaging());
+ assertNull(model.getProperties().getProperty("org.testng.version"));
+ assertEquals("1.3", model.getProperties().getProperty("guice.api.version"));
+ assertEquals("true", model.getProperties().getProperty("guice.with.jarjar"));
+ assertEquals("true", model.getProperties().getProperty("guice.with.no_aop"));
+ assertEquals("UTF-8", model.getProperties().getProperty("project.build.sourceEncoding"));
+ //
+ // depMan
+ //
+ Dependency testNg = model.getDependencies().get(0);
+ assertEquals("org.testng:testng:5.11", gav(testNg));
+ assertEquals("jdk15", testNg.getClassifier());
+ //
+ // modules
+ //
+ assertEquals("core", model.getModules().get(0));
+ assertEquals("extensions", model.getModules().get(1));
+ //
+ // plugins
+ //
+ Plugin p0 = model.getBuild().getPluginManagement().getPlugins().get(0);
+ assertEquals("org.apache.maven.plugins:maven-remote-resources-plugin:1.1", gav(p0));
+
+ p0 = model.getBuild().getPluginManagement().getPlugins().get(1);
+ assertEquals("org.apache.maven.plugins:maven-compiler-plugin:2.3.2", gav(p0));
+ assertNull(p0.getConfiguration());
+
+ p0 = model.getBuild().getPluginManagement().getPlugins().get(2);
+ assertEquals("org.codehaus.mojo:animal-sniffer-maven-plugin:1.6", gav(p0));
+ assertEquals(3, ((Xpp3Dom)p0.getConfiguration()).getChild("signature").getChildCount());
+ }
+
+ String gav(Dependency d) {
+ return d.getGroupId() + ":" + d.getArtifactId() + ":" + d.getVersion();
+ }
+
+ String gav(Plugin p) {
+ return p.getGroupId() + ":" + p.getArtifactId() + ":" + p.getVersion();
+ }
+}
@@ -57,7 +57,7 @@ void testMavenModelForCompleteness(Model model) {
//
// repos
//
- assertEquals("http://repo1.maven.org/maven2", model.getRepositories().get(0).getUrl());
+ assertEquals(0, model.getRepositories().size());
//
// parent
//
@@ -94,12 +94,12 @@ void testMavenModelForCompleteness(Model model) {
//
// plugins
//
- Plugin p0 = model.getBuild().getPlugins().get(0);
+ Plugin p0 = model.getBuild().getPluginManagement().getPlugins().get(0);
assertEquals("org.codehaus.plexus:plexus-component-metadata:${plexusVersion}", gav(p0));
- assertEquals(0, ((Xpp3Dom)p0.getConfiguration()).getChildCount());
+ assertNull(p0.getConfiguration());
- p0 = model.getBuild().getPlugins().get(1);
+ p0 = model.getBuild().getPluginManagement().getPlugins().get(1);
assertEquals("org.apache.maven.plugins:maven-compiler-plugin:2.3.2", gav(p0));
assertEquals("1.5", ((Xpp3Dom)p0.getConfiguration()).getChild("source").getValue());
assertEquals("1.5", ((Xpp3Dom)p0.getConfiguration()).getChild("target").getValue());
Oops, something went wrong.

0 comments on commit 6847a28

Please sign in to comment.