View Javadoc

1   /**
2    * Copyright 2009-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.mojo.properties;
17  
18  import java.util.Properties;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.apache.maven.plugin.AbstractMojo;
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.project.MavenProject;
24  
25  /**
26   * Translate the indicated properties into classpath friendly form.
27   *
28   * eg Transform <code>org.kuali.rice</code> into <code>org/kuali/rice</code>
29   *
30   * A new project property with ".path" added as a suffix gets set with the transformed value
31   *
32   * @goal translate-properties
33   */
34  public class TranslatePropertiesMojo extends AbstractMojo {
35  
36      /**
37       * @parameter default-value="${project}"
38       * @required
39       * @readonly
40       */
41      private MavenProject project;
42  
43      /**
44       * The list of properties to translate
45       *
46       * @parameter
47       * @required
48       */
49      private String[] properties;
50  
51      /**
52       * The suffix appended to the existing property name where the translated property is stored
53       *
54       * @parameter expression="${properties.suffix}" default-value=".path"
55       * @required
56       */
57      private String suffix;
58  
59      protected String getProperty(String key) {
60          String sys = System.getProperty(key);
61          String proj = project.getProperties().getProperty(key);
62          if (!StringUtils.isBlank(sys)) {
63              return sys;
64          } else {
65              return proj;
66          }
67  
68      }
69  
70      @Override
71      public void execute() throws MojoExecutionException {
72          Properties props = project.getProperties();
73          for (String key : properties) {
74              String value = getProperty(key);
75              if (StringUtils.isBlank(value)) {
76                  continue;
77              } else {
78                  String newValue = value.replace(".", "/");
79                  String newKey = key + suffix;
80                  getLog().info("Setting " + newKey + "=" + newValue);
81                  props.setProperty(newKey, newValue);
82              }
83          }
84      }
85  
86      public String[] getProperties() {
87          return properties;
88      }
89  
90      public void setProperties(String[] properties) {
91          this.properties = properties;
92      }
93  }