You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by se...@apache.org on 2013/06/28 00:54:06 UTC
svn commit: r1497602 -
/maven/sandbox/trunk/plugins/maven-digest-plugin/src/main/java/org/apache/maven/plugins/digest/HelperMojo.java
Author: sebb
Date: Thu Jun 27 22:54:05 2013
New Revision: 1497602
URL: http://svn.apache.org/r1497602
Log:
Configure pluginId from plugin.xml
Modified:
maven/sandbox/trunk/plugins/maven-digest-plugin/src/main/java/org/apache/maven/plugins/digest/HelperMojo.java
Modified: maven/sandbox/trunk/plugins/maven-digest-plugin/src/main/java/org/apache/maven/plugins/digest/HelperMojo.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plugins/maven-digest-plugin/src/main/java/org/apache/maven/plugins/digest/HelperMojo.java?rev=1497602&r1=1497601&r2=1497602&view=diff
==============================================================================
--- maven/sandbox/trunk/plugins/maven-digest-plugin/src/main/java/org/apache/maven/plugins/digest/HelperMojo.java (original)
+++ maven/sandbox/trunk/plugins/maven-digest-plugin/src/main/java/org/apache/maven/plugins/digest/HelperMojo.java Thu Jun 27 22:54:05 2013
@@ -18,11 +18,23 @@
package org.apache.maven.plugins.digest;
+import java.io.IOException;
+import java.io.InputStream;
import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.help.DescribeMojo;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
/**
* Helper Mojo that extends the standard Maven help plugin describe goal. This is needed because the generated help mojo
@@ -32,6 +44,8 @@ import org.apache.maven.plugins.help.Des
public class HelperMojo
extends DescribeMojo
{
+ // Where to find plugin config
+ private static final String PLUGIN_PATH = "/META-INF/maven/plugin.xml";
// ----------------------------------------------------------------------
// Mojo components
@@ -56,6 +70,7 @@ public class HelperMojo
public void execute()
throws MojoExecutionException
{
+ final String pluginId = getpluginGA();
Field f = null;
boolean isAccessible = true; // assume accessible
try
@@ -67,11 +82,7 @@ public class HelperMojo
{
f.setAccessible( true );
}
- // N.B. Customise for each plugin
- // We used to use project component to get the info, but that forces plugin to be used in a project
- // Plugin should be able to be used standalone
- final String plugin = "org.apache.maven.plugins:maven-digest-plugin";
- f.set( this, plugin );
+ f.set( this, pluginId );
super.execute();
}
catch ( Exception e )
@@ -93,6 +104,81 @@ public class HelperMojo
// ----------------------------------------------------------------------
// Private methods
// ----------------------------------------------------------------------
+ private String getpluginGA() throws MojoExecutionException {
+ Document doc = build();
+ Node plugin = getSingleChild( doc, "plugin" );
+ String id = getValue( plugin, "groupId" ) + ":" + getValue( plugin, "artifactId" );
+ return id;
+ }
+
+ private Document build()
+ throws MojoExecutionException
+ {
+ getLog().debug( "load plugin-help.xml: " + PLUGIN_PATH );
+ InputStream is = getClass().getResourceAsStream( PLUGIN_PATH );
+ try
+ {
+ DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
+ return dBuilder.parse( is );
+ }
+ catch ( IOException e )
+ {
+ throw new MojoExecutionException( e.getMessage(), e );
+ }
+ catch ( ParserConfigurationException e )
+ {
+ throw new MojoExecutionException( e.getMessage(), e );
+ }
+ catch ( SAXException e )
+ {
+ throw new MojoExecutionException( e.getMessage(), e );
+ }
+ finally
+ {
+ try {
+ is.close();
+ } catch (IOException e) {
+ }
+ }
+ }
+
+ private List<Node> findNamedChild( Node node, String elementName )
+ {
+ List<Node> result = new ArrayList<Node>();
+ NodeList childNodes = node.getChildNodes();
+ for ( int i = 0; i < childNodes.getLength(); i++ )
+ {
+ Node item = childNodes.item( i );
+ if ( elementName.equals( item.getNodeName() ) )
+ {
+ result.add( item );
+ }
+ }
+ return result;
+ }
+
+ private Node getSingleChild( Node node, String elementName )
+ throws MojoExecutionException
+ {
+ List<Node> namedChild = findNamedChild( node, elementName );
+ if ( namedChild.isEmpty() )
+ {
+ throw new MojoExecutionException( "Could not find " + elementName + " in plugin-help.xml" );
+ }
+ if ( namedChild.size() > 1 )
+ {
+ throw new MojoExecutionException( "Multiple " + elementName + " in plugin-help.xml" );
+ }
+ return namedChild.get( 0 );
+ }
+
+ private String getValue( Node node, String elementName )
+ throws MojoExecutionException
+ {
+ return getSingleChild( node, elementName ).getTextContent();
+ }
+
// ----------------------------------------------------------------------
// Static methods