001    /**
002     * Copyright 2009-2013 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.Enumeration;
019    import java.util.Properties;
020    
021    import org.apache.maven.plugin.AbstractMojo;
022    import org.apache.maven.plugin.MojoExecutionException;
023    import org.apache.maven.plugin.MojoFailureException;
024    
025    /**
026     * Sets system properties.
027     *
028     * @author <a href="mailto:markh@apache.org">Mark Hobson</a>
029     * @version $Id: SetSystemPropertiesMojo.java 9751 2009-05-20 14:53:00Z mark $
030     * @goal set-system-properties
031     * @phase initialize
032     */
033    public class SetSystemPropertiesMojo extends AbstractMojo {
034        // fields -----------------------------------------------------------------
035    
036        /**
037         * The system properties to set.
038         *
039         * @parameter
040         * @required
041         */
042        private Properties properties;
043    
044        // Mojo methods -----------------------------------------------------------
045    
046        /**
047         * {@inheritDoc}
048         */
049        @Override
050        public void execute() throws MojoExecutionException, MojoFailureException {
051            if (properties.isEmpty()) {
052                getLog().debug("No system properties found");
053    
054                return;
055            }
056    
057            getLog().debug("Setting system properties:");
058    
059            for (Enumeration<?> propertyNames = properties.propertyNames(); propertyNames.hasMoreElements();) {
060                String propertyName = propertyNames.nextElement().toString();
061                String propertyValue = properties.getProperty(propertyName);
062    
063                getLog().debug("- " + propertyName + " = " + propertyValue);
064    
065                System.setProperty(propertyName, propertyValue);
066            }
067    
068            int count = properties.size();
069    
070            getLog().info("Set " + count + " system " + (count > 1 ? "properties" : "property"));
071        }
072    }