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