From 1ed5a7ab1a2a0a6720dcd2fda80cedb71d1ce458 Mon Sep 17 00:00:00 2001 From: Brett Leslie Porter Date: Wed, 8 Jun 2005 03:37:15 +0000 Subject: [PATCH] add missing file for the set properties capabilities git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@189512 13f79535-47bb-0310-9956-ffa450edef68 --- .../maven/artifact/ant/SetPropertyTask.java | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/SetPropertyTask.java 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 new file mode 100644 index 0000000000..126f2f3980 --- /dev/null +++ b/maven-artifact-ant/src/main/java/org/apache/maven/artifact/ant/SetPropertyTask.java @@ -0,0 +1,112 @@ +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; + } +}