View Javadoc

1   package org.apache.maven.plugin.coreit;
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.maven.plugin.AbstractMojo;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugin.MojoFailureException;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.util.HashMap;
29  import java.util.Iterator;
30  import java.util.Map;
31  import java.util.Properties;
32  
33  /**
34   * Creates a properties file with the effective values of some user-defined expressions. Unlike Maven's built-in
35   * expression syntax for interpolation, these expressions use forward slashes to navigate down the object graph and
36   * support access to individual collection/array elements. Furthermore, the result of an expression need not be a scalar
37   * value but can also be a collection/array or a bean-like object (from the Maven model). For example, the expression
38   * "project/dependencies/0" would extract the first project dependency. In more detail, this example expression could
39   * output the following keys to the properties file:
40   * 
41   * <pre>
42   * project.dependencies.0.groupId = org.apache.maven
43   * project.dependencies.0.artifactId = maven-project
44   * project.dependencies.0.type = jar
45   * project.dependencies.0.version = 2.0
46   * project.dependencies.0.optional = false
47   * project.dependencies.0.exclusions = 2
48   * project.dependencies.0.exclusions.0.groupId = plexus
49   * project.dependencies.0.exclusions.0.artifactId = plexus-utils
50   * project.dependencies.0.exclusions.1.groupId = plexus
51   * project.dependencies.0.exclusions.1.artifactId = plexus-container-default
52   * </pre>
53   * 
54   * Expressions that reference non-existing objects or use invalid collection/array indices silently resolve to
55   * <code>null</code>. For collections and arrays, the special index "*" can be used to iterate all elements.
56   * 
57   * @goal eval
58   * @phase initialize
59   * 
60   * @author Benjamin Bentmann
61   * @version $Id: EvalMojo.java 949790 2010-05-31 16:16:22Z bentmann $
62   */
63  public class EvalMojo
64      extends AbstractMojo
65  {
66  
67      /**
68       * The project's base directory, used for manual path translation.
69       * 
70       * @parameter default-value="${basedir}"
71       * @readonly
72       */
73      private File basedir;
74  
75      /**
76       * The path to the output file for the properties with the expression values. For each expression given by the
77       * parameter {@link #expressions} an similar named properties key will be used to save the expression value. If an
78       * expression evaluated to <code>null</code>, there will be no corresponding key in the properties file.
79       * 
80       * @parameter expression="${expression.outputFile}"
81       */
82      private File outputFile;
83  
84      /**
85       * The set of expressions to evaluate.
86       * 
87       * @parameter
88       */
89      private String[] expressions;
90  
91      /**
92       * The comma separated set of expressions to evaluate.
93       * 
94       * @parameter expression="${expression.expressions}"
95       */
96      private String expressionList;
97  
98      /**
99       * The current Maven project against which expressions are evaluated.
100      * 
101      * @parameter default-value="${project}"
102      * @readonly
103      */
104     private Object project;
105 
106     /**
107      * The forked Maven project against which expressions are evaluated.
108      * 
109      * @parameter default-value="${executedProject}"
110      * @readonly
111      */
112     private Object executedProject;
113 
114     /**
115      * The merged user/global settings of the current build against which expressions are evaluated.
116      * 
117      * @parameter default-value="${settings}"
118      * @readonly
119      */
120     private Object settings;
121 
122     /**
123      * The session context of the current build against which expressions are evaluated.
124      * 
125      * @parameter default-value="${session}"
126      * @readonly
127      */
128     private Object session;
129 
130     /**
131      * The local repository of the current build against which expressions are evaluated.
132      * 
133      * @parameter default-value="${localRepository}"
134      * @readonly
135      */
136     private Object localRepository;
137 
138     /**
139      * Runs this mojo.
140      * 
141      * @throws MojoExecutionException If the output file could not be created.
142      * @throws MojoFailureException If the output file has not been set.
143      */
144     public void execute()
145         throws MojoExecutionException, MojoFailureException
146     {
147         if ( outputFile == null )
148         {
149             throw new MojoFailureException( "Path name for output file has not been specified" );
150         }
151 
152         /*
153          * NOTE: We don't want to test path translation here.
154          */
155         if ( !outputFile.isAbsolute() )
156         {
157             outputFile = new File( basedir, outputFile.getPath() ).getAbsoluteFile();
158         }
159 
160         getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file: " + outputFile );
161 
162         Properties expressionProperties = new Properties();
163 
164         if ( expressionList != null && expressionList.length() > 0 )
165         {
166             expressions = expressionList.split( "," );
167         }
168         if ( expressions != null && expressions.length > 0 )
169         {
170             Map contexts = new HashMap();
171             contexts.put( "project", project );
172             contexts.put( "executedProject", executedProject );
173             contexts.put( "pom", project );
174             contexts.put( "settings", settings );
175             contexts.put( "session", session );
176             contexts.put( "localRepository", localRepository );
177 
178             for ( int i = 0; i < expressions.length; i++ )
179             {
180                 Map values = ExpressionUtil.evaluate( expressions[i], contexts );
181                 for ( Iterator it = values.keySet().iterator(); it.hasNext(); )
182                 {
183                     Object key = it.next();
184                     Object value = values.get( key );
185                     PropertyUtil.store( expressionProperties, key.toString().replace( '/', '.' ), value );
186                 }
187             }
188         }
189 
190         try
191         {
192             PropertyUtil.write( expressionProperties, outputFile );
193         }
194         catch ( IOException e )
195         {
196             throw new MojoExecutionException( "Output file could not be created: " + outputFile, e );
197         }
198 
199         getLog().info( "[MAVEN-CORE-IT-LOG] Created output file: " + outputFile );
200     }
201 
202     public void setOutputFile( File outputFile )
203     {
204         this.outputFile = outputFile;
205     }
206 }
207