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 java.util.ArrayList;
19  import java.util.List;
20  import java.util.Properties;
21  
22  import org.apache.maven.project.MavenProject;
23  import org.apache.maven.settings.Settings;
24  import org.kuali.common.util.CollectionUtils;
25  import org.kuali.common.util.LocationUtils;
26  import org.kuali.common.util.maven.RepositoryUtils;
27  import org.kuali.common.util.maven.model.Dependency;
28  import org.kuali.common.util.nullify.NullUtils;
29  
30  /**
31   * Maven related utilities that are aware of the Maven model objects eg MavenProject
32   */
33  public class MavenAwareUtils {
34  
35  	public static Properties getInternalProperties(MavenProject project, Settings settings) {
36  		Properties properties = new Properties();
37  		nullSafeSet(properties, "project.id", project.getId());
38  		nullSafeSet(properties, "project.groupId", project.getGroupId());
39  		nullSafeSet(properties, "project.artifactId", project.getArtifactId());
40  		nullSafeSet(properties, "project.version", project.getVersion());
41  		nullSafeSet(properties, "project.packaging", project.getPackaging());
42  		nullSafeSet(properties, "project.name", project.getName());
43  		nullSafeSet(properties, "project.description", project.getDescription());
44  		nullSafeSet(properties, "project.inceptionYear", project.getInceptionYear());
45  		nullSafeSet(properties, "project.basedir", LocationUtils.getCanonicalPath(project.getBasedir()));
46  		if (project.getCiManagement() != null) {
47  			nullSafeSet(properties, "project.ciManagement.system", project.getCiManagement().getSystem());
48  			nullSafeSet(properties, "project.ciManagement.url", project.getCiManagement().getUrl());
49  		}
50  		if (project.getIssueManagement() != null) {
51  			nullSafeSet(properties, "project.issueManagement.system", project.getIssueManagement().getSystem());
52  			nullSafeSet(properties, "project.issueManagement.url", project.getIssueManagement().getUrl());
53  		}
54  		if (project.getBuild() != null) {
55  			nullSafeSet(properties, "project.build.directory", project.getBuild().getDirectory());
56  			nullSafeSet(properties, "project.build.outputDirectory", project.getBuild().getOutputDirectory());
57  			nullSafeSet(properties, "project.build.testOutputDirectory", project.getBuild().getTestOutputDirectory());
58  			nullSafeSet(properties, "project.build.sourceDirectory", project.getBuild().getSourceDirectory());
59  			nullSafeSet(properties, "project.build.scriptSourceDirectory", project.getBuild().getScriptSourceDirectory());
60  			nullSafeSet(properties, "project.build.testSourceDirectory", project.getBuild().getTestSourceDirectory());
61  		}
62  		if (project.getScm() != null) {
63  			nullSafeSet(properties, "project.scm.connection", project.getScm().getConnection());
64  			nullSafeSet(properties, "project.scm.developerConnection", project.getScm().getDeveloperConnection());
65  			nullSafeSet(properties, "project.scm.url", project.getScm().getDeveloperConnection());
66  		}
67  		nullSafeSet(properties, "project.pom.location", getPomLocation(project));
68  		List<Dependency> dependencies = convertToSimplePojos(project.getDependencies());
69  		nullSafeSet(properties, "project.dependencies", getDependenciesCSV(dependencies));
70  		if (settings != null) {
71  			nullSafeSet(properties, "settings.localRepository", settings.getLocalRepository());
72  			nullSafeSet(properties, "settings.modelEncoding", settings.getModelEncoding());
73  			nullSafeSet(properties, "settings.sourceLevel", settings.getSourceLevel());
74  			if (settings.getInteractiveMode() != null) {
75  				nullSafeSet(properties, "settings.interactiveMode", settings.getInteractiveMode() + "");
76  			}
77  		}
78  		return properties;
79  	}
80  
81  	public static Properties getInternalProperties(MavenProject project) {
82  		return getInternalProperties(project, null);
83  	}
84  
85  	/**
86  	 * Don't call setProperty() if value is null
87  	 */
88  	public static void nullSafeSet(Properties properties, String key, String value) {
89  		if (value != null) {
90  			properties.setProperty(key, value);
91  		}
92  	}
93  
94  	// Maven automatically stores the pom to this location
95  	public static String getPomLocation(MavenProject project) {
96  		StringBuilder sb = new StringBuilder();
97  		sb.append("classpath:");
98  		sb.append("META-INF");
99  		sb.append("/");
100 		sb.append("maven");
101 		sb.append("/");
102 		sb.append(project.getGroupId());
103 		sb.append("/");
104 		sb.append(project.getArtifactId());
105 		sb.append("/");
106 		sb.append("pom.xml");
107 		return sb.toString();
108 	}
109 
110 	/**
111 	 * Convert the formal Maven dependency objects into vanilla pojo objects
112 	 */
113 	public static List<Dependency> convertToSimplePojos(List<org.apache.maven.model.Dependency> dependencies) {
114 		List<Dependency> pojos = new ArrayList<Dependency>();
115 		for (org.apache.maven.model.Dependency d : CollectionUtils.toEmptyList(dependencies)) {
116 			Dependency pojo = new Dependency.Builder(d.getGroupId(), d.getArtifactId(), d.getVersion()).classifier(d.getClassifier()).type(d.getType()).scope(d.getScope()).build();
117 			pojos.add(pojo);
118 		}
119 		return pojos;
120 
121 	}
122 
123 	/**
124 	 * Convert the list of dependencies into a CSV string
125 	 */
126 	public static String getDependenciesCSV(List<Dependency> dependencies) {
127 		if (CollectionUtils.isEmpty(dependencies)) {
128 			return NullUtils.NONE;
129 		}
130 		StringBuilder sb = new StringBuilder();
131 		for (int i = 0; i < dependencies.size(); i++) {
132 			if (i != 0) {
133 				sb.append(",");
134 			}
135 			Dependency dependency = dependencies.get(i);
136 			sb.append(RepositoryUtils.toString(dependency));
137 		}
138 		return sb.toString();
139 	}
140 }