diff --git a/maven-artifact-ant/sample.build.xml b/maven-artifact-ant/sample.build.xml index a76290e168..3e889043b7 100644 --- a/maven-artifact-ant/sample.build.xml +++ b/maven-artifact-ant/sample.build.xml @@ -2,11 +2,9 @@ - - Artifact ID = ${my.artifactid} + Artifact ID = ${my.maven.project:artifactId} - - Artifact ID = ${my.parent.artifactid} + Parent Artifact ID = ${my.maven.project:parent.artifactId} diff --git a/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/Pom.java b/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/Pom.java index 06f685f779..125dd2b3f0 100755 --- a/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/Pom.java +++ b/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/Pom.java @@ -18,6 +18,7 @@ package org.apache.maven.artifact.ant; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.apache.maven.artifact.resolver.ArtifactResolver; import org.apache.maven.model.Build; import org.apache.maven.model.CiManagement; import org.apache.maven.model.DependencyManagement; @@ -30,25 +31,44 @@ import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectBuilder; import org.apache.maven.project.ProjectBuildingException; import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.ProjectComponent; +import org.apache.tools.ant.PropertyHelper; +import org.apache.tools.ant.Task; +import org.apache.tools.ant.Project; +import org.codehaus.plexus.util.introspection.ReflectionValueExtractor; import java.io.File; +import java.util.Collections; /** - * A POM. + * A POM typedef. + * + * Also an Ant Task that registers a handler called POMPropertyHelper + * that intercepts all calls to property value resolution and replies instead + * of Ant to properties that start with the id of the pom. + * + * Example: + * ${maven.project:artifactId} * * @author Brett Porter + * @author Nicola Ken Barozzi * @version $Id$ */ public class Pom - extends ProjectComponent + extends AbstractArtifactTask { private String refid; + private String antId; + private MavenProject mavenProject; private File file; + /** + * The property interceptor. + */ + private final POMPropertyHelper helper = new POMPropertyHelper(); + public String getRefid() { return refid; @@ -59,6 +79,11 @@ public class Pom this.refid = refid; } + public void setId( String id ) + { + this.antId = id; + } + protected Pom getInstance() { Pom instance = this; @@ -81,11 +106,13 @@ public class Pom void initialise( MavenProjectBuilder builder, ArtifactRepository localRepository ) { + // TODO: should this be in execute() too? Would that work when it is used as a type? if ( file != null ) { try { - mavenProject = builder.build( file, localRepository ); + // TODO: should the profiles be constructed and passed in here? From Ant, or perhaps settings? + mavenProject = builder.build( file, localRepository, Collections.EMPTY_LIST ); } catch ( ProjectBuildingException e ) { @@ -237,4 +264,76 @@ public class Pom return getMavenProject().getId(); } + /** + * Registers POMPropertyHelper as a property interceptor + */ + public void execute() + { + ArtifactRepository localRepo = createLocalArtifactRepository(); + MavenProjectBuilder projectBuilder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE ); + initialise( projectBuilder, localRepo ); + + Project project = getProject(); + + // Add a reference to this task/type + project.addReference( antId, this ); + + // Register the property interceptor + PropertyHelper phelper = PropertyHelper.getPropertyHelper( project ); + helper.setNext( phelper.getNext() ); + helper.setProject( project ); + phelper.setNext( helper ); + } + + /** + * The property interceptor that handles the calls for "pom." properties + */ + private class POMPropertyHelper + extends PropertyHelper + { + /** + * The method that gets called by Ant with every request of property + */ + public Object getPropertyHook( String ns, String name, boolean user ) + { + + String prefix = antId + ":"; + + if ( !name.startsWith( prefix ) ) + { + // pass on to next interceptor + return super.getPropertyHook( ns, name, user ); + } + try + { + // else handle the property resolution + String expression = name.substring( prefix.length() ); + return getPOMValue( "project." + expression ); + + } + catch ( Exception ex ) + { + ex.printStackTrace(); + return null; + } + } + + private Object getPOMValue( String expression ) + { + Object value = null; + + try + { + value = ReflectionValueExtractor.evaluate( expression, getMavenProject() ); + } + catch ( Exception e ) + { + throw new BuildException( "Error extracting expression from POM", e ); + } + + return value; + } + + } + } diff --git a/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/SetPropertyTask.java b/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/SetPropertyTask.java deleted file mode 100644 index 126f2f3980..0000000000 --- a/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/SetPropertyTask.java +++ /dev/null @@ -1,112 +0,0 @@ -package org.apache.maven.artifact.ant; - -/* - * 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.artifact.repository.ArtifactRepository; -import org.apache.maven.project.MavenProjectBuilder; -import org.apache.tools.ant.BuildException; -import org.codehaus.plexus.util.introspection.ReflectionValueExtractor; - -/** - * Set a property from a POM. - * - * @author Brett Porter - * @version $Id$ - */ -public class SetPropertyTask - extends AbstractArtifactTask -{ - private String ref; - - private String property; - - private String expression; - - public void execute() - throws BuildException - { - ArtifactRepository localRepo = createLocalArtifactRepository(); - - MavenProjectBuilder builder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE ); - Pom pom = buildPom( builder, localRepo ); - - if ( expression == null ) - { - throw new BuildException( "the expression attribute is required" ); - } - - if ( property == null && ref == null ) - { - throw new BuildException( "the property or ref attribute is required" ); - } - - else if ( property != null && ref != null ) - { - throw new BuildException( "only one of the property or ref attribute is allowed" ); - - } - - Object value = null; - try - { - value = ReflectionValueExtractor.evaluate( expression, pom.getMavenProject() ); - } - catch ( Exception e ) - { - throw new BuildException( "Error extracting expression from POM", e ); - } - - if ( property != null ) - { - getProject().setProperty( property, value.toString() ); - } - else - { - getProject().addReference( ref, value ); - } - } - - public String getProperty() - { - return property; - } - - public void setProperty( String property ) - { - this.property = property; - } - - public String getExpression() - { - return expression; - } - - public void setExpression( String expression ) - { - this.expression = expression; - } - - public String getRef() - { - return ref; - } - - public void setRef( String ref ) - { - this.ref = ref; - } -} diff --git a/maven-artifact-ant/src/main/resources/org/apache/maven/artifact/ant/antlib.xml b/maven-artifact-ant/src/main/resources/org/apache/maven/artifact/ant/antlib.xml index b363938cb5..98fdf1990e 100755 --- a/maven-artifact-ant/src/main/resources/org/apache/maven/artifact/ant/antlib.xml +++ b/maven-artifact-ant/src/main/resources/org/apache/maven/artifact/ant/antlib.xml @@ -1,15 +1,16 @@ - - - - + + + - + + +