001    /**
002     * Copyright 2009-2013 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] and [trimmed]. The version parsing
027     * logic is crudely simple. It splits the version string into tokens using both "." and "-" as delimiters. It assumes
028     * the first token is "major", the second token is "minor" the third token is "incremental" and any tokens after that
029     * are "qualifier". "SNAPSHOT" is always omitted from qualifier.
030     *
031     * [trimmed] is the full version minus "-SNAPSHOT"
032     *
033     * @goal parse-version-properties
034     */
035    public class ParseVersionPropertiesMojo extends AbstractMojo {
036        public static final String MAVEN_SNAPSHOT_TOKEN = "SNAPSHOT";
037    
038        /**
039         * @parameter default-value="${project}"
040         * @required
041         * @readonly
042         */
043        private MavenProject project;
044    
045        /**
046         * The list of properties containing version numbers to parse
047         *
048         * @parameter
049         * @required
050         */
051        private String[] properties;
052    
053        /**
054         * If true, the plugin will emit no logging messages
055         *
056         * @parameter expression="${properties.silent}" default-value="false"
057         * @required
058         */
059        private boolean silent;
060    
061        @Override
062        public void execute() throws MojoExecutionException {
063            Properties props = project.getProperties();
064            for (String key : properties) {
065                String value = getProperty(key);
066                if (StringUtils.isBlank(value)) {
067                    continue;
068                }
069                Version version = parseVersion(value);
070                setProjectProperty(key, "major", version.getMajor(), props);
071                setProjectProperty(key, "minor", version.getMinor(), props);
072                setProjectProperty(key, "incremental", version.getIncremental(), props);
073                setProjectProperty(key, "qualifier", version.getQualifier(), props);
074                setProjectProperty(key, "trimmed", trimSnapshot(value), props);
075            }
076        }
077    
078        protected String trimSnapshot(String version) {
079            if (version.toUpperCase().endsWith("-" + MAVEN_SNAPSHOT_TOKEN)) {
080                int length = MAVEN_SNAPSHOT_TOKEN.length() + 1;
081                return StringUtils.left(version, version.length() - length);
082            } else {
083                return version;
084            }
085        }
086    
087        protected void setProjectProperty(String key, String suffix, String value, Properties props) {
088            if (StringUtils.isBlank(value)) {
089                return;
090            }
091            props.setProperty(key + "." + suffix, value);
092            if (!silent) {
093                getLog().info("Setting " + key + "." + suffix + "=" + value);
094            }
095        }
096    
097        protected Version parseVersion(String s) {
098            boolean snapshot = s.toUpperCase().endsWith("-" + MAVEN_SNAPSHOT_TOKEN);
099            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    }