1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
21 import java.util.Properties;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.kuali.common.util.property.Constants;
25 import org.kuali.common.util.property.GlobalPropertiesMode;
26 import org.kuali.common.util.property.ProjectProperties;
27 import org.kuali.common.util.property.PropertiesContext;
28 import org.kuali.common.util.property.processor.ProjectProcessor;
29 import org.kuali.common.util.property.processor.PropertyProcessor;
30 import org.kuali.common.util.property.processor.VersionProcessor;
31 import org.kuali.common.util.service.PropertySourceContext;
32 import org.kuali.common.util.service.SpringContext;
33 import org.kuali.common.util.service.SpringService;
34 import org.kuali.common.util.spring.SpringUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.core.env.Environment;
38 import org.springframework.core.env.PropertySource;
39
40
41
42
43 public class MavenUtils {
44
45 private static final Logger logger = LoggerFactory.getLogger(MavenUtils.class);
46
47 public static final String POM = "pom";
48 public static final String INCLUDE = "properties.maven.include";
49 public static final String EXCLUDE = "properties.maven.exclude";
50 public static final String PROJECT_VERSION_KEY = "project.version";
51
52 public static SpringContext getMavenizedSpringContext(SpringService service, Properties mavenProperties, Class<?> propertySourceConfig) {
53
54
55
56
57 PropertySource<?> source = getPropertySource(service, mavenProperties, propertySourceConfig);
58
59
60
61 PropertySourceContext psc = new PropertySourceContext(source, true);
62
63
64 SpringContext context = new SpringContext();
65
66
67 context.setPropertySourceContext(psc);
68
69
70 return context;
71 }
72
73 protected static PropertySource<?> getPropertySource(SpringService service, Properties mavenProperties, Class<?> annotatedClass) {
74 return getPropertySource(service, annotatedClass, Constants.DEFAULT_MAVEN_PROPERTIES_BEAN_NAME, mavenProperties);
75 }
76
77 protected static PropertySource<?> getPropertySource(SpringService service, Class<?> annotatedClass, String mavenPropertiesBeanName, Properties mavenProperties) {
78 List<PropertySource<?>> sources = SpringUtils.getPropertySources(service, annotatedClass, mavenPropertiesBeanName, mavenProperties);
79 if (sources.size() > 1) {
80 throw new IllegalStateException("More than one PropertySource was registered in the context");
81 } else {
82 return sources.get(0);
83 }
84 }
85
86
87
88
89
90 public static void augmentProjectProperties(Properties mavenProperties) {
91
92 List<PropertyProcessor> processors = new ArrayList<PropertyProcessor>();
93
94
95 processors.add(new ProjectProcessor());
96
97
98
99 processors.add(new VersionProcessor(Arrays.asList(PROJECT_VERSION_KEY), true));
100
101
102 PropertyUtils.process(mavenProperties, processors);
103
104
105 PropertyUtils.overrideWithGlobalValues(mavenProperties, GlobalPropertiesMode.BOTH);
106
107
108 SpringUtils.decrypt(mavenProperties);
109
110
111 trim(mavenProperties);
112
113
114 SpringUtils.resolve(mavenProperties);
115 }
116
117 public static void trim(Properties mavenProperties) {
118 List<String> excludes = getList(mavenProperties, EXCLUDE);
119 List<String> includes = getList(mavenProperties, INCLUDE);
120 PropertyUtils.trim(mavenProperties, includes, excludes);
121 }
122
123 public static void trim(Environment env, Properties mavenProperties) {
124 List<String> excludes = getList(env, mavenProperties, EXCLUDE);
125 List<String> includes = getList(env, mavenProperties, INCLUDE);
126 PropertyUtils.trim(mavenProperties, includes, excludes);
127 }
128
129 public static ProjectProperties getMavenProjectProperties(Environment env, Properties mavenProperties) {
130 Project project = ProjectUtils.getProject(mavenProperties);
131
132 trim(env, mavenProperties);
133
134 PropertiesContext pc = new PropertiesContext();
135 pc.setProperties(mavenProperties);
136
137 ProjectProperties pp = new ProjectProperties();
138 pp.setProject(project);
139 pp.setPropertiesContext(pc);
140 return pp;
141 }
142
143 protected static List<String> getList(Properties properties, String key) {
144 String csv = properties.getProperty(key);
145 List<String> list = new ArrayList<String>();
146 list.addAll(CollectionUtils.getTrimmedListFromCSV(csv));
147 return list;
148 }
149
150 protected static List<String> getList(Environment env, Properties properties, String key) {
151 String csv = env.getProperty(key);
152 List<String> list = new ArrayList<String>();
153 list.addAll(CollectionUtils.getTrimmedListFromCSV(csv));
154 list.addAll(getList(properties, key));
155 return list;
156 }
157
158
159
160
161 public static final boolean skip(boolean forceMojoExecution, boolean skip, String packaging) {
162 if (forceMojoExecution) {
163 logger.info("Forced mojo execution");
164 return false;
165 }
166 if (skip) {
167 logger.info("Skipping mojo execution");
168 return true;
169 }
170 if (StringUtils.equalsIgnoreCase(packaging, POM)) {
171 logger.info("Skipping mojo execution for project with packaging type '{}'", POM);
172 return true;
173 } else {
174 return false;
175 }
176 }
177
178 }