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         * @parameter
045         * @required
046         */
047        private String[] properties;
048    
049        /**
050         * @parameter expression="${properties.suffix}" default-value=".path"
051         * @required
052         */
053        private String suffix;
054    
055        protected String getProperty(String key) {
056            String sys = System.getProperty(key);
057            String proj = project.getProperties().getProperty(key);
058            if (!StringUtils.isBlank(sys)) {
059                return sys;
060            } else {
061                return proj;
062            }
063    
064        }
065    
066        @Override
067        public void execute() throws MojoExecutionException {
068            Properties props = project.getProperties();
069            for (String key : properties) {
070                String value = getProperty(key);
071                if (StringUtils.isBlank(value)) {
072                    continue;
073                } else {
074                    String newValue = value.replace(".", "/");
075                    String newKey = key + suffix;
076                    getLog().info("Setting " + newKey + "=" + newValue);
077                    props.setProperty(newKey, newValue);
078                }
079            }
080        }
081    
082        public String[] getProperties() {
083            return properties;
084        }
085    
086        public void setProperties(String[] properties) {
087            this.properties = properties;
088        }
089    }