001    /**
002     * Copyright 2009-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.codehaus.mojo.properties;
017    
018    import java.util.ArrayList;
019    import java.util.Collections;
020    import java.util.List;
021    import java.util.Map;
022    import java.util.Properties;
023    
024    import org.apache.maven.plugin.MojoExecutionException;
025    import org.apache.maven.plugin.MojoFailureException;
026    import org.springframework.util.PropertyPlaceholderHelper;
027    
028    /**
029     * Write project properties to a file.
030     *
031     * @author Jeff Caddel
032     *
033     * @goal write-project-properties
034     */
035    public class WriteProjectProperties extends AbstractWritePropertiesMojo {
036    
037            /**
038             * If true, the plugin will include system properties when writing the properties file. System properties override both environment
039             * variables and project properties.
040             *
041             * @parameter default-value="false" expression="${properties.includeSystemProperties}"
042             */
043            private boolean includeSystemProperties;
044    
045            /**
046             * If true, the plugin will include environment variables when writing the properties file. Environment variables are prefixed with
047             * "env". Environment variables override project properties.
048             *
049             * @parameter default-value="false" expression="${properties.includeEnvironmentVariables}"
050             */
051            private boolean includeEnvironmentVariables;
052    
053            /**
054             * Comma separated set of properties to exclude when writing the properties file
055             *
056             * @parameter expression="${properties.exclude}"
057             */
058            private String exclude;
059    
060            /**
061             * Comma separated set of properties to write to the properties file. If provided, only the properties matching those supplied here will
062             * be written to the properties file.
063             *
064             * @parameter expression="${properties.include}"
065             */
066            private String include;
067    
068            /**
069             * If true placeholders are resolved before writing properties to the file
070             *
071             * @parameter expression="${properties.resolvePlaceholders}"
072             */
073            private boolean resolvePlaceholders;
074    
075            /**
076             * If true, include/exclude is used to trim the properties list before resolving placeholders
077             *
078             * @parameter expression="${properties.trimBeforeResolve}" default-value="true"
079             */
080            private boolean trimBeforeResolve;
081    
082            @Override
083            public void execute() throws MojoExecutionException, MojoFailureException {
084                    Properties properties = new Properties();
085                    // Add project properties
086                    properties.putAll(project.getProperties());
087                    if (includeEnvironmentVariables) {
088                            // Add environment variables, overriding any existing properties with the same key
089                            properties.putAll(getEnvironmentVariables());
090                    }
091                    if (includeSystemProperties) {
092                            // Add system properties, overriding any existing properties with the same key
093                            properties.putAll(System.getProperties());
094                    }
095    
096                    if (trimBeforeResolve) {
097                            // Remove properties as appropriate
098                            trim(properties, exclude, include);
099    
100                    }
101    
102                    // Resolve placeholders
103                    if (resolvePlaceholders) {
104                            properties = getResolvedProperties(properties);
105                    }
106    
107                    // Remove properties as appropriate
108                    trim(properties, exclude, include);
109    
110                    getLog().info("Creating " + outputFile);
111    
112                    // Save the properties to a file
113                    writeProperties(this.outputFile, properties, this.outputStyle, this.prefix);
114            }
115    
116            protected Properties getResolvedProperties(Properties props) {
117                    PropertyPlaceholderHelper pph = new PropertyPlaceholderHelper("${", "}");
118                    List<String> keys = new ArrayList<String>(props.stringPropertyNames());
119                    Collections.sort(keys);
120                    Properties newProps = new Properties();
121                    for (String key : keys) {
122                            String originalValue = props.getProperty(key);
123                            String resolvedValue = pph.replacePlaceholders(originalValue, props);
124                            newProps.setProperty(key, resolvedValue);
125                    }
126                    return newProps;
127    
128            }
129    
130            protected static Properties getEnvironmentVariables() {
131                    String prefix = "env";
132                    Map<String, String> map = System.getenv();
133                    Properties props = new Properties();
134                    for (String key : map.keySet()) {
135                            String newKey = prefix + "." + key;
136                            String value = map.get(key);
137                            props.setProperty(newKey, value);
138                    }
139                    return props;
140            }
141    
142            protected void trim(Properties properties, String excludeCSV, String includeCSV) {
143                    List<String> excludes = ReadPropertiesMojo.getListFromCSV(excludeCSV);
144                    List<String> includes = ReadPropertiesMojo.getListFromCSV(includeCSV);
145                    List<String> keys = new ArrayList<String>(properties.stringPropertyNames());
146                    Collections.sort(keys);
147                    for (String key : keys) {
148                            boolean include = include(key, includes, excludes);
149                            if (!include) {
150                                    properties.remove(key);
151                            }
152                    }
153            }
154    
155            public boolean include(String value, List<String> includes, List<String> excludes) {
156                    if (excludes.contains(value)) {
157                            return false;
158                    } else {
159                            return includes.size() == 0 || includes.contains(value);
160                    }
161            }
162    
163            public boolean isIncludeSystemProperties() {
164                    return includeSystemProperties;
165            }
166    
167            public void setIncludeSystemProperties(boolean includeSystemProperties) {
168                    this.includeSystemProperties = includeSystemProperties;
169            }
170    
171            public boolean isIncludeEnvironmentVariables() {
172                    return includeEnvironmentVariables;
173            }
174    
175            public void setIncludeEnvironmentVariables(boolean includeEnvironmentVariables) {
176                    this.includeEnvironmentVariables = includeEnvironmentVariables;
177            }
178    
179            public String getExclude() {
180                    return exclude;
181            }
182    
183            public void setExclude(String exclude) {
184                    this.exclude = exclude;
185            }
186    
187            public String getInclude() {
188                    return include;
189            }
190    
191            public void setInclude(String include) {
192                    this.include = include;
193            }
194    
195            public boolean isResolvePlaceholders() {
196                    return resolvePlaceholders;
197            }
198    
199            public void setResolvePlaceholders(boolean resolvePlaceholders) {
200                    this.resolvePlaceholders = resolvePlaceholders;
201            }
202    
203            public boolean isTrimBeforeResolve() {
204                    return trimBeforeResolve;
205            }
206    
207            public void setTrimBeforeResolve(boolean trimBeforeResolve) {
208                    this.trimBeforeResolve = trimBeforeResolve;
209            }
210    }