Add pom rewriting

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@170605 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Emmanuel Venisse 2005-05-17 14:58:14 +00:00
parent 8a28cde7b6
commit 21c7962649
7 changed files with 686 additions and 10 deletions

View File

@ -30,5 +30,15 @@
<artifactId>maven-scm-provider-svn</artifactId>
<version>1.0-alpha-1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.4-dev-8</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.0-FCS</version>
</dependency>
</dependencies>
</project>

View File

@ -92,6 +92,11 @@ public abstract class AbstractReleaseMojo
return scmManager;
}
public String getTag()
{
return tag;
}
protected ScmBean getScm()
{
ScmBean scm = new ScmBean();

View File

@ -23,17 +23,16 @@ import org.apache.maven.model.Plugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.scm.ScmBean;
import org.apache.maven.plugin.transformer.PomTransformer;
import org.apache.maven.plugin.transformer.VersionTransformer;
import org.apache.maven.project.MavenProject;
import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFile;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
/**
* @goal prepare
@ -73,7 +72,7 @@ public class PrepareReleaseMojo
transformPom();
//checkin();
checkin();
tag();
}
@ -228,16 +227,18 @@ public class PrepareReleaseMojo
}
}
//Write pom
MavenXpp3Writer modelWriter = new MavenXpp3Writer();
try
{
//TODO: Write in pom file
//TODO: Write only necessary informations
StringWriter writer = new StringWriter();
modelWriter.write( writer, model );
getLog().info( writer.toString() );
PomTransformer transformer = new VersionTransformer();
transformer.setOutputFile( project.getFile() );
transformer.setProject( project.getFile() );
transformer.setUpdatedModel ( model );
transformer.transformNodes();
transformer.write();
}
catch ( IOException e )
catch ( Exception e )
{
throw new MojoExecutionException( "Can't update pom.", e );
}

View File

@ -0,0 +1,349 @@
package org.apache.maven.plugin.transformer;
/* ====================================================================
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed 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 org.apache.maven.model.Model;
import org.codehaus.plexus.util.FileUtils;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.jaxen.XPath;
import org.jaxen.dom4j.Dom4jXPath;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* This is the base class for any tool that attempts to transform fields
* in the POM. Currently we are using the XML form of the POM and using Jaxen
* but eventually we will be able to perform the same transformations on
* POM beans. Jaxen needs to be modified and some serious cleanup needs to
* go on in Maven internally, but this will serve as a start. An attempt is
* made to make this tool GUI friendly.
*
* @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
*
* @version $Id: AbstractPomTransformer.java 115932 2004-08-06 22:43:03Z carlos $
*/
public abstract class AbstractPomTransformer
implements PomTransformer
{
/** POM document */
private File project;
/** Dom4j document. */
private Document document;
/** Output file. */
private File outputFile;
/** Properties used in transformNode */
private Map variables;
/** Nodes selected for transformation using xpath. */
private List selectedNodes;
/** Updated model obtain from MavenProject. */
private Model updatedModel;
private List transformations;
// -------------------------------------------------------------------------
// Accessors
// -------------------------------------------------------------------------
/**
*
* @param updatedModel
*/
public Model getUpdatedModel()
{
return updatedModel;
}
/**
*
* @param updatedModel
*/
public void setUpdatedModel( Model updatedModel )
{
this.updatedModel = updatedModel;
}
/**
*
* @return
*/
public Map getVariables()
{
return variables;
}
/**
*
* @param variables
*/
public void setVariables( Map variables )
{
this.variables = variables;
}
/**
*
* @param project
*/
public void setProject( File project )
{
this.project = project;
}
/**
*
* @return
*/
public File getProject()
{
return project;
}
/**
*
* @return
*/
public Document getDocument()
{
return document;
}
/**
*
* @param document
*/
public void setDocument( Document document )
{
this.document = document;
}
/**
*
* @return
*/
public File getOutputFile()
{
return outputFile;
}
/**
*
* @param outputFile
*/
public void setOutputFile( File outputFile )
{
this.outputFile = outputFile;
}
/**
*
* @return
*/
public List getSelectedNodes()
{
if ( selectedNodes == null )
{
try
{
selectNodes();
}
catch ( Exception e )
{
// do nothing.
}
}
return selectedNodes;
}
/**
*
* @param selectedNodes
*/
public void setSelectedNodes( List selectedNodes )
{
this.selectedNodes = selectedNodes;
}
/**
*
* @return
*/
public int getSelectedNodeCount()
{
return getSelectedNodes().size();
}
/**
*
* @return
*/
public List getTransformations()
{
if ( transformations == null )
{
createTransformations();
}
return transformations;
}
/**
*
*/
public void createTransformations()
{
transformations = new ArrayList();
for ( Iterator i = getSelectedNodes().iterator(); i.hasNext(); )
{
Object o = i.next();
if ( o instanceof Node )
{
Transformation transformation = new Transformation( this );
transformation.setNode( (Node) o );
transformations.add( transformation );
}
}
}
/**
* This is the automated way of transforming the nodes if there is
* no user interaction involved.
*
* @throws Exception If an error occurs while transforming the nodes.
*/
public void transformNodes()
throws Exception
{
for ( Iterator i = getSelectedNodes().iterator(); i.hasNext(); )
{
Object o = i.next();
if ( o instanceof Node )
{
transformNode( (Node) o );
}
}
}
// ----------------------------------------------------------------------
// Implementation
// ----------------------------------------------------------------------
/**
*
* @return
*/
public abstract String selectProjectNodeXPathExpression();
/**
*
* @return
*/
public abstract String selectDependenciesNodesXPathExpression();
/**
*
* @return
*/
public abstract String selectPluginsNodesXPathExpression();
/**
*
* @param node
* @throws Exception
*/
public abstract void transformNode( Node node )
throws Exception;
/**
* Update the snapshot version identifiers with actual timestamp versions
* and write out the POM in its updated form.
*
* @throws Exception
*/
public void selectNodes()
throws Exception
{
SAXReader reader = new SAXReader();
setDocument( reader.read( getProject() ) );
// The selecting nodes with the xpath expression will give us a list
// of dependencies elements where the version element is equal to 'SNAPSHOT'.
// So we can get any information we need, and alter anything we need to before writing
// the dom4j document back out.
XPath pomXpath = new Dom4jXPath( selectProjectNodeXPathExpression() );
XPath dependenciesXpath = new Dom4jXPath( selectDependenciesNodesXPathExpression() );
XPath pluginsXpath = new Dom4jXPath( selectPluginsNodesXPathExpression() );
List nodes = new ArrayList();
nodes.addAll( pomXpath.selectNodes( getDocument() ) );
nodes.addAll( dependenciesXpath.selectNodes( getDocument() ) );
nodes.addAll( pluginsXpath.selectNodes( getDocument() ) );
setSelectedNodes( nodes );
}
/**
*
* @throws Exception
*/
public void write()
throws Exception
{
OutputStream os = null;
if ( getOutputFile() != null )
{
// Backup the original first.
FileUtils.copyFile( getOutputFile(), new File( getOutputFile() + ".backup" ) );
// Now hand of the os.
os = new FileOutputStream( getOutputFile() );
}
else
{
os = new PrintStream( System.out );
}
OutputFormat format = new OutputFormat();
format.setIndentSize( 2 );
format.setNewlines( true );
format.setTrimText( true );
XMLWriter writer = new XMLWriter( format );
writer.setOutputStream( os );
writer.write( getDocument() );
writer.flush();
writer.close();
}
}

View File

@ -0,0 +1,56 @@
package org.apache.maven.plugin.transformer;
/* ====================================================================
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed 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 org.apache.maven.model.Model;
import org.dom4j.Node;
/**
* @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
*
* @version $Id: PomTransformer.java 114783 2004-03-02 15:37:56Z evenisse $
*/
public interface PomTransformer
{
File getProject();
void setProject( File project );
void transformNodes()
throws Exception;
void transformNode( Node node )
throws Exception;
Node getTransformedNode( Node node )
throws Exception;
void write()
throws Exception;
Model getUpdatedModel();
void setUpdatedModel( Model updatedModel );
File getOutputFile();
void setOutputFile( File outputFile );
}

View File

@ -0,0 +1,95 @@
package org.apache.maven.plugin.transformer;
/* ====================================================================
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed 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 org.dom4j.Node;
/**
*
*
* @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
*
* @version $Id: Transformation.java 114783 2004-03-02 15:37:56Z evenisse $
*/
public class Transformation
{
/** Pom Transformer associated with this transformation. */
private PomTransformer pomTransformer;
/** Node to transform. */
private Node node;
public Transformation( PomTransformer pomTransformer )
{
this.pomTransformer = pomTransformer;
}
// ----------------------------------------------------------------------
// Accessors
// ----------------------------------------------------------------------
/**
*
* @return
*/
public Node getNode()
{
return node;
}
/**
*
* @param node
*/
public void setNode( Node node )
{
this.node = node;
}
/**
*
* @throws Exception
*/
public void transform()
throws Exception
{
pomTransformer.transformNode( node );
}
/**
*
* @return
* @throws Exception
*/
public String getBeforeTransformation()
throws Exception
{
return getNode().asXML();
}
/**
*
* @return
* @throws Exception
*/
public String getAfterTransformation()
throws Exception
{
return pomTransformer.getTransformedNode( getNode() ).asXML();
}
}

View File

@ -0,0 +1,160 @@
package org.apache.maven.plugin.transformer;
/* ====================================================================
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed 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.util.Iterator;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Plugin;
import org.dom4j.Element;
import org.dom4j.Node;
/**
*
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
*
* @version $Id: VersionTransformer.java 115421 2004-06-01 02:20:18Z dion $
*/
public class VersionTransformer
extends AbstractPomTransformer
{
// -------------------------------------------------------------------------
// Accessors
// -------------------------------------------------------------------------
public String selectProjectNodeXPathExpression()
{
return "/project";
}
public String selectDependenciesNodesXPathExpression()
{
return "/project/dependencies/dependency";
}
public String selectPluginsNodesXPathExpression()
{
return "/project/build/plugins/plugin";
}
public void transformNode( Node node )
throws Exception
{
if ( selectProjectNodeXPathExpression().equals( node.getPath() ) )
{
Element project = (Element) node;
Node version = node.selectSingleNode( "version" );
if ( version != null )
{
version.setText( getUpdatedModel().getVersion() );
}
else
{
project.addElement( "version" ).addText( getUpdatedModel().getVersion() );
}
}
else if ( selectDependenciesNodesXPathExpression().equals( node.getPath() ) )
{
Element dependency = (Element) node;
Node groupId = node.selectSingleNode( "groupId" );
Node artifactId = node.selectSingleNode( "artifactId" );
Node type = node.selectSingleNode( "type" );
String typeText = "jar";
if ( type != null )
{
typeText = type.getText();
}
Node version = node.selectSingleNode( "version" );
if ( version != null )
{
version.setText( getDependency( groupId.getText(), artifactId.getText(), typeText ).getVersion() );
}
else
{
dependency.addElement( "version" ).addText( getDependency( groupId.getText(), artifactId.getText(),
type.getText() ).getVersion() );
}
}
else
{
Element plugin = (Element) node;
Node groupId = node.selectSingleNode( "groupId" );
String groupIdText = "org.apache.maven.plugins";
if ( groupId != null )
{
groupIdText = groupId.getText();
}
Node artifactId = node.selectSingleNode( "artifactId" );
Node version = node.selectSingleNode( "version" );
Plugin p = getPlugin( groupIdText, artifactId.getText() );
if ( groupId != null )
{
groupId.setText( p.getGroupId() );
}
else
{
plugin.addElement( "groupId" ).addText( p.getGroupId() );
}
if ( version != null )
{
version.setText( p.getVersion() );
}
else
{
plugin.addElement( "version" ).addText( p.getVersion() );
}
}
}
private Dependency getDependency( String groupId, String artifactId, String type )
{
for ( Iterator i = getUpdatedModel().getDependencies().iterator(); i.hasNext(); )
{
Dependency dependency = (Dependency) i.next();
if ( dependency.getGroupId().equals( groupId ) && dependency.getArtifactId().equals( artifactId )
&& dependency.getType().equals( type ) )
{
return dependency;
}
}
return null;
}
private Plugin getPlugin( String groupId, String artifactId )
{
for ( Iterator i = getUpdatedModel().getBuild().getPlugins().iterator(); i.hasNext(); )
{
Plugin plugin = (Plugin) i.next();
if ( plugin.getGroupId().equals( groupId ) && plugin.getArtifactId().equals( artifactId ) )
{
return plugin;
}
}
return null;
}
public Node getTransformedNode( Node node )
throws Exception
{
throw new UnsupportedOperationException( "getTransformedNode not implemented" );
}
}