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.ArrayList;
019    import java.util.List;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.apache.maven.plugin.AbstractMojo;
023    import org.apache.maven.plugin.MojoExecutionException;
024    import org.apache.maven.project.MavenProject;
025    
026    /**
027     * Find a GAV specific property value using GroupId+ArtifactId+Version. The logic goes from most specific to least
028     * specific. If there is a value for the full GAV, that value is used. Otherwise, a value for GroupId + ArtifactId, or
029     * finally just GroupId. If no value is found, the defaultValue is used.
030     *
031     * A new project property with .gav appended is set if a value is found.
032     *
033     * @goal get-gav-property
034     */
035    public class GetGAVProperty extends AbstractMojo {
036    
037        /**
038         * @parameter default-value="${project}"
039         * @required
040         * @readonly
041         */
042        private MavenProject project;
043    
044        /**
045         * @parameter expression="${properties.groupId}" default-value="${project.groupId}"
046         */
047        private String groupId;
048    
049        /**
050         * @parameter expression="${properties.artifactId}" default-value="${project.artifactId}"
051         */
052        private String artifactId;
053    
054        /**
055         * @parameter expression="${properties.version}" default-value="${project.version}"
056         */
057        private String version;
058    
059        /**
060         * @parameter expression="${properties.property}"
061         */
062        private String property;
063    
064        /**
065         * @parameter expression="${properties.defaultValue}"
066         */
067        private String defaultValue;
068    
069        /**
070         * @parameter expression="${properties.suffix}" default-value="gav"
071         */
072        private String suffix;
073    
074        @Override
075        public void execute() throws MojoExecutionException {
076            List<String> keys = getKeys();
077            String value = getValue(project, keys);
078            if (StringUtils.isBlank(value)) {
079                getLog().info("No value for '" + property + "'");
080            } else {
081                String key = property + "." + suffix;
082                getLog().info("Setting " + key + "=" + value);
083                project.getProperties().setProperty(key, value);
084            }
085        }
086    
087        protected String getValue(MavenProject project, List<String> keys) {
088            for (String key : keys) {
089                String value = getProperty(key);
090                getLog().debug(key + "=" + value);
091                if (!StringUtils.isBlank(value)) {
092                    return value;
093                }
094            }
095            return defaultValue;
096        }
097    
098        protected List<String> getKeys() {
099            List<String> keys = new ArrayList<String>();
100            keys.add(property + "." + groupId + "." + artifactId + "." + version);
101            keys.add(property + "." + groupId + "." + artifactId);
102            keys.add(property + "." + groupId);
103            keys.add(property);
104            return keys;
105        }
106    
107        protected String getProperty(String key) {
108            String sys = System.getProperty(key);
109            String proj = project.getProperties().getProperty(key);
110            if (!StringUtils.isBlank(sys)) {
111                return sys;
112            } else {
113                return proj;
114            }
115    
116        }
117    
118        public MavenProject getProject() {
119            return project;
120        }
121    
122        public String getGroupId() {
123            return groupId;
124        }
125    
126        public void setGroupId(String groupId) {
127            this.groupId = groupId;
128        }
129    
130        public String getArtifactId() {
131            return artifactId;
132        }
133    
134        public void setArtifactId(String artifactId) {
135            this.artifactId = artifactId;
136        }
137    
138        public String getVersion() {
139            return version;
140        }
141    
142        public void setVersion(String version) {
143            this.version = version;
144        }
145    
146        public String getProperty() {
147            return property;
148        }
149    
150        public void setProperty(String property) {
151            this.property = property;
152        }
153    
154        public String getDefaultValue() {
155            return defaultValue;
156        }
157    
158        public void setDefaultValue(String defaultValue) {
159            this.defaultValue = defaultValue;
160        }
161    
162        public void setProject(MavenProject project) {
163            this.project = project;
164        }
165    
166        public String getSuffix() {
167            return suffix;
168        }
169    
170        public void setSuffix(String suffix) {
171            this.suffix = suffix;
172        }
173    
174    }