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    /*
019     * Licensed to the Apache Software Foundation (ASF) under one
020     * or more contributor license agreements.  See the NOTICE file
021     * distributed with this work for additional information
022     * regarding copyright ownership.  The ASF licenses this file
023     * to you under the Apache License, Version 2.0 (the
024     * "License"); you may not use this file except in compliance
025     * with the License.  You may obtain a copy of the License at
026     *
027     *   http://www.apache.org/licenses/LICENSE-2.0
028     *
029     * Unless required by applicable law or agreed to in writing,
030     * software distributed under the License is distributed on an
031     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
032     * KIND, either express or implied.  See the License for the
033     * specific language governing permissions and limitations
034     * under the License.
035     */
036    
037    import java.util.Enumeration;
038    import java.util.Properties;
039    
040    import org.apache.maven.plugin.AbstractMojo;
041    import org.apache.maven.plugin.MojoExecutionException;
042    import org.apache.maven.plugin.MojoFailureException;
043    
044    /**
045     * Sets system properties.
046     *
047     * @author <a href="mailto:markh@apache.org">Mark Hobson</a>
048     * @version $Id: SetSystemPropertiesMojo.java 9751 2009-05-20 14:53:00Z mark $
049     * @goal set-system-properties
050     * @phase initialize
051     */
052    public class SetSystemPropertiesMojo extends AbstractMojo {
053        // fields -----------------------------------------------------------------
054    
055        /**
056         * The system properties to set.
057         *
058         * @parameter
059         * @required
060         */
061        private Properties properties;
062    
063        // Mojo methods -----------------------------------------------------------
064    
065        /**
066         * {@inheritDoc}
067         */
068        @Override
069        public void execute() throws MojoExecutionException, MojoFailureException {
070            if (properties.isEmpty()) {
071                getLog().debug("No system properties found");
072    
073                return;
074            }
075    
076            getLog().debug("Setting system properties:");
077    
078            for (Enumeration<?> propertyNames = properties.propertyNames(); propertyNames.hasMoreElements();) {
079                String propertyName = propertyNames.nextElement().toString();
080                String propertyValue = properties.getProperty(propertyName);
081    
082                getLog().debug("- " + propertyName + " = " + propertyValue);
083    
084                System.setProperty(propertyName, propertyValue);
085            }
086    
087            int count = properties.size();
088    
089            getLog().info("Set " + count + " system " + (count > 1 ? "properties" : "property"));
090        }
091    }