001    package org.kuali.maven.plugin;
002    
003    import java.util.Properties;
004    
005    import org.apache.maven.plugin.AbstractMojo;
006    import org.apache.maven.plugin.MojoExecutionException;
007    import org.apache.maven.plugin.MojoFailureException;
008    import org.apache.maven.project.MavenProject;
009    import org.codehaus.plexus.util.StringUtils;
010    
011    /**
012     * Stores the major version of the project as a property. The mojo assumes 3 digit versions eg "1.0.1" and counts "1.0"
013     * as the major version
014     *
015     * @goal get-major-version
016     */
017    public class GetMajorVersionMojo extends AbstractMojo {
018    
019        /**
020         * The Maven project this plugin runs in.
021         *
022         * @parameter expression="${project}"
023         * @required
024         * @readonly
025         */
026        private MavenProject project;
027    
028        /**
029         * The property where the major version will be stored
030         *
031         * @parameter expression="${version.majorVersion}" default-value="majorVersion"
032         * @required
033         */
034        private String majorVersionProperty;
035    
036        @Override
037        public void execute() throws MojoExecutionException, MojoFailureException {
038            Properties properties = project.getProperties();
039            if (properties.getProperty(majorVersionProperty) != null) {
040                getLog().debug(majorVersionProperty + " already set");
041                return;
042            }
043            String majorVersion = getMajorVersion(project.getVersion());
044            properties.setProperty(majorVersionProperty, majorVersion);
045            getLog().debug("Major Version: " + majorVersion);
046        }
047    
048        protected String getMajorVersion(String version) {
049            if (StringUtils.isEmpty(version)) {
050                return null;
051            }
052            int pos = getPos(version);
053            if (pos == -1) {
054                return version;
055            } else {
056                return version.substring(0, pos);
057            }
058        }
059    
060        protected int getPos(String version) {
061            int pos1 = version.indexOf(".");
062            int pos2 = version.indexOf("-");
063            // No dot or dash
064            if (pos1 == pos2 && pos2 == -1) {
065                return -1;
066            }
067            // Dash but no dot
068            if (pos1 == -1 && pos2 != -1) {
069                return pos1;
070            }
071            // Dot but no dash
072            if (pos1 != -1 && pos2 == -1) {
073                return pos2;
074            }
075    
076            // Both a dot and a dash, use the first one
077            int pos = Math.min(pos1, pos2);
078    
079            // Attempt to peek ahead one character after the dot/dash
080            int index = pos + 1;
081    
082            // If we go past the end of the string, forget it
083            if (index >= version.length()) {
084                return pos;
085            }
086    
087            // Is that character an integer?
088            char c = version.charAt(index);
089    
090            if (isInteger(c)) {
091                // If so, include it
092                return index + 1;
093            } else {
094                // If not return the original
095                return pos;
096            }
097    
098        }
099    
100        protected boolean isInteger(char c) {
101            switch (c) {
102            case '0':
103            case '1':
104            case '2':
105            case '3':
106            case '4':
107            case '5':
108            case '6':
109            case '7':
110            case '8':
111            case '9':
112                return true;
113            default:
114                return false;
115            }
116        }
117    
118        public MavenProject getProject() {
119            return project;
120        }
121    
122        public void setProject(MavenProject project) {
123            this.project = project;
124        }
125    }