Author: brett Date: Wed Apr 26 23:06:52 2006 New Revision: 397431 URL: http://svn.apache.org/viewcvs?rev=397431&view=rev Log: [MRELEASE-98] switch to JDOM and ensure formatting is preserved
Added: maven/plugins/trunk/maven-release-plugin/src/test/resources/projects/rewrite-for-release/expected-basic-pom-with-schema.xml (with props) maven/plugins/trunk/maven-release-plugin/src/test/resources/projects/rewrite-for-release/expected-basic-pom.xml (with props) Modified: maven/plugins/trunk/maven-release-plugin/pom.xml maven/plugins/trunk/maven-release-plugin/src/main/java/org/apache/maven/plugins/release/phase/RewritePomsForReleasePhase.java maven/plugins/trunk/maven-release-plugin/src/test/java/org/apache/maven/plugins/release/phase/RewritePomsForReleasePhaseTest.java maven/plugins/trunk/maven-release-plugin/src/test/resources/projects/rewrite-for-release/basic-pom.xml Modified: maven/plugins/trunk/maven-release-plugin/pom.xml URL: http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-release-plugin/pom.xml?rev=397431&r1=397430&r2=397431&view=diff ============================================================================== --- maven/plugins/trunk/maven-release-plugin/pom.xml (original) +++ maven/plugins/trunk/maven-release-plugin/pom.xml Wed Apr 26 23:06:52 2006 @@ -105,9 +105,9 @@ <scope>test</scope> </dependency> <dependency> - <groupId>dom4j</groupId> - <artifactId>dom4j</artifactId> - <version>1.6.1</version> + <groupId>jdom</groupId> + <artifactId>jdom</artifactId> + <version>1.0</version> </dependency> </dependencies> <build> Modified: maven/plugins/trunk/maven-release-plugin/src/main/java/org/apache/maven/plugins/release/phase/RewritePomsForReleasePhase.java URL: http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-release-plugin/src/main/java/org/apache/maven/plugins/release/phase/RewritePomsForReleasePhase.java?rev=397431&r1=397430&r2=397431&view=diff ============================================================================== --- maven/plugins/trunk/maven-release-plugin/src/main/java/org/apache/maven/plugins/release/phase/RewritePomsForReleasePhase.java (original) +++ maven/plugins/trunk/maven-release-plugin/src/main/java/org/apache/maven/plugins/release/phase/RewritePomsForReleasePhase.java Wed Apr 26 23:06:52 2006 @@ -31,20 +31,25 @@ import org.apache.maven.scm.repository.ScmRepository; import org.apache.maven.scm.repository.ScmRepositoryException; import org.codehaus.plexus.logging.AbstractLogEnabled; +import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.IOUtil; -import org.dom4j.Document; -import org.dom4j.DocumentException; -import org.dom4j.Element; -import org.dom4j.Namespace; -import org.dom4j.QName; -import org.dom4j.io.SAXReader; -import org.dom4j.io.XMLWriter; +import org.jdom.Document; +import org.jdom.Element; +import org.jdom.JDOMException; +import org.jdom.Namespace; +import org.jdom.filter.ElementFilter; +import org.jdom.input.SAXBuilder; +import org.jdom.output.XMLOutputter; import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; import java.io.Writer; import java.util.Iterator; +import java.util.List; +import java.util.Map; /** * Rewrite POMs for release. @@ -60,8 +65,6 @@ */ private ScmRepositoryConfigurator scmRepositoryConfigurator; - // TODO: separate release POM generation? - public void execute( ReleaseConfiguration releaseConfiguration ) throws ReleaseExecutionException { @@ -73,13 +76,44 @@ getLogger().info( "Transforming " + projectId + " to release" ); - Document document = readPom( project.getFile() ); + Document document; + String intro = null; + String outtro = null; + try + { + String content = FileUtils.fileRead( project.getFile() ); + + SAXBuilder builder = new SAXBuilder(); + document = builder.build( new StringReader( content ) ); + + // rewrite DOM as a string to find differences, since text outside the root element is not tracked + StringWriter w = new StringWriter(); + new XMLOutputter().output( document.getRootElement(), w ); + + int index = content.indexOf( w.toString() ); + if ( index > 0 ) + { + intro = content.substring( 0, index ); + outtro = content.substring( index + w.toString().length() ); + } + } + catch ( JDOMException e ) + { + throw new ReleaseExecutionException( "Error reading POM: " + e.getMessage(), e ); + } + catch ( IOException e ) + { + throw new ReleaseExecutionException( "Error reading POM: " + e.getMessage(), e ); + } - transformPomToReleaseVersionPom(); + transformPomToReleaseVersionPom( projectId, document.getRootElement(), + releaseConfiguration.getReleaseVersions() ); - writePom( project.getFile(), releaseConfiguration, document, project.getModelVersion() ); + writePom( project.getFile(), releaseConfiguration, document, intro, outtro, project.getModelVersion() ); } + // TODO: separate release POM generation into a separate phase? + if ( releaseConfiguration.isGenerateReleasePoms() ) { generateReleasePoms(); @@ -87,26 +121,18 @@ } - private Document readPom( File file ) + private void transformPomToReleaseVersionPom( String projectId, Element rootElement, Map mappedVersions ) throws ReleaseExecutionException { - Document document; - - SAXReader reader = new SAXReader(); - try + // TODO: what about if version is inherited? shouldn't prompt... + Element versionElement = rootElement.getChild( "version", rootElement.getNamespace() ); + String version = (String) mappedVersions.get( projectId ); + if ( version == null ) { - document = reader.read( file ); + throw new ReleaseExecutionException( "Version for '" + projectId + "' was not mapped" ); } - catch ( DocumentException e ) - { - throw new ReleaseExecutionException( "Error reading POM: " + e.getMessage(), e ); - } - - return document; - } + versionElement.setText( version ); - private void transformPomToReleaseVersionPom() - { // TODO: rewrite parent // TODO: rewrite SCM // TODO: rewrite dependencies @@ -271,8 +297,8 @@ } - private void writePom( File pomFile, ReleaseConfiguration releaseConfiguration, Document document, - String modelVersion ) + private void writePom( File pomFile, ReleaseConfiguration releaseConfiguration, Document document, String intro, + String outtro, String modelVersion ) throws ReleaseExecutionException { ScmRepository repository; @@ -314,24 +340,39 @@ if ( releaseConfiguration.isAddSchema() ) { - rootElement.setQName( QName.get( "project", Namespace.get( "", "http://maven.apache.org/POM/4.0.0" ) ) ); + Namespace pomNamespace = Namespace.getNamespace( "", "http://maven.apache.org/POM/" + modelVersion ); + rootElement.setNamespace( pomNamespace ); + Namespace xsiNamespace = Namespace.getNamespace( "xsi", "http://www.w3.org/2001/XMLSchema-instance" ); + rootElement.addNamespaceDeclaration( xsiNamespace ); - rootElement.addNamespace( "xsi", "http://www.w3.org/2001/XMLSchema-instance" ); + if ( rootElement.getAttribute( "schemaLocation", xsiNamespace ) == null ) + { + rootElement.setAttribute( "schemaLocation", "http://maven.apache.org/POM/" + modelVersion + + " http://maven.apache.org/maven-v" + modelVersion.replace( '.', '_' ) + ".xsd", xsiNamespace ); + } - if ( rootElement.attributeValue( "schemaLocation" ) == null ) + // the empty namespace is considered equal to the POM namespace, so match them up to avoid extra xmlns="" + List elements = rootElement.getContent( new ElementFilter( Namespace.getNamespace( "" ) ) ); + for ( Iterator i = elements.iterator(); i.hasNext(); ) { - rootElement.addAttribute( "xsi:schemaLocation", "http://maven.apache.org/POM/" + modelVersion + - " http://maven.apache.org/maven-v" + modelVersion.replace( '.', '_' ) + ".xsd" ); + Element e = (Element) i.next(); + e.setNamespace( pomNamespace ); } } Writer writer = null; try { + // TODO: better handling of encoding. Currently the definition is not written out and is embedded in the intro if it already existed + // TODO: the XMLOutputter and Writer need to have their encodings aligned writer = new FileWriter( pomFile ); - XMLWriter xmlWriter = new XMLWriter( writer ); - xmlWriter.write( document ); + writer.write( intro ); + + XMLOutputter outp = new XMLOutputter(); + outp.output( document.getRootElement(), writer ); + + writer.write( outtro ); } catch ( IOException e ) { Modified: maven/plugins/trunk/maven-release-plugin/src/test/java/org/apache/maven/plugins/release/phase/RewritePomsForReleasePhaseTest.java URL: http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-release-plugin/src/test/java/org/apache/maven/plugins/release/phase/RewritePomsForReleasePhaseTest.java?rev=397431&r1=397430&r2=397431&view=diff ============================================================================== --- maven/plugins/trunk/maven-release-plugin/src/test/java/org/apache/maven/plugins/release/phase/RewritePomsForReleasePhaseTest.java (original) +++ maven/plugins/trunk/maven-release-plugin/src/test/java/org/apache/maven/plugins/release/phase/RewritePomsForReleasePhaseTest.java Wed Apr 26 23:06:52 2006 @@ -19,8 +19,6 @@ import java.io.IOException; import java.util.Collections; import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * Test the SCM modification check phase. @@ -55,11 +53,15 @@ { File testFile = getCopiedTestFile( "rewrite-for-release/basic-pom.xml" ); MavenProject project = projectBuilder.build( testFile, localRepository, null ); + ReleaseConfiguration config = createReleaseConfiguration( Collections.singletonList( project ) ); + config.mapReleaseVersion( project.getGroupId() + ":" + project.getArtifactId(), "1.0" ); phase.execute( config ); - // TODO: assertions + String expected = readTestProjectFile( "rewrite-for-release/expected-basic-pom.xml" ); + String actual = readTestProjectFile( "rewrite-for-release/basic-pom.xml" ); + assertEquals( "Check the transformed POM", expected, actual ); } public void testRewriteAddSchema() @@ -68,6 +70,7 @@ File testFile = getCopiedTestFile( "rewrite-for-release/basic-pom.xml" ); MavenProject project = projectBuilder.build( testFile, localRepository, null ); ReleaseConfiguration config = createReleaseConfiguration( Collections.singletonList( project ) ); + config.mapReleaseVersion( project.getGroupId() + ":" + project.getArtifactId(), "1.0" ); config.setAddSchema( true ); // Run a second time to check they are not duplicated @@ -75,20 +78,9 @@ { phase.execute( config ); - String content = FileUtils.fileRead( testFile ).replace( '\n', ' ' ); - Matcher m = Pattern.compile( "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" ).matcher( content ); - assertTrue( "check for schema location", m.find() ); - assertFalse( "check schema location is not duplicated", m.find() ); - - m = Pattern.compile( - "xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\"" ).matcher( - content ); - assertTrue( "check for schema", m.find() ); - assertFalse( "check for duplicated schema", m.find() ); - - m = Pattern.compile( "xmlns=\"http://maven.apache.org/POM/4.0.0\"" ).matcher( content ); - assertTrue( "check for namespace", m.find() ); - assertFalse( "check for duplicated namespace", m.find() ); + String expected = readTestProjectFile( "rewrite-for-release/expected-basic-pom-with-schema.xml" ); + String actual = readTestProjectFile( "rewrite-for-release/basic-pom.xml" ); + assertEquals( "Check the transformed POM", expected, actual ); } } @@ -98,6 +90,12 @@ File testFile = getTestFile( "target/test-classes/projects/" + fileName ); FileUtils.copyFile( getTestFile( "src/test/resources/projects/" + fileName ), testFile ); return testFile; + } + + private static String readTestProjectFile( String fileName ) + throws IOException + { + return FileUtils.fileRead( getTestFile( "target/test-classes/projects/" + fileName ) ); } private static ReleaseConfiguration createReleaseConfiguration( List reactorProjects ) Modified: maven/plugins/trunk/maven-release-plugin/src/test/resources/projects/rewrite-for-release/basic-pom.xml URL: http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-release-plugin/src/test/resources/projects/rewrite-for-release/basic-pom.xml?rev=397431&r1=397430&r2=397431&view=diff ============================================================================== --- maven/plugins/trunk/maven-release-plugin/src/test/resources/projects/rewrite-for-release/basic-pom.xml (original) +++ maven/plugins/trunk/maven-release-plugin/src/test/resources/projects/rewrite-for-release/basic-pom.xml Wed Apr 26 23:06:52 2006 @@ -1,3 +1,5 @@ +<?xml version="1.0" encoding="UTF-8"?> + <!-- ~ Copyright 2005-2006 The Apache Software Foundation. ~ Added: maven/plugins/trunk/maven-release-plugin/src/test/resources/projects/rewrite-for-release/expected-basic-pom-with-schema.xml URL: http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-release-plugin/src/test/resources/projects/rewrite-for-release/expected-basic-pom-with-schema.xml?rev=397431&view=auto ============================================================================== --- maven/plugins/trunk/maven-release-plugin/src/test/resources/projects/rewrite-for-release/expected-basic-pom-with-schema.xml (added) +++ maven/plugins/trunk/maven-release-plugin/src/test/resources/projects/rewrite-for-release/expected-basic-pom-with-schema.xml Wed Apr 26 23:06:52 2006 @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + ~ Copyright 2005-2006 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. + --> + +<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>groupId</groupId> + <artifactId>artifactId</artifactId> + <version>1.0</version> +</project> \ No newline at end of file Propchange: maven/plugins/trunk/maven-release-plugin/src/test/resources/projects/rewrite-for-release/expected-basic-pom-with-schema.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/plugins/trunk/maven-release-plugin/src/test/resources/projects/rewrite-for-release/expected-basic-pom-with-schema.xml ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: maven/plugins/trunk/maven-release-plugin/src/test/resources/projects/rewrite-for-release/expected-basic-pom.xml URL: http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-release-plugin/src/test/resources/projects/rewrite-for-release/expected-basic-pom.xml?rev=397431&view=auto ============================================================================== --- maven/plugins/trunk/maven-release-plugin/src/test/resources/projects/rewrite-for-release/expected-basic-pom.xml (added) +++ maven/plugins/trunk/maven-release-plugin/src/test/resources/projects/rewrite-for-release/expected-basic-pom.xml Wed Apr 26 23:06:52 2006 @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + ~ Copyright 2005-2006 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. + --> + +<project> + <modelVersion>4.0.0</modelVersion> + <groupId>groupId</groupId> + <artifactId>artifactId</artifactId> + <version>1.0</version> +</project> \ No newline at end of file Propchange: maven/plugins/trunk/maven-release-plugin/src/test/resources/projects/rewrite-for-release/expected-basic-pom.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/plugins/trunk/maven-release-plugin/src/test/resources/projects/rewrite-for-release/expected-basic-pom.xml ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision