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] and [trimmed]. The version parsing
27   * logic is crudely simple. It splits the version string into tokens using both "." and "-" as delimiters. It assumes
28   * the first token is "major", the second token is "minor" the third token is "incremental" and any tokens after that
29   * are "qualifier". "SNAPSHOT" is always omitted from qualifier.
30   *
31   * [trimmed] is the full version minus "-SNAPSHOT"
32   *
33   * @goal parse-version-properties
34   */
35  public class ParseVersionPropertiesMojo extends AbstractMojo {
36      public static final String MAVEN_SNAPSHOT_TOKEN = "SNAPSHOT";
37  
38      /**
39       * @parameter default-value="${project}"
40       * @required
41       * @readonly
42       */
43      private MavenProject project;
44  
45      /**
46       * The list of properties containing version numbers to parse
47       *
48       * @parameter
49       * @required
50       */
51      private String[] properties;
52  
53      /**
54       * If true, the plugin will emit no logging messages
55       *
56       * @parameter expression="${properties.silent}" default-value="false"
57       * @required
58       */
59      private boolean silent;
60  
61      @Override
62      public void execute() throws MojoExecutionException {
63          Properties props = project.getProperties();
64          for (String key : properties) {
65              String value = getProperty(key);
66              if (StringUtils.isBlank(value)) {
67                  continue;
68              }
69              Version version = parseVersion(value);
70              setProjectProperty(key, "major", version.getMajor(), props);
71              setProjectProperty(key, "minor", version.getMinor(), props);
72              setProjectProperty(key, "incremental", version.getIncremental(), props);
73              setProjectProperty(key, "qualifier", version.getQualifier(), props);
74              setProjectProperty(key, "trimmed", trimSnapshot(value), props);
75          }
76      }
77  
78      protected String trimSnapshot(String version) {
79          if (version.toUpperCase().endsWith("-" + MAVEN_SNAPSHOT_TOKEN)) {
80              int length = MAVEN_SNAPSHOT_TOKEN.length() + 1;
81              return StringUtils.left(version, version.length() - length);
82          } else {
83              return version;
84          }
85      }
86  
87      protected void setProjectProperty(String key, String suffix, String value, Properties props) {
88          if (StringUtils.isBlank(value)) {
89              return;
90          }
91          props.setProperty(key + "." + suffix, value);
92          if (!silent) {
93              getLog().info("Setting " + key + "." + suffix + "=" + value);
94          }
95      }
96  
97      protected Version parseVersion(String s) {
98          boolean snapshot = s.toUpperCase().endsWith("-" + MAVEN_SNAPSHOT_TOKEN);
99          Version version = new Version();
100         version.setSnapshot(snapshot);
101         String[] tokens = StringUtils.split(s, ".-");
102         if (tokens.length > 0) {
103             version.setMajor(tokens[0]);
104         }
105         if (tokens.length > 1) {
106             version.setMinor(tokens[1]);
107         }
108         if (tokens.length > 2) {
109             version.setIncremental(tokens[2]);
110         }
111         String qualifier = getQualifier(tokens);
112         version.setQualifier(qualifier);
113         return version;
114     }
115 
116     protected String getQualifier(String[] tokens) {
117         if (tokens.length <= 3) {
118             return null;
119         }
120         StringBuilder sb = new StringBuilder();
121         for (int i = 3; i < tokens.length; i++) {
122             if (tokens[i].toUpperCase().equals(MAVEN_SNAPSHOT_TOKEN)) {
123                 break;
124             }
125             if (i != 3) {
126                 sb.append("-");
127             }
128             sb.append(tokens[i]);
129         }
130         return sb.toString();
131     }
132 
133     protected String getProperty(String key) {
134         String sys = System.getProperty(key);
135         String proj = project.getProperties().getProperty(key);
136         if (!StringUtils.isBlank(sys)) {
137             return sys;
138         } else {
139             return proj;
140         }
141 
142     }
143 
144     public String[] getProperties() {
145         return properties;
146     }
147 
148     public void setProperties(String[] properties) {
149         this.properties = properties;
150     }
151 
152     public boolean isSilent() {
153         return silent;
154     }
155 
156     public void setSilent(boolean silent) {
157         this.silent = silent;
158     }
159 }