View Javadoc

1   /**
2    * Copyright 2009-2012 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       * @parameter
45       * @required
46       */
47      private String[] properties;
48  
49      /**
50       * @parameter expression="${properties.suffix}" default-value=".path"
51       * @required
52       */
53      private String suffix;
54  
55      protected String getProperty(String key) {
56          String sys = System.getProperty(key);
57          String proj = project.getProperties().getProperty(key);
58          if (!StringUtils.isBlank(sys)) {
59              return sys;
60          } else {
61              return proj;
62          }
63  
64      }
65  
66      @Override
67      public void execute() throws MojoExecutionException {
68          Properties props = project.getProperties();
69          for (String key : properties) {
70              String value = getProperty(key);
71              if (StringUtils.isBlank(value)) {
72                  continue;
73              } else {
74                  String newValue = value.replace(".", "/");
75                  String newKey = key + suffix;
76                  getLog().info("Setting " + newKey + "=" + newValue);
77                  props.setProperty(newKey, newValue);
78              }
79          }
80      }
81  
82      public String[] getProperties() {
83          return properties;
84      }
85  
86      public void setProperties(String[] properties) {
87          this.properties = properties;
88      }
89  }