View Javadoc
1   package org.apache.maven.model.interpolation;
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.ArrayList;
24  import java.util.Arrays;
25  import java.util.Collection;
26  import java.util.HashSet;
27  import java.util.List;
28  import java.util.Properties;
29  
30  import javax.inject.Inject;
31  
32  import org.apache.maven.model.Model;
33  import org.apache.maven.model.building.ModelBuildingRequest;
34  import org.apache.maven.model.building.ModelProblemCollector;
35  import org.apache.maven.model.path.PathTranslator;
36  import org.apache.maven.model.path.UrlNormalizer;
37  import org.codehaus.plexus.interpolation.AbstractValueSource;
38  import org.codehaus.plexus.interpolation.InterpolationPostProcessor;
39  import org.codehaus.plexus.interpolation.MapBasedValueSource;
40  import org.codehaus.plexus.interpolation.ObjectBasedValueSource;
41  import org.codehaus.plexus.interpolation.PrefixAwareRecursionInterceptor;
42  import org.codehaus.plexus.interpolation.PrefixedObjectValueSource;
43  import org.codehaus.plexus.interpolation.PrefixedValueSourceWrapper;
44  import org.codehaus.plexus.interpolation.RecursionInterceptor;
45  import org.codehaus.plexus.interpolation.ValueSource;
46  
47  /**
48   * Use a regular expression search to find and resolve expressions within the POM.
49   *
50   * @author jdcasey Created on Feb 3, 2005
51   */
52  public abstract class AbstractStringBasedModelInterpolator
53      implements ModelInterpolator
54  {
55      private static final List<String> PROJECT_PREFIXES = Arrays.asList( "pom.", "project." );
56  
57      private static final Collection<String> TRANSLATED_PATH_EXPRESSIONS;
58  
59      static
60      {
61          Collection<String> translatedPrefixes = new HashSet<>();
62  
63          // MNG-1927, MNG-2124, MNG-3355:
64          // If the build section is present and the project directory is non-null, we should make
65          // sure interpolation of the directories below uses translated paths.
66          // Afterward, we'll double back and translate any paths that weren't covered during interpolation via the
67          // code below...
68          translatedPrefixes.add( "build.directory" );
69          translatedPrefixes.add( "build.outputDirectory" );
70          translatedPrefixes.add( "build.testOutputDirectory" );
71          translatedPrefixes.add( "build.sourceDirectory" );
72          translatedPrefixes.add( "build.testSourceDirectory" );
73          translatedPrefixes.add( "build.scriptSourceDirectory" );
74          translatedPrefixes.add( "reporting.outputDirectory" );
75  
76          TRANSLATED_PATH_EXPRESSIONS = translatedPrefixes;
77      }
78  
79      @Inject
80      private PathTranslator pathTranslator;
81  
82      @Inject
83      private UrlNormalizer urlNormalizer;
84  
85      @Inject
86      private ModelVersionProcessor versionProcessor;
87  
88      public AbstractStringBasedModelInterpolator setPathTranslator( PathTranslator pathTranslator )
89      {
90          this.pathTranslator = pathTranslator;
91          return this;
92      }
93  
94      public AbstractStringBasedModelInterpolator setUrlNormalizer( UrlNormalizer urlNormalizer )
95      {
96          this.urlNormalizer = urlNormalizer;
97          return this;
98      }
99  
100     public AbstractStringBasedModelInterpolator setVersionPropertiesProcessor( ModelVersionProcessor processor )
101     {
102         this.versionProcessor = processor;
103         return this;
104     }
105 
106     protected List<ValueSource> createValueSources( final Model model, final File projectDir,
107                                                     final ModelBuildingRequest config,
108                                                     final ModelProblemCollector problems )
109     {
110         Properties modelProperties = model.getProperties();
111 
112         ValueSource modelValueSource1 = new PrefixedObjectValueSource( PROJECT_PREFIXES, model, false );
113         if ( config.getValidationLevel() >= ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0 )
114         {
115             modelValueSource1 = new ProblemDetectingValueSource( modelValueSource1, "pom.", "project.", problems );
116         }
117 
118         ValueSource modelValueSource2 = new ObjectBasedValueSource( model );
119         if ( config.getValidationLevel() >= ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0 )
120         {
121             modelValueSource2 = new ProblemDetectingValueSource( modelValueSource2, "", "project.", problems );
122         }
123 
124         // NOTE: Order counts here!
125         List<ValueSource> valueSources = new ArrayList<>( 9 );
126 
127         if ( projectDir != null )
128         {
129             ValueSource basedirValueSource = new PrefixedValueSourceWrapper( new AbstractValueSource( false )
130             {
131                 @Override
132                 public Object getValue( String expression )
133                 {
134                     if ( "basedir".equals( expression ) )
135                     {
136                         return projectDir.getAbsolutePath();
137                     }
138                     return null;
139                 }
140             }, PROJECT_PREFIXES, true );
141             valueSources.add( basedirValueSource );
142 
143             ValueSource baseUriValueSource = new PrefixedValueSourceWrapper( new AbstractValueSource( false )
144             {
145                 @Override
146                 public Object getValue( String expression )
147                 {
148                     if ( "baseUri".equals( expression ) )
149                     {
150                         return projectDir.getAbsoluteFile().toPath().toUri().toASCIIString();
151                     }
152                     return null;
153                 }
154             }, PROJECT_PREFIXES, false );
155             valueSources.add( baseUriValueSource );
156             valueSources.add( new BuildTimestampValueSource( config.getBuildStartTime(), modelProperties ) );
157         }
158 
159         valueSources.add( modelValueSource1 );
160 
161         valueSources.add( new MapBasedValueSource( config.getUserProperties() ) );
162 
163         // Overwrite existing values in model properties. Otherwise it's not possible
164         // to define them via command line e.g.: mvn -Drevision=6.5.7 ...
165         versionProcessor.overwriteModelProperties( modelProperties, config );
166         valueSources.add( new MapBasedValueSource( modelProperties ) );
167 
168         valueSources.add( new MapBasedValueSource( config.getSystemProperties() ) );
169 
170         valueSources.add( new AbstractValueSource( false )
171         {
172             @Override
173             public Object getValue( String expression )
174             {
175                 return config.getSystemProperties().getProperty( "env." + expression );
176             }
177         } );
178 
179         valueSources.add( modelValueSource2 );
180 
181         return valueSources;
182     }
183 
184     protected List<? extends InterpolationPostProcessor> createPostProcessors( final Model model,
185                                                                                final File projectDir,
186                                                                                final ModelBuildingRequest config )
187     {
188         List<InterpolationPostProcessor> processors = new ArrayList<>( 2 );
189         if ( projectDir != null )
190         {
191             processors.add( new PathTranslatingPostProcessor( PROJECT_PREFIXES, TRANSLATED_PATH_EXPRESSIONS,
192                                                               projectDir, pathTranslator ) );
193         }
194         processors.add( new UrlNormalizingPostProcessor( urlNormalizer ) );
195         return processors;
196     }
197 
198     protected RecursionInterceptor createRecursionInterceptor()
199     {
200         return new PrefixAwareRecursionInterceptor( PROJECT_PREFIXES );
201     }
202 
203 }