001 /**
002 * Copyright 2010-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.common.util;
017
018 import java.util.ArrayList;
019 import java.util.Arrays;
020 import java.util.List;
021 import java.util.Properties;
022
023 import org.apache.commons.lang3.StringUtils;
024 import org.kuali.common.util.property.Constants;
025 import org.kuali.common.util.property.ProjectProperties;
026 import org.kuali.common.util.property.PropertiesContext;
027 import org.kuali.common.util.property.processor.ProjectProcessor;
028 import org.kuali.common.util.property.processor.PropertyProcessor;
029 import org.kuali.common.util.property.processor.VersionProcessor;
030 import org.kuali.common.util.service.SpringContext;
031 import org.kuali.common.util.spring.SpringUtils;
032 import org.slf4j.Logger;
033 import org.slf4j.LoggerFactory;
034 import org.springframework.core.env.Environment;
035 import org.springframework.core.env.PropertySource;
036
037 /**
038 * Maven utilities that don't depend on Maven libraries
039 */
040 public class MavenUtils {
041
042 private static final Logger logger = LoggerFactory.getLogger(MavenUtils.class);
043
044 public static final String POM = "pom";
045 public static final String PROJECT_VERSION_KEY = "project.version";
046 public static final String PROJECT_ENCODING_KEY = "project.encoding";
047
048 public static SpringContext getMavenizedSpringContext(Class<?> propertySourceConfig) {
049 return getMavenizedSpringContext(null, propertySourceConfig);
050 }
051
052 /**
053 * Return a SpringContext that resolves placeholders using the single <code>PropertySource</code> registered with <code>propertySourceConfig</code>
054 */
055 public static SpringContext getMavenizedSpringContext(Properties mavenProperties, Class<?> propertySourceConfig) {
056 // This PropertySource object is backed by a set of properties that has been
057 // 1 - fully resolved
058 // 2 - contains all properties needed by Spring
059 // 3 - contains system/environment properties where system/env properties override loaded properties
060 PropertySource<?> source = SpringUtils.getSinglePropertySource(propertySourceConfig, Constants.DEFAULT_MAVEN_PROPERTIES_BEAN_NAME, mavenProperties);
061
062 return SpringUtils.getSinglePropertySourceContext(source);
063 }
064
065 /**
066 * 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
067 * build
068 */
069 public static void augmentProjectProperties(Properties mavenProperties) {
070 // Setup some processors
071 List<PropertyProcessor> processors = new ArrayList<PropertyProcessor>();
072
073 // Add some organization, group, and path properties
074 processors.add(new ProjectProcessor());
075
076 // Tokenize the version number and add properties for each token (major/minor/incremental)
077 // Also add a boolean property indicating if this is a SNAPSHOT build
078 processors.add(new VersionProcessor(Arrays.asList(PROJECT_VERSION_KEY), true));
079
080 // Process default Maven properties and add in our custom properties
081 PropertyUtils.process(mavenProperties, processors);
082
083 // Finish preparing the properties using the encoding from the project
084 String encoding = PropertyUtils.getRequiredResolvedProperty(mavenProperties, PROJECT_ENCODING_KEY);
085 PropertyUtils.prepareContextProperties(mavenProperties, encoding);
086 }
087
088 public static ProjectProperties getMavenProjectProperties(Properties mavenProperties) {
089 Project project = ProjectUtils.getProject(mavenProperties);
090
091 PropertiesContext pc = new PropertiesContext();
092 pc.setProperties(mavenProperties);
093
094 ProjectProperties pp = new ProjectProperties();
095 pp.setProject(project);
096 pp.setPropertiesContext(pc);
097 return pp;
098 }
099
100 protected static List<String> getList(Properties properties, String key) {
101 String csv = properties.getProperty(key);
102 List<String> list = new ArrayList<String>();
103 list.addAll(CollectionUtils.getTrimmedListFromCSV(csv));
104 return list;
105 }
106
107 protected static List<String> getList(Environment env, Properties properties, String key) {
108 String csv = env.getProperty(key);
109 List<String> list = new ArrayList<String>();
110 list.addAll(CollectionUtils.getTrimmedListFromCSV(csv));
111 list.addAll(getList(properties, key));
112 return list;
113 }
114
115 /**
116 * 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.
117 */
118 public static final boolean skip(boolean forceMojoExecution, boolean skip, String packaging) {
119 if (forceMojoExecution) {
120 logger.info("Forced mojo execution");
121 return false;
122 }
123 if (skip) {
124 logger.info("Skipping mojo execution");
125 return true;
126 }
127 if (StringUtils.equalsIgnoreCase(packaging, POM)) {
128 logger.info("Skipping mojo execution for project with packaging type '{}'", POM);
129 return true;
130 } else {
131 return false;
132 }
133 }
134
135 }