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 public class MavenUtils {
41
42 private static final Logger logger = LoggerFactory.getLogger(MavenUtils.class);
43
44 public static final String POM = "pom";
45 public static final String INCLUDE = "properties.maven.include";
46 public static final String EXCLUDE = "properties.maven.exclude";
47 public static final String PROJECT_VERSION_KEY = "project.version";
48
49 public static SpringContext getMavenizedSpringContext(SpringService service, Properties mavenProperties, Class<?> propertySourceConfig) {
50
51
52
53
54 PropertySource<?> source = getPropertySource(service, mavenProperties, propertySourceConfig);
55
56
57
58 PropertySourceContext psc = new PropertySourceContext(source, true);
59
60
61 SpringContext context = new SpringContext();
62
63
64 context.setPropertySourceContext(psc);
65
66
67 return context;
68 }
69
70 protected static PropertySource<?> getPropertySource(SpringService service, Properties mavenProperties, Class<?> annotatedClass) {
71 return getPropertySource(service, annotatedClass, Constants.DEFAULT_MAVEN_PROPERTIES_BEAN_NAME, mavenProperties);
72 }
73
74 protected static PropertySource<?> getPropertySource(SpringService service, Class<?> annotatedClass, String mavenPropertiesBeanName, Properties mavenProperties) {
75 List<PropertySource<?>> sources = SpringUtils.getPropertySources(service, annotatedClass, mavenPropertiesBeanName, mavenProperties);
76 if (sources.size() > 1) {
77 throw new IllegalStateException("More than one PropertySource was registered in the context");
78 } else {
79 return sources.get(0);
80 }
81 }
82
83
84
85
86
87 public static void augmentProjectProperties(Properties mavenProperties) {
88
89 List<PropertyProcessor> processors = new ArrayList<PropertyProcessor>();
90
91
92 processors.add(new ProjectProcessor());
93
94
95
96 processors.add(new VersionProcessor(Arrays.asList(PROJECT_VERSION_KEY), true));
97
98
99 PropertyUtils.process(mavenProperties, processors);
100
101
102 PropertyUtils.overrideWithGlobalValues(mavenProperties, GlobalPropertiesMode.BOTH);
103 }
104
105 public static void trim(Environment env, Properties mavenProperties) {
106 List<String> excludes = getList(env, mavenProperties, EXCLUDE);
107 List<String> includes = getList(env, mavenProperties, INCLUDE);
108 PropertyUtils.trim(mavenProperties, includes, excludes);
109 }
110
111 public static ProjectProperties getMavenProjectProperties(Environment env, Properties mavenProperties) {
112 Project project = ProjectUtils.getProject(mavenProperties);
113
114 trim(env, mavenProperties);
115
116 PropertiesContext pc = new PropertiesContext();
117 pc.setProperties(mavenProperties);
118
119 ProjectProperties pp = new ProjectProperties();
120 pp.setProject(project);
121 pp.setPropertiesContext(pc);
122 return pp;
123 }
124
125 protected static List<String> getList(Environment env, Properties properties, String key) {
126 String csv1 = env.getProperty(key);
127 String csv2 = properties.getProperty(key);
128 List<String> list = new ArrayList<String>();
129 list.addAll(CollectionUtils.getTrimmedListFromCSV(csv1));
130 list.addAll(CollectionUtils.getTrimmedListFromCSV(csv2));
131 return list;
132 }
133
134
135
136
137 public static final boolean skip(boolean forceMojoExecution, boolean skip, String packaging) {
138 if (forceMojoExecution) {
139 logger.info("Forced mojo execution");
140 return false;
141 }
142 if (skip) {
143 logger.info("Skipping mojo execution");
144 return true;
145 }
146 if (StringUtils.equalsIgnoreCase(packaging, POM)) {
147 logger.info("Skipping mojo execution for project with packaging type '{}'", POM);
148 return true;
149 } else {
150 return false;
151 }
152 }
153
154 }