View Javadoc

1   package org.apache.maven.plugin.antrun;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.Collections;
24  import java.util.Hashtable;
25  import java.util.Iterator;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.plugin.logging.Log;
31  import org.apache.maven.project.MavenProject;
32  import org.apache.tools.ant.PropertyHelper;
33  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
34  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
35  import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
36  
37  /**
38   * Makes the ${expressions} used in Maven available to Ant as properties.
39   *
40   * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
41   */
42  public class AntPropertyHelper
43      extends PropertyHelper
44  {
45      private static final String DEPENDENCY_PREFIX = "maven.dependency.";
46      private Log log;
47      private ExpressionEvaluator exprEvaluator;
48      private MavenProject mavenProject;
49      private Map artifactMap = new Hashtable();
50  
51      /**
52       * @deprecated use the other constructor
53       * @param project
54       * @param l
55       */
56      public AntPropertyHelper( MavenProject project, Log l )
57      {
58          mavenProject = project;
59          log = l;
60      }
61  
62      /**
63       * @deprecated use {@link #AntPropertyHelper(ExpressionEvaluator, Set, Log)} to resolve maven.dependency.* properties
64       * @param exprEvaluator
65       * @param l
66       */
67      public AntPropertyHelper( ExpressionEvaluator exprEvaluator, Log l )
68      {
69          this( exprEvaluator, Collections.EMPTY_SET, l );
70      }
71  
72      /**
73       * @param exprEvaluator
74       * @param artifacts
75       * @param l
76       */
77      public AntPropertyHelper( ExpressionEvaluator exprEvaluator, Set artifacts, Log l )
78      {
79          this.mavenProject = null;
80          this.exprEvaluator = exprEvaluator;
81          this.log = l;
82  
83          for ( Iterator it = artifacts.iterator(); it.hasNext(); )
84          {
85              Artifact artifact = (Artifact) it.next();
86  
87              String key = DEPENDENCY_PREFIX + artifact.getGroupId() + "." + artifact.getArtifactId()
88                  + ( artifact.getClassifier() != null ? "." + artifact.getClassifier() : "" )
89                  + ( artifact.getType() != null ? "." + artifact.getType() : "" ) + ".path";
90  
91              log.debug( "Storing: " + key + "=" + artifact.getFile().getPath() );
92  
93              artifactMap.put( key, artifact.getFile().getPath() );
94          }
95      }
96  
97      /**
98       * @see org.apache.tools.ant.PropertyHelper#getPropertyHook(java.lang.String, java.lang.String, boolean)
99       */
100     public synchronized Object getPropertyHook( String ns, String name, boolean user )
101     {
102         if ( log.isDebugEnabled() )
103         {
104             log.debug( "getProperty(ns=" + ns + ", name=" + name + ", user=" + user + ")" );
105         }
106 
107         /* keep old behaviour */
108         if ( mavenProject != null )
109         {
110             return getPropertyHook( ns, name, user, mavenProject );
111         }
112 
113 
114         Object val = null;
115 
116         if ( name.startsWith( DEPENDENCY_PREFIX ) )
117         {
118             val = (String) artifactMap.get( name );
119         }
120 
121         if ( val == null )
122         {
123             try
124             {
125                 val = exprEvaluator.evaluate( "${" + name + "}" );
126             }
127             catch ( ExpressionEvaluationException e )
128             {
129                 if ( log.isErrorEnabled() )
130                 {
131                     log.error( "Failed to evaluate expression", e );
132                 }
133             }
134         }
135 
136         if ( val == null )
137         {
138             val = super.getPropertyHook( ns, name, user );
139 
140             if ( val == null )
141             {
142                 val = System.getProperty( name.toString() );
143             }
144         }
145 
146         return val;
147     }
148 
149     /**
150      * @deprecated added to keep backwards compatibility
151      * @param ns
152      * @param name
153      * @param user
154      * @param mavenProject
155      * @return The property value.
156      */
157     private Object getPropertyHook( String ns, String name, boolean user, MavenProject mavenProject )
158     {
159         Object val = null;
160         try
161         {
162             if ( name.startsWith( DEPENDENCY_PREFIX ) )
163             {
164                 val = (String) artifactMap.get( name );
165             }
166             else if ( name.startsWith( "project." ) )
167             {
168                 val = ReflectionValueExtractor.evaluate(
169                     name,
170                     mavenProject,
171                     true
172                 );
173             }
174             else if ( name.equals( "basedir" ) )
175             {
176                 val = ReflectionValueExtractor.evaluate(
177                     "basedir.path",
178                     mavenProject,
179                     false
180                 );
181             }
182         }
183         catch ( Exception e )
184         {
185             if ( log.isWarnEnabled() )
186             {
187                 log.warn( "Error evaluating expression '" + name + "'", e );
188             }
189         }
190 
191         if ( val == null )
192         {
193             val = super.getPropertyHook( ns, name, user );
194             if ( val == null )
195             {
196                 val = System.getProperty( name.toString() );
197             }
198         }
199 
200         if ( val instanceof File )
201         {
202             val = ( (File) val ).getAbsoluteFile();
203         }
204 
205         return val;
206     }
207 }