1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
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
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
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 }