View Javadoc

1   package org.apache.maven.artifact.ant;
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 org.apache.tools.ant.BuildException;
23  import org.apache.tools.ant.PropertyHelper;
24  import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
25  
26  /**
27   * The property intercepter that handles the calls for "pom." properties in Ant 1.6 - 1.7.1
28   */
29  class POMPropertyHelper
30      extends PropertyHelper
31  {
32  
33      protected final Pom pom;
34  
35      POMPropertyHelper( Pom pom )
36      {
37          this.pom = pom;
38      }
39  
40      /**
41       * The method that gets called by Ant 1.6 - 1.7.1 with every request of property
42       */
43      public Object getPropertyHook( String ns, String name, boolean user )
44      {
45          String prefix = this.pom.antId + ".";
46  
47          if ( !name.startsWith( prefix ) )
48          {
49              // pass on to next interceptor
50              return super.getPropertyHook( ns, name, user );
51          }
52          try
53          {
54              // else handle the property resolution
55              String expression = name.substring( prefix.length() );
56              return getPOMValue( "project." + expression );
57          }
58          catch ( Exception ex )
59          {
60              ex.printStackTrace();
61              return null;
62          }
63      }
64  
65      private static final String PROPERTIES_PREFIX = "project.properties.";
66  
67      protected Object getPOMValue( String expression )
68      {
69          Object value = null;
70  
71          try
72          {
73              if ( expression.startsWith( PROPERTIES_PREFIX ) )
74              {
75                  expression = expression.substring( PROPERTIES_PREFIX.length() );
76                  value = pom.getMavenProject().getProperties().get( expression );
77              }
78              else
79              {
80                  value = ReflectionValueExtractor.evaluate( expression, pom.getMavenProject() );
81              }
82          }
83          catch ( Exception e )
84          {
85              throw new BuildException( "Error extracting expression from POM", e );
86          }
87  
88          return value;
89      }
90  
91  }