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  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  		// This PropertySource object is backed by a set of properties that has been
51  		// 1 - fully resolved
52  		// 2 - contains all properties needed by Spring
53  		// 3 - contains system/environment properties where system/env properties override loaded properties
54  		PropertySource<?> source = getPropertySource(service, mavenProperties, propertySourceConfig);
55  
56  		// Setup a property source context such that our single property source is the only one registered with Spring
57  		// This will make it so our PropertySource is the ONLY thing to resolve placeholders
58  		PropertySourceContext psc = new PropertySourceContext(source, true);
59  
60  		// Setup a Spring context
61  		SpringContext context = new SpringContext();
62  
63  		// Supply Spring with our PropertySource
64  		context.setPropertySourceContext(psc);
65  
66  		// Return a Spring context configured with a single property source
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  	 * 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
85  	 * build
86  	 */
87  	public static void augmentProjectProperties(Properties mavenProperties) {
88  		// Setup some processors
89  		List<PropertyProcessor> processors = new ArrayList<PropertyProcessor>();
90  
91  		// Add some organization, group, and path properties
92  		processors.add(new ProjectProcessor());
93  
94  		// Tokenize the version number and add properties for each token (major/minor/incremental)
95  		// Also add a boolean property indicating if this is a SNAPSHOT build
96  		processors.add(new VersionProcessor(Arrays.asList(PROJECT_VERSION_KEY), true));
97  
98  		// Process default Maven properties and add in our custom properties
99  		PropertyUtils.process(mavenProperties, processors);
100 
101 		// Make sure system/environment properties still always win
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 	 * 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.
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 }