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   * Parse version number properties into [major].[minor].[incremental].[qualifier]. The version parsing logic is crudely
27   * simple. It splits the version string into tokens using both "." and "-" as delimiters. It assumes the first token is
28   * "major", the second token is "minor" the third token is "incremental" and the fourth token is "qualifier".
29   *
30   * @goal parse-version-properties
31   */
32  public class ParseVersionPropertiesMojo extends AbstractMojo {
33  
34      /**
35       * @parameter default-value="${project}"
36       * @required
37       * @readonly
38       */
39      private MavenProject project;
40  
41      /**
42       * The list of properties containing version numbers to parse
43       *
44       * @parameter
45       * @required
46       */
47      private String[] properties;
48  
49      @Override
50      public void execute() throws MojoExecutionException {
51          Properties props = project.getProperties();
52          for (String key : properties) {
53              String value = getProperty(key);
54              if (StringUtils.isBlank(value)) {
55                  continue;
56              }
57              Version version = parseVersion(value);
58              setProjectProperty(key, "major", version.getMajor(), props);
59              setProjectProperty(key, "minor", version.getMinor(), props);
60              setProjectProperty(key, "incremental", version.getIncremental(), props);
61              setProjectProperty(key, "qualifier", version.getQualifier(), props);
62          }
63      }
64  
65      protected void setProjectProperty(String key, String suffix, String value, Properties props) {
66          if (StringUtils.isBlank(value)) {
67              return;
68          }
69          props.setProperty(key + "." + suffix, value);
70          getLog().info("Setting " + key + "." + suffix + "=" + value);
71      }
72  
73      protected Version parseVersion(String s) {
74          Version version = new Version();
75          String[] tokens = StringUtils.split(s, ".-");
76          if (tokens.length > 0) {
77              version.setMajor(tokens[0]);
78          }
79          if (tokens.length > 1) {
80              version.setMinor(tokens[1]);
81          }
82          if (tokens.length > 2) {
83              version.setIncremental(tokens[2]);
84          }
85          if (tokens.length > 3) {
86              version.setQualifier(tokens[3]);
87          }
88          return version;
89      }
90  
91      protected String getProperty(String key) {
92          String sys = System.getProperty(key);
93          String proj = project.getProperties().getProperty(key);
94          if (!StringUtils.isBlank(sys)) {
95              return sys;
96          } else {
97              return proj;
98          }
99  
100     }
101 
102     public String[] getProperties() {
103         return properties;
104     }
105 
106     public void setProperties(String[] properties) {
107         this.properties = properties;
108     }
109 }