001 /**
002 * Copyright 2009-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.codehaus.mojo.properties;
017
018 import java.util.Properties;
019
020 import org.apache.commons.lang.StringUtils;
021 import org.apache.maven.plugin.AbstractMojo;
022 import org.apache.maven.plugin.MojoExecutionException;
023 import org.apache.maven.project.MavenProject;
024
025 /**
026 * Parse version number properties into [major].[minor].[incremental].[qualifier]. The version parsing logic is crudely
027 * simple. It splits the version string into tokens using both "." and "-" as delimiters. It assumes the first token is
028 * "major", the second token is "minor" the third token is "incremental" and the fourth token is "qualifier".
029 *
030 * @goal parse-version-properties
031 */
032 public class ParseVersionPropertiesMojo extends AbstractMojo {
033
034 /**
035 * @parameter default-value="${project}"
036 * @required
037 * @readonly
038 */
039 private MavenProject project;
040
041 /**
042 * The list of properties containing version numbers to parse
043 *
044 * @parameter
045 * @required
046 */
047 private String[] properties;
048
049 @Override
050 public void execute() throws MojoExecutionException {
051 Properties props = project.getProperties();
052 for (String key : properties) {
053 String value = getProperty(key);
054 if (StringUtils.isBlank(value)) {
055 continue;
056 }
057 Version version = parseVersion(value);
058 setProjectProperty(key, "major", version.getMajor(), props);
059 setProjectProperty(key, "minor", version.getMinor(), props);
060 setProjectProperty(key, "incremental", version.getIncremental(), props);
061 setProjectProperty(key, "qualifier", version.getQualifier(), props);
062 }
063 }
064
065 protected void setProjectProperty(String key, String suffix, String value, Properties props) {
066 if (StringUtils.isBlank(value)) {
067 return;
068 }
069 props.setProperty(key + "." + suffix, value);
070 getLog().info("Setting " + key + "." + suffix + "=" + value);
071 }
072
073 protected Version parseVersion(String s) {
074 Version version = new Version();
075 String[] tokens = StringUtils.split(s, ".-");
076 if (tokens.length > 0) {
077 version.setMajor(tokens[0]);
078 }
079 if (tokens.length > 1) {
080 version.setMinor(tokens[1]);
081 }
082 if (tokens.length > 2) {
083 version.setIncremental(tokens[2]);
084 }
085 if (tokens.length > 3) {
086 version.setQualifier(tokens[3]);
087 }
088 return version;
089 }
090
091 protected String getProperty(String key) {
092 String sys = System.getProperty(key);
093 String proj = project.getProperties().getProperty(key);
094 if (!StringUtils.isBlank(sys)) {
095 return sys;
096 } else {
097 return proj;
098 }
099
100 }
101
102 public String[] getProperties() {
103 return properties;
104 }
105
106 public void setProperties(String[] properties) {
107 this.properties = properties;
108 }
109 }