View Javadoc

1   package org.codehaus.mojo.properties;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file 
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, 
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
17   * KIND, either express or implied.  See the License for the 
18   * specific language governing permissions and limitations 
19   * under the License.
20   */
21  
22  import java.util.Enumeration;
23  import java.util.Properties;
24  
25  import org.apache.maven.plugin.AbstractMojo;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.MojoFailureException;
28  
29  /**
30   * Sets system properties.
31   * 
32   * @author <a href="mailto:markh@apache.org">Mark Hobson</a>
33   * @version $Id: SetSystemPropertiesMojo.java 9751 2009-05-20 14:53:00Z mark $
34   * @goal set-system-properties
35   * @phase initialize
36   */
37  public class SetSystemPropertiesMojo
38      extends AbstractMojo
39  {
40      // fields -----------------------------------------------------------------
41  
42      /**
43       * The system properties to set.
44       * 
45       * @parameter
46       * @required
47       */
48      private Properties properties;
49  
50      // Mojo methods -----------------------------------------------------------
51  
52      /**
53       * {@inheritDoc}
54       */
55      public void execute()
56          throws MojoExecutionException, MojoFailureException
57      {
58          if ( properties.isEmpty() )
59          {
60              getLog().debug( "No system properties found" );
61  
62              return;
63          }
64  
65          getLog().debug( "Setting system properties:" );
66  
67          for ( Enumeration propertyNames = properties.propertyNames(); propertyNames.hasMoreElements(); )
68          {
69              String propertyName = propertyNames.nextElement().toString();
70              String propertyValue = properties.getProperty( propertyName );
71  
72              getLog().debug( "- " + propertyName + " = " + propertyValue );
73  
74              System.setProperty( propertyName, propertyValue );
75          }
76  
77          int count = properties.size();
78  
79          getLog().info( "Set " + count + " system " + ( count > 1 ? "properties" : "property" ) );
80      }
81  }