View Javadoc
1   /**
2    * Copyright 2004-2014 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.kuali.common.maven.spring;
17  
18  import static java.lang.String.format;
19  import static org.apache.commons.lang3.StringUtils.isBlank;
20  import static org.kuali.common.core.scm.maven.MavenScmUtils.MAVEN_SCM_CONNECTION_KEY;
21  import static org.kuali.common.core.scm.maven.MavenScmUtils.MAVEN_SCM_DEVELOPER_CONNECTION_KEY;
22  import static org.kuali.common.core.scm.maven.MavenScmUtils.MAVEN_SCM_REVISION_KEY;
23  import static org.kuali.common.core.scm.maven.MavenScmUtils.MAVEN_SCM_TAG_DEFAULT_VALUE;
24  import static org.kuali.common.core.scm.maven.MavenScmUtils.MAVEN_SCM_TAG_KEY;
25  import static org.kuali.common.core.scm.maven.MavenScmUtils.MAVEN_SCM_URL_KEY;
26  import static org.kuali.common.core.scm.maven.MavenScmUtils.getMavenUrl;
27  import static org.kuali.common.util.base.Exceptions.illegalState;
28  import static org.kuali.common.util.base.Precondition.checkNotNull;
29  import static org.kuali.common.util.log.Loggers.newLogger;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  import java.util.Properties;
34  
35  import org.apache.maven.model.Scm;
36  import org.apache.maven.project.MavenProject;
37  import org.apache.maven.settings.Settings;
38  import org.kuali.common.core.scm.api.ScmContext;
39  import org.kuali.common.core.scm.api.ScmType;
40  import org.kuali.common.core.scm.maven.MavenScmUtils;
41  import org.kuali.common.util.CollectionUtils;
42  import org.kuali.common.util.LocationUtils;
43  import org.kuali.common.util.maven.RepositoryUtils;
44  import org.kuali.common.util.maven.model.Dependency;
45  import org.kuali.common.util.nullify.NullUtils;
46  import org.slf4j.Logger;
47  
48  import com.google.common.base.Optional;
49  
50  /**
51   * Maven related utilities that are aware of the Maven model objects eg MavenProject
52   */
53  public class MavenAwareUtils {
54  
55  	private static final Logger logger = newLogger();
56  
57  	public static ScmType getScmType(Scm scm) {
58  		if (!isBlank(scm.getDeveloperConnection())) {
59  			return MavenScmUtils.getScmType(scm.getDeveloperConnection());
60  		} else if (!isBlank(scm.getConnection())) {
61  			return MavenScmUtils.getScmType(scm.getConnection());
62  		} else {
63  			throw illegalState("unable to determine scm type. both connection and developerConnection are blank");
64  		}
65  	}
66  
67  	public static void updateMavenScm(MavenProject project, ScmContext context) {
68  		checkNotNull(project, "project");
69  		checkNotNull(context, "context");
70  		Scm scm = checkNotNull(project.getScm(), "scm");
71  		ScmType type = context.getType();
72  		scm.setDeveloperConnection(checkConverted(MAVEN_SCM_DEVELOPER_CONNECTION_KEY, type, scm.getDeveloperConnection(), context.getPushUrl()));
73  		scm.setConnection(checkConverted(MAVEN_SCM_CONNECTION_KEY, type, scm.getConnection(), context.getFetchUrl()));
74  		scm.setUrl(checkEquals(MAVEN_SCM_URL_KEY, type, scm.getUrl(), context.getBrowseUrl()));
75  		scm.setTag(checkEquals(MAVEN_SCM_TAG_KEY, scm.getTag(), context.getLabel()));
76  		String revision = context.getRevision().isPresent() ? context.getRevision().get() : NullUtils.NONE;
77  		project.getProperties().setProperty(MAVEN_SCM_REVISION_KEY, revision);
78  	}
79  
80  	private static String checkEquals(String label, String oldTag, Optional<String> newTag) {
81  		String newTagValue = newTag.isPresent() ? newTag.get() : NullUtils.NONE;
82  		if (!newTagValue.equals(oldTag)) {
83  			logUpdate(label, oldTag, newTagValue);
84  		}
85  		return newTagValue;
86  	}
87  
88  	private static String checkEquals(String label, ScmType type, String oldUrl, String newUrl) {
89  		if (!newUrl.equals(oldUrl)) {
90  			logUpdate(label, oldUrl, newUrl);
91  		}
92  		return newUrl;
93  	}
94  
95  	private static String checkConverted(String label, ScmType type, String oldMavenUrl, String newRawUrl) {
96  		String newMavenUrl = getMavenUrl(type, newRawUrl);
97  		if (!newMavenUrl.equals(oldMavenUrl)) {
98  			logUpdate(label, oldMavenUrl, newMavenUrl);
99  		}
100 		return newMavenUrl;
101 	}
102 
103 	private static void logUpdate(String label, String before, String after) {
104 		logger.debug(format("updating -> %s", label));
105 		logger.debug(format("before   -> %s", before));
106 		logger.debug(format("after    -> %s", after));
107 	}
108 
109 	public static Properties getInternalProperties(MavenProject project, Settings settings) {
110 		Properties properties = new Properties();
111 		nullSafeSet(properties, "project.id", project.getId());
112 		nullSafeSet(properties, "project.groupId", project.getGroupId());
113 		nullSafeSet(properties, "project.artifactId", project.getArtifactId());
114 		nullSafeSet(properties, "project.version", project.getVersion());
115 		nullSafeSet(properties, "project.packaging", project.getPackaging());
116 		nullSafeSet(properties, "project.name", project.getName());
117 		nullSafeSet(properties, "project.description", project.getDescription());
118 		nullSafeSet(properties, "project.inceptionYear", project.getInceptionYear());
119 		nullSafeSet(properties, "project.basedir", LocationUtils.getCanonicalPath(project.getBasedir()));
120 		if (project.getCiManagement() != null) {
121 			nullSafeSet(properties, "project.ciManagement.system", project.getCiManagement().getSystem());
122 			nullSafeSet(properties, "project.ciManagement.url", project.getCiManagement().getUrl());
123 		}
124 		if (project.getIssueManagement() != null) {
125 			nullSafeSet(properties, "project.issueManagement.system", project.getIssueManagement().getSystem());
126 			nullSafeSet(properties, "project.issueManagement.url", project.getIssueManagement().getUrl());
127 		}
128 		if (project.getBuild() != null) {
129 			nullSafeSet(properties, "project.build.directory", project.getBuild().getDirectory());
130 			nullSafeSet(properties, "project.build.outputDirectory", project.getBuild().getOutputDirectory());
131 			nullSafeSet(properties, "project.build.testOutputDirectory", project.getBuild().getTestOutputDirectory());
132 			nullSafeSet(properties, "project.build.sourceDirectory", project.getBuild().getSourceDirectory());
133 			nullSafeSet(properties, "project.build.scriptSourceDirectory", project.getBuild().getScriptSourceDirectory());
134 			nullSafeSet(properties, "project.build.testSourceDirectory", project.getBuild().getTestSourceDirectory());
135 		}
136 		if (project.getScm() != null) {
137 			nullSafeSet(properties, MAVEN_SCM_CONNECTION_KEY, project.getScm().getConnection());
138 			nullSafeSet(properties, MAVEN_SCM_DEVELOPER_CONNECTION_KEY, project.getScm().getDeveloperConnection());
139 			nullSafeSet(properties, MAVEN_SCM_URL_KEY, project.getScm().getUrl());
140 			String tag = project.getScm().getTag();
141 			if (tag.equals(MAVEN_SCM_TAG_DEFAULT_VALUE)) {
142 				tag = null;
143 			}
144 			nullSafeSet(properties, MAVEN_SCM_TAG_KEY, tag);
145 		}
146 		nullSafeSet(properties, "project.pom.location", getPomLocation(project));
147 		List<Dependency> dependencies = convertToSimplePojos(project.getDependencies());
148 		nullSafeSet(properties, "project.dependencies", getDependenciesCSV(dependencies));
149 		if (settings != null) {
150 			nullSafeSet(properties, "settings.localRepository", settings.getLocalRepository());
151 			nullSafeSet(properties, "settings.modelEncoding", settings.getModelEncoding());
152 			nullSafeSet(properties, "settings.sourceLevel", settings.getSourceLevel());
153 			if (settings.getInteractiveMode() != null) {
154 				nullSafeSet(properties, "settings.interactiveMode", settings.getInteractiveMode() + "");
155 			}
156 		}
157 		return properties;
158 	}
159 
160 	public static Properties getInternalProperties(MavenProject project) {
161 		return getInternalProperties(project, null);
162 	}
163 
164 	/**
165 	 * Don't call setProperty() if value is null
166 	 */
167 	public static void nullSafeSet(Properties properties, String key, String value) {
168 		if (value != null) {
169 			properties.setProperty(key, value);
170 		}
171 	}
172 
173 	// Maven automatically stores the pom to this location
174 	public static String getPomLocation(MavenProject project) {
175 		StringBuilder sb = new StringBuilder();
176 		sb.append("classpath:");
177 		sb.append("META-INF");
178 		sb.append("/");
179 		sb.append("maven");
180 		sb.append("/");
181 		sb.append(project.getGroupId());
182 		sb.append("/");
183 		sb.append(project.getArtifactId());
184 		sb.append("/");
185 		sb.append("pom.xml");
186 		return sb.toString();
187 	}
188 
189 	/**
190 	 * Convert the formal Maven dependency objects into vanilla pojo objects
191 	 */
192 	public static List<Dependency> convertToSimplePojos(List<org.apache.maven.model.Dependency> dependencies) {
193 		List<Dependency> pojos = new ArrayList<Dependency>();
194 		for (org.apache.maven.model.Dependency d : CollectionUtils.toEmptyList(dependencies)) {
195 			Dependency pojo = new Dependency.Builder(d.getGroupId(), d.getArtifactId(), d.getVersion()).classifier(d.getClassifier()).type(d.getType()).scope(d.getScope()).build();
196 			pojos.add(pojo);
197 		}
198 		return pojos;
199 
200 	}
201 
202 	/**
203 	 * Convert the list of dependencies into a CSV string
204 	 */
205 	public static String getDependenciesCSV(List<Dependency> dependencies) {
206 		if (CollectionUtils.isEmpty(dependencies)) {
207 			return NullUtils.NONE;
208 		}
209 		StringBuilder sb = new StringBuilder();
210 		for (int i = 0; i < dependencies.size(); i++) {
211 			if (i != 0) {
212 				sb.append(",");
213 			}
214 			Dependency dependency = dependencies.get(i);
215 			sb.append(RepositoryUtils.toString(dependency));
216 		}
217 		return sb.toString();
218 	}
219 }