diff --git a/artifact/project.xml b/artifact/project.xml index 0e2666bd..1a2aaf10 100644 --- a/artifact/project.xml +++ b/artifact/project.xml @@ -18,7 +18,8 @@ */ --> - + ../plugin-parent/project.xml 3 maven-artifact-plugin @@ -89,6 +90,21 @@ commons-digester 1.4.1 + + commons-collections + commons-collections + 3.1 + + + commons-beanutils + commons-beanutils + 1.6.1 + + + dom4j + dom4j + 1.4 + commons-net commons-net @@ -165,4 +181,14 @@ 3.0.0 + + src/test/java + + + + src/test/resources + + + + diff --git a/artifact/src/main/org/apache/maven/artifact/PomRewriter.java b/artifact/src/main/org/apache/maven/artifact/PomRewriter.java new file mode 100644 index 00000000..2db516b8 --- /dev/null +++ b/artifact/src/main/org/apache/maven/artifact/PomRewriter.java @@ -0,0 +1,176 @@ +package org.apache.maven.artifact; + +/* + * 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.project.Project; +import org.apache.maven.MavenException; +import org.apache.maven.MavenUtils; +import org.apache.maven.jelly.MavenJellyContext; +import org.apache.maven.model.Model; +import org.apache.maven.model.Dependency; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; +import org.apache.maven.model.io.xpp3.MavenXpp3Writer; +import org.apache.commons.betwixt.io.BeanWriter; +import org.apache.commons.betwixt.XMLIntrospector; +import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper; +import org.apache.oro.text.perl.Perl5Util; +import org.codehaus.plexus.util.IOUtil; + +import java.io.File; +import java.io.ByteArrayOutputStream; +import java.io.StringReader; +import java.io.FileWriter; +import java.io.IOException; +import java.lang.reflect.Method; +import java.util.Iterator; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +/** + * Rewrite a full model for publishing. Inheritence and expression will have been resolved. + * + * @author Brett Porter + * @version $Id$ + */ +public class PomRewriter +{ + public static File getRewrittenPom( Project project ) + throws MavenException + { + Model model = getRewrittenModel( project ); + + FileWriter w = null; + try + { + MavenXpp3Writer writer = new MavenXpp3Writer(); + File f = File.createTempFile( "maven-artifact-plugin.", null ); + f.deleteOnExit(); + w = new FileWriter( f ); + writer.write( w, model ); + + return f; + } + catch ( IOException e ) + { + throw new MavenException( "Error getting the project as a string", e ); + } + finally + { + IOUtil.close( w ); + } + } + + static Model getRewrittenModel( Project project ) + throws MavenException + { + Model model; + try + { + // Very gross, but in Maven 1.0 we can't get access, and we don't want initialize() called + // A future version should use use project.getModel() and serialize that + Method m = MavenUtils.class.getDeclaredMethod( "getNonJellyProject", + new Class[]{File.class, MavenJellyContext.class, + boolean.class} ); + m.setAccessible( true ); + Project p = (Project) m.invoke( null, + new Object[]{project.getFile(), project.getContext(), Boolean.TRUE} ); + m.setAccessible( false ); + m = MavenUtils.class.getDeclaredMethod( "getJellyProject", new Class[]{Project.class} ); + m.setAccessible( true ); + p = (Project) m.invoke( null, new Object[]{p} ); + m.setAccessible( false ); + + // now sanitize + p.setContext( null ); + p.setParent( null ); + p.setArtifacts( null ); + p.setDependencyVerifier( null ); + p.setExtend( null ); + + Map depProperties = new HashMap(); + for ( Iterator i = p.getDependencies().iterator(); i.hasNext(); ) + { + org.apache.maven.project.Dependency d = (org.apache.maven.project.Dependency) i.next(); + if ( d.getProperties() != null && !d.getProperties().isEmpty() ) + { + depProperties.put( d.getId(), d.getProperties() ); + d.setProperties( null ); + } + } + + ByteArrayOutputStream projectStream = new ByteArrayOutputStream(); + + BeanWriter beanWriter = new BeanWriter( projectStream ); + beanWriter.setXMLIntrospector( createXMLIntrospector() ); + + beanWriter.enablePrettyPrint(); + beanWriter.setWriteIDs( false ); + beanWriter.write( p ); + + String asString = projectStream.toString( System.getProperty( "file.encoding" ) ); + + MavenXpp3Reader reader = new MavenXpp3Reader(); + model = reader.read( new StringReader( asString ) ); + model.setId( null ); + + for ( Iterator i = model.getDependencies().iterator(); i.hasNext(); ) + { + Dependency d = (Dependency) i.next(); + + if ( depProperties.containsKey( d.getId() ) ) + { + d.getProperties().putAll( (Properties) depProperties.get( d.getId() ) ); + } + + d.setId( null ); + + if ( d.getUrl() != null && d.getUrl().length() == 0 ) + { + d.setUrl( null ); + } + + if ( d.getType() != null && d.getType().length() == 0 ) + { + d.setType( null ); + } + + if ( d.getJar() != null && d.getJar().length() == 0 ) + { + d.setJar( null ); + } + + + } + } + catch ( Exception e ) + { + throw new MavenException( "Error getting the project as a string", e ); + } + return model; + } + + private static XMLIntrospector createXMLIntrospector() + { + XMLIntrospector introspector = new XMLIntrospector(); + + introspector.setAttributesForPrimitives( false ); + introspector.setElementNameMapper( new DecapitalizeNameMapper() ); + + return introspector; + } +} diff --git a/artifact/src/main/org/apache/maven/artifact/deployer/DefaultArtifactDeployer.java b/artifact/src/main/org/apache/maven/artifact/deployer/DefaultArtifactDeployer.java index 694df826..c7467aa9 100644 --- a/artifact/src/main/org/apache/maven/artifact/deployer/DefaultArtifactDeployer.java +++ b/artifact/src/main/org/apache/maven/artifact/deployer/DefaultArtifactDeployer.java @@ -1,20 +1,19 @@ package org.apache.maven.artifact.deployer; -/* ==================================================================== - * Copyright 2001-2004 The Apache Software Foundation. +/* + * 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 + * 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 + * 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. - * ==================================================================== + * 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.commons.betwixt.XMLIntrospector; @@ -26,6 +25,7 @@ import org.apache.commons.logging.LogFactory; import org.apache.maven.MavenConstants; import org.apache.maven.MavenException; import org.apache.maven.MavenUtils; +import org.apache.maven.artifact.PomRewriter; import org.apache.maven.jelly.MavenJellyContext; import org.apache.maven.model.Dependency; import org.apache.maven.model.Model; @@ -118,7 +118,7 @@ public class DefaultArtifactDeployer File file; if ( POM_TYPE.equals( type ) ) { - file = getRewrittenPom( project ); + file = PomRewriter.getRewrittenPom( project ); } else { @@ -128,111 +128,12 @@ public class DefaultArtifactDeployer // do not deploy POM twice if ( !POM_TYPE.equals( type ) ) { - doDeploy( getRewrittenPom( project ), project, POM_ARTIFACT_TYPE_HANDLER, version, POM_TYPE ); + doDeploy( PomRewriter.getRewrittenPom( project ), project, POM_ARTIFACT_TYPE_HANDLER, version, POM_TYPE ); } doDeploy( file, project, handler, version, type ); } - private static File getRewrittenPom( Project project ) - throws MavenException - { - Model model; - try - { - // Very gross, but in Maven 1.0 we can't get access, and we don't want initialize() called - // A future version should use use project.getModel() and serialize that - Method m = MavenUtils.class.getDeclaredMethod( "getNonJellyProject", - new Class[]{File.class, MavenJellyContext.class, - boolean.class} ); - m.setAccessible( true ); - Project p = (Project) m.invoke( null, - new Object[]{project.getFile(), project.getContext(), Boolean.TRUE} ); - m.setAccessible( false ); - m = MavenUtils.class.getDeclaredMethod( "getJellyProject", new Class[]{Project.class} ); - m.setAccessible( true ); - p = (Project) m.invoke( null, new Object[]{p} ); - m.setAccessible( false ); - - // now sanitize - p.setContext( null ); - p.setParent( null ); - p.setArtifacts( null ); - p.setDependencyVerifier( null ); - p.setExtend( null ); - - ByteArrayOutputStream projectStream = new ByteArrayOutputStream(); - - BeanWriter beanWriter = new BeanWriter( projectStream ); - beanWriter.setXMLIntrospector( createXMLIntrospector() ); - - beanWriter.enablePrettyPrint(); - beanWriter.setWriteIDs( false ); - beanWriter.write( p ); - - String asString = projectStream.toString( System.getProperty( "file.encoding" ) ); - - MavenXpp3Reader reader = new MavenXpp3Reader(); - model = reader.read( new StringReader( asString ) ); - model.setId( null ); - - for ( Iterator i = model.getDependencies().iterator(); i.hasNext(); ) - { - Dependency d = (Dependency) i.next(); - d.setId( null ); - - if ( d.getUrl() != null && d.getUrl().length() == 0 ) - { - d.setUrl( null ); - } - - if ( d.getType() != null && d.getType().length() == 0 ) - { - d.setType( null ); - } - - if ( d.getJar() != null && d.getJar().length() == 0 ) - { - d.setJar( null ); - } - } - } - catch ( Exception e ) - { - throw new MavenException( "Error getting the project as a string", e ); - } - - FileWriter w = null; - try - { - MavenXpp3Writer writer = new MavenXpp3Writer(); - File f = File.createTempFile( "maven-artifact-plugin.", null ); - f.deleteOnExit(); - w = new FileWriter( f ); - writer.write( w, model ); - - return f; - } - catch ( IOException e ) - { - throw new MavenException( "Error getting the project as a string", e ); - } - finally - { - IOUtil.close( w ); - } - } - - private static XMLIntrospector createXMLIntrospector() - { - XMLIntrospector introspector = new XMLIntrospector(); - - introspector.setAttributesForPrimitives( false ); - introspector.setElementNameMapper( new DecapitalizeNameMapper() ); - - return introspector; - } - /** * @see ArtifactDeployer#install(String, String, Project, ArtifactTypeHandler) */ @@ -258,7 +159,7 @@ public class DefaultArtifactDeployer File file; if ( POM_TYPE.equals( type ) ) { - file = getRewrittenPom( project ); + file = PomRewriter.getRewrittenPom( project ); } else { @@ -270,7 +171,7 @@ public class DefaultArtifactDeployer // do not install twice if ( !POM_TYPE.equals( type ) ) { - doInstall( getRewrittenPom( project ), POM_TYPE, project, version, POM_ARTIFACT_TYPE_HANDLER ); + doInstall( PomRewriter.getRewrittenPom( project ), POM_TYPE, project, version, POM_ARTIFACT_TYPE_HANDLER ); } } diff --git a/artifact/src/test/java/org/apache/maven/artifact/PomRewriterTest.java b/artifact/src/test/java/org/apache/maven/artifact/PomRewriterTest.java new file mode 100644 index 00000000..9c7f9cc3 --- /dev/null +++ b/artifact/src/test/java/org/apache/maven/artifact/PomRewriterTest.java @@ -0,0 +1,60 @@ +package org.apache.maven.artifact; + +/* + * 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 junit.framework.TestCase; +import org.apache.maven.project.Project; +import org.apache.maven.model.Model; +import org.apache.maven.model.Dependency; +import org.apache.maven.MavenException; +import org.apache.maven.MavenUtils; + +import java.io.File; + +/** + * Test the POM rewriter. + * + * @author Brett Porter + * @version $Id$ + */ +public class PomRewriterTest + extends TestCase +{ + public void testPropertiesRewriting() + throws MavenException + { + Project project = getProject( "pom-with-properties.xml" ); + + Model model = PomRewriter.getRewrittenModel( project ); + + Dependency dep = (Dependency) model.getDependencies().get( 0 ); + assertEquals( "check property war.bundle", "true", dep.getProperty( "war.bundle" ) ); + assertEquals( "check num properties", 1, dep.getProperties().size() ); + + dep = (Dependency) model.getDependencies().get( 1 ); + assertEquals( "check property gump.project", "jakarta-taglibs-standard", dep.getProperty( "gump.project" ) ); + assertEquals( "check property gump.id", "jstl", dep.getProperty( "gump.id" ) ); + assertEquals( "check num properties", 2, dep.getProperties().size() ); + } + + private static Project getProject( String resourceName ) + throws MavenException + { + // TODO: use a resource if MavenUtils supported it + return MavenUtils.getProject( new File( System.getProperty( "basedir"), "src/test/resources/" + resourceName ) ); + } +} diff --git a/artifact/src/test/resources/pom-with-properties.xml b/artifact/src/test/resources/pom-with-properties.xml new file mode 100644 index 00000000..e8bc369d --- /dev/null +++ b/artifact/src/test/resources/pom-with-properties.xml @@ -0,0 +1,27 @@ + + 3 + artifactId + groupId + 1.0-SNAPSHOT + + + a1 + g1 + v1 + + true + + + + jstl + jstl + 1.0.6 + http://jakarta.apache.org/taglibs/doc/standard-1.0-doc/intro.html + + jakarta-taglibs-standard + jstl + + + + diff --git a/artifact/xdocs/changes.xml b/artifact/xdocs/changes.xml index ba7faec6..9a23e14c 100644 --- a/artifact/xdocs/changes.xml +++ b/artifact/xdocs/changes.xml @@ -26,6 +26,7 @@ + Correct the translation of dependency properties on deployment of the POM Upgrade Wagon SSH External provider to correct issues