PR: MNG-460

Submitted by: Nicola Ken Barozzi
Reviewed by:  Brett Porter
(applied with modifications)

Replace setProperty by a chained property helper, set up automatically when the POM is first declared.



git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@189873 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-06-10 01:43:21 +00:00
parent 2a8ed2f339
commit cb291e9fa2
4 changed files with 111 additions and 125 deletions

View File

@ -2,11 +2,9 @@
<target name="test-pom">
<artifact:pom file="pom.xml" id="my.maven.project"/>
<artifact:setProperty property="my.artifactid" expression="project.artifactId" pomRefId="my.maven.project" />
<echo>Artifact ID = ${my.artifactid}</echo>
<echo>Artifact ID = ${my.maven.project:artifactId}</echo>
<artifact:setProperty property="my.parent.artifactid" expression="project.parent.artifactId" pomRefId="my.maven.project" />
<echo>Artifact ID = ${my.parent.artifactid}</echo>
<echo>Parent Artifact ID = ${my.maven.project:parent.artifactId}</echo>
</target>
<target name="foo">

View File

@ -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 <a href="mailto:brett@apache.org">Brett Porter</a>
* @author <a href="mailto:nicolaken@apache.org">Nicola Ken Barozzi</a>
* @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;
}
}
}

View File

@ -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 <a href="mailto:brett@apache.org">Brett Porter</a>
* @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;
}
}

View File

@ -1,15 +1,16 @@
<?xml version="1.0"?>
<antlib>
<!-- Tasks -->
<typedef name="dependencies" classname="org.apache.maven.artifact.ant.DependenciesTask"/>
<typedef name="install" classname="org.apache.maven.artifact.ant.InstallTask"/>
<typedef name="deploy" classname="org.apache.maven.artifact.ant.DeployTask"/>
<typedef name="setProperty" classname="org.apache.maven.artifact.ant.SetPropertyTask"/>
<taskdef name="dependencies" classname="org.apache.maven.artifact.ant.DependenciesTask"/>
<taskdef name="install" classname="org.apache.maven.artifact.ant.InstallTask"/>
<taskdef name="deploy" classname="org.apache.maven.artifact.ant.DeployTask"/>
<!-- Types -->
<typedef name="localRepository" classname="org.apache.maven.artifact.ant.LocalRepository"/>
<typedef name="remoteRepository" classname="org.apache.maven.artifact.ant.RemoteRepository"/>
<typedef name="authentication" classname="org.apache.maven.artifact.ant.Authentication"/>
<typedef name="proxy" classname="org.apache.maven.artifact.ant.Proxy"/>
<typedef name="pom" classname="org.apache.maven.artifact.ant.Pom"/>
<!-- Tasks that are also types -->
<taskdef name="pom" classname="org.apache.maven.artifact.ant.Pom"/>
</antlib>