View Javadoc

1   /**
2    * Copyright 2010-2013 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.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   * Maven related utilities that do not need depend on classes that are Maven specific.
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  		// This PropertySource object is backed by a set of properties that has been
54  		// 1 - fully resolved
55  		// 2 - contains all properties needed by Spring
56  		// 3 - contains system/environment properties where system/env properties override loaded properties
57  		PropertySource<?> source = getPropertySource(service, mavenProperties, propertySourceConfig);
58  
59  		// Setup a property source context such that our single property source is the only one registered with Spring
60  		// This will make it so our PropertySource is the ONLY thing to resolve placeholders
61  		PropertySourceContext psc = new PropertySourceContext(source, true);
62  
63  		// Setup a Spring context
64  		SpringContext context = new SpringContext();
65  
66  		// Supply Spring with our PropertySource
67  		context.setPropertySourceContext(psc);
68  
69  		// Return a Spring context configured with a single property source
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  	 * Add organization, group, and path properties and tokenize the version number adding properties for each token along with a boolean property indicating if this is a SNAPSHOT
88  	 * build
89  	 */
90  	public static void augmentProjectProperties(Properties mavenProperties) {
91  		// Setup some processors
92  		List<PropertyProcessor> processors = new ArrayList<PropertyProcessor>();
93  
94  		// Add some organization, group, and path properties
95  		processors.add(new ProjectProcessor());
96  
97  		// Tokenize the version number and add properties for each token (major/minor/incremental)
98  		// Also add a boolean property indicating if this is a SNAPSHOT build
99  		processors.add(new VersionProcessor(Arrays.asList(PROJECT_VERSION_KEY), true));
100 
101 		// Process default Maven properties and add in our custom properties
102 		PropertyUtils.process(mavenProperties, processors);
103 
104 		// Make sure system/environment properties still always win
105 		PropertyUtils.overrideWithGlobalValues(mavenProperties, GlobalPropertiesMode.BOTH);
106 
107 		// Optionally decrypt if properties.decrypt=true
108 		SpringUtils.decrypt(mavenProperties);
109 
110 		// Remove properties that are not desired
111 		trim(mavenProperties);
112 
113 		// Make sure every single property can be fully resolved
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 	 * Always return false if <code>forceMojoExecution</code> is true, otherwise return true only if <code>skip</code> is true or <code>packaging</code> is pom.
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 }