View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  package org.basepom.mojo.propertyhelper;
16  
17  import org.basepom.mojo.propertyhelper.macros.MacroType;
18  
19  import java.io.File;
20  import java.security.SecureRandom;
21  import java.util.Collections;
22  import java.util.Map;
23  import java.util.Properties;
24  import java.util.Random;
25  
26  import org.apache.maven.project.MavenProject;
27  import org.apache.maven.settings.Settings;
28  
29  /**
30   * All accessible values from the Maven build system for a Field.
31   */
32  public interface FieldContext {
33  
34      /** Returns a fixed instance for testing. Do not use outside test code. */
35      static FieldContext forTesting() {
36          return forTesting(new SecureRandom());
37      }
38  
39      /** Returns a fixed instance for testing. The Random instance can be set to simulate reproducible builds. */
40      static FieldContext forTesting(Random random) {
41          return new FieldContext() {
42  
43              @Override
44              public Map<String, MacroType> getMacros() {
45                  return Collections.emptyMap();
46              }
47  
48              @Override
49              public Properties getProjectProperties() {
50                  return new Properties();
51              }
52  
53              @Override
54              public MavenProject getProject() {
55                  return new MavenProject();
56              }
57  
58              @Override
59              public Settings getSettings() {
60                  return new Settings();
61              }
62  
63              @Override
64              public File getBasedir() {
65                  throw new UnsupportedOperationException();
66              }
67  
68              @Override
69              public InterpolatorFactory getInterpolatorFactory() {
70                  return InterpolatorFactory.forTesting();
71              }
72  
73              @Override
74              public TransformerRegistry getTransformerRegistry() {
75                  return TransformerRegistry.INSTANCE;
76              }
77  
78              @Override
79              public Random getRandom() {
80                  return random;
81              }
82          };
83      }
84  
85      /**
86       * Returns a map with all known macros. Key value is the macro hint as given by the plexus component annotation.
87       * @return A map with all known macros.
88       */
89      Map<String, MacroType> getMacros();
90  
91      /**
92       * Return the maven project properties.
93       * @return A properties object for the project properties.
94       */
95      Properties getProjectProperties();
96  
97      /**
98       * Returns a reference to the {@link MavenProject}.
99       * @return A {@link MavenProject} object
100      */
101     MavenProject getProject();
102 
103     /**
104      * Returns the current maven {@link Settings} object
105      * @return A {@link Settings} object
106      */
107     Settings getSettings();
108 
109     /**
110      * Returns the base dir for this maven build execution.
111      *
112      * @return A {@link File} object.
113      */
114     File getBasedir();
115 
116     /**
117      * Returns the {@link InterpolatorFactory} that can interpolate "late resolution" properties.
118      * @return An {@link InterpolatorFactory} reference.
119      */
120     InterpolatorFactory getInterpolatorFactory();
121 
122     /**
123      * Returns a reference to the {@link TransformerRegistry}.
124      * @return A {@link TransformerRegistry} object.
125      */
126     TransformerRegistry getTransformerRegistry();
127 
128     /**
129      * Returns a {@link java.security.SecureRandom} instance for generating random values.
130      */
131     Random getRandom();
132 }