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