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.Properties;
019    
020    import org.apache.commons.lang.StringUtils;
021    import org.apache.maven.plugin.AbstractMojo;
022    import org.apache.maven.plugin.MojoExecutionException;
023    import org.apache.maven.project.MavenProject;
024    
025    /**
026     * Translate the indicated properties into classpath friendly form.
027     *
028     * eg Transform <code>org.kuali.rice</code> into <code>org/kuali/rice</code>
029     *
030     * A new project property with ".path" added as a suffix gets set with the transformed value
031     *
032     * @goal translate-properties
033     */
034    public class TranslatePropertiesMojo extends AbstractMojo {
035    
036        /**
037         * @parameter default-value="${project}"
038         * @required
039         * @readonly
040         */
041        private MavenProject project;
042    
043        /**
044         * The list of properties to translate
045         *
046         * @parameter
047         * @required
048         */
049        private String[] properties;
050    
051        /**
052         * The suffix appended to the existing property name where the translated property is stored
053         *
054         * @parameter expression="${properties.suffix}" default-value=".path"
055         * @required
056         */
057        private String suffix;
058    
059        protected String getProperty(String key) {
060            String sys = System.getProperty(key);
061            String proj = project.getProperties().getProperty(key);
062            if (!StringUtils.isBlank(sys)) {
063                return sys;
064            } else {
065                return proj;
066            }
067    
068        }
069    
070        @Override
071        public void execute() throws MojoExecutionException {
072            Properties props = project.getProperties();
073            for (String key : properties) {
074                String value = getProperty(key);
075                if (StringUtils.isBlank(value)) {
076                    continue;
077                } else {
078                    String newValue = value.replace(".", "/");
079                    String newKey = key + suffix;
080                    getLog().info("Setting " + newKey + "=" + newValue);
081                    props.setProperty(newKey, newValue);
082                }
083            }
084        }
085    
086        public String[] getProperties() {
087            return properties;
088        }
089    
090        public void setProperties(String[] properties) {
091            this.properties = properties;
092        }
093    }