You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by jm...@apache.org on 2007/06/25 19:45:40 UTC

svn commit: r550564 - in /maven/sandbox/trunk/plugins/maven-pom-plugin/src/main/java/org/apache/maven/plugins/pom: AddByXPathMojo.java AlterByXPathMojo.java

Author: jmcconnell
Date: Mon Jun 25 10:45:39 2007
New Revision: 550564

URL: http://svn.apache.org/viewvc?view=rev&rev=550564
Log:
adding two new mojos for tweaking elements based on arbitary xpath statements

Added:
    maven/sandbox/trunk/plugins/maven-pom-plugin/src/main/java/org/apache/maven/plugins/pom/AddByXPathMojo.java   (with props)
    maven/sandbox/trunk/plugins/maven-pom-plugin/src/main/java/org/apache/maven/plugins/pom/AlterByXPathMojo.java   (with props)

Added: maven/sandbox/trunk/plugins/maven-pom-plugin/src/main/java/org/apache/maven/plugins/pom/AddByXPathMojo.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plugins/maven-pom-plugin/src/main/java/org/apache/maven/plugins/pom/AddByXPathMojo.java?view=auto&rev=550564
==============================================================================
--- maven/sandbox/trunk/plugins/maven-pom-plugin/src/main/java/org/apache/maven/plugins/pom/AddByXPathMojo.java (added)
+++ maven/sandbox/trunk/plugins/maven-pom-plugin/src/main/java/org/apache/maven/plugins/pom/AddByXPathMojo.java Mon Jun 25 10:45:39 2007
@@ -0,0 +1,119 @@
+package org.apache.maven.plugins.pom;
+
+/*
+ * 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.
+ */
+
+import java.io.File;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.pom.util.XMLException;
+import org.apache.maven.plugins.pom.util.XMLTool;
+import org.dom4j.Element;
+
+/**
+ * Alter a project file based on an xpath location
+ * 
+ * @author <a href="mailto:jmcconnell@apache.org">Jesse McConnell</a>
+ * @version $Id:$
+ * 
+ * @phase process-resources
+ * @goal add-by-xpath
+ */
+public class AddByXPathMojo extends AbstractSingleAlterationMojo
+{
+
+	/**
+	 * @parameter
+	 * @required
+	 */
+    private String xpath;
+
+    /**
+     * @parameter
+     * @required
+     */
+    private String newElement;
+    
+    /**
+     * @parameter
+     */
+    private String newValue;
+    
+    /**
+     * setting this boolean to false will allow the mojo to skip altering a field that might not exist in the 
+     * target project file.
+     * 
+     * @parameter default-value="true"
+     */
+    private boolean failFast;
+    
+    /**
+     * xpath statement that is tested for existance, if it does exist, then skip the execution of this mojo
+     * 
+     * @parameter
+     */
+    private String skipXPath;
+    
+    public void execute() throws MojoExecutionException, MojoFailureException
+    {
+        if ( xpath == null || newElement == null )
+        {
+            getLog().info( "missing required information" );
+            return;
+        }
+
+        try
+        {
+            XMLTool xmlTool = new XMLTool( "project", new File( projectFile ) );
+
+            xmlTool.removeNamespaces();
+
+            if( xmlTool.hasElement( skipXPath ) )
+            {
+            	getLog().info( "skipXPath element located, skipping execution" );
+            	return;
+            }
+            
+            if ( xmlTool.hasElement( xpath ) )
+            {
+            	Element node = xmlTool.getElement( xpath );
+            	
+            	node.addElement( newElement );
+            	
+            	if ( newValue != null )
+            	{
+            		node.setText( newValue );
+            	}
+            } 
+            else if ( failFast )
+            {
+            	throw new MojoFailureException( "unable to locate required element in project file: " + projectFile + " with xpath: " + xpath );
+            }           
+            
+            xmlTool.writeDocument( projectFile );
+        }
+        catch ( XMLException e )
+        {
+            throw new MojoExecutionException( "error processing: " + projectFile, e );
+        }
+    }
+}

Propchange: maven/sandbox/trunk/plugins/maven-pom-plugin/src/main/java/org/apache/maven/plugins/pom/AddByXPathMojo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: maven/sandbox/trunk/plugins/maven-pom-plugin/src/main/java/org/apache/maven/plugins/pom/AlterByXPathMojo.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plugins/maven-pom-plugin/src/main/java/org/apache/maven/plugins/pom/AlterByXPathMojo.java?view=auto&rev=550564
==============================================================================
--- maven/sandbox/trunk/plugins/maven-pom-plugin/src/main/java/org/apache/maven/plugins/pom/AlterByXPathMojo.java (added)
+++ maven/sandbox/trunk/plugins/maven-pom-plugin/src/main/java/org/apache/maven/plugins/pom/AlterByXPathMojo.java Mon Jun 25 10:45:39 2007
@@ -0,0 +1,96 @@
+package org.apache.maven.plugins.pom;
+
+/*
+ * 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.
+ */
+
+import java.io.File;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.pom.util.XMLException;
+import org.apache.maven.plugins.pom.util.XMLTool;
+import org.dom4j.Element;
+
+/**
+ * Alter a project file based on an xpath location
+ * 
+ * @author <a href="mailto:jmcconnell@apache.org">Jesse McConnell</a>
+ * @version $Id:$
+ * 
+ * @phase process-resources
+ * @goal alter-by-xpath
+ */
+public class AlterByXPathMojo extends AbstractSingleAlterationMojo
+{
+
+	/**
+	 * @parameter
+	 * @required
+	 */
+    private String xpath;
+
+    /**
+     * @parameter
+     * @required
+     */
+    private String newValue;
+    
+    /**
+     * setting this boolean to false will allow the mojo to skip altering a field that might not exist in the 
+     * target project file.
+     * 
+     * @parameter default-value="true"
+     */
+    private boolean failFast;
+    
+    public void execute() throws MojoExecutionException, MojoFailureException
+    {
+        if ( xpath == null || newValue == null )
+        {
+            getLog().info( "missing required information" );
+            return;
+        }
+
+        try
+        {
+            XMLTool xmlTool = new XMLTool( "project", new File( projectFile ) );
+
+            xmlTool.removeNamespaces();
+
+            if ( xmlTool.hasElement( xpath ) )
+            {
+            	Element node = xmlTool.getElement( xpath );
+            	
+            	node.setText( newValue );            	            
+            } 
+            else if ( failFast )
+            {
+            	throw new MojoFailureException( "unable to locate required element in project file: " + projectFile + " with xpath: " + xpath );
+            }           
+            
+            xmlTool.writeDocument( projectFile );
+        }
+        catch ( XMLException e )
+        {
+            throw new MojoExecutionException( "error processing: " + projectFile, e );
+        }
+    }
+}

Propchange: maven/sandbox/trunk/plugins/maven-pom-plugin/src/main/java/org/apache/maven/plugins/pom/AlterByXPathMojo.java
------------------------------------------------------------------------------
    svn:eol-style = native