001/** 002 * Copyright 2010-2014 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 */ 016package org.kuali.common.util.maven; 017 018import static com.google.common.collect.Lists.newArrayList; 019import static java.util.Arrays.asList; 020import static org.kuali.common.util.PropertyUtils.getRequiredResolvedProperty; 021import static org.kuali.common.util.PropertyUtils.prepareContextProperties; 022 023import java.util.ArrayList; 024import java.util.List; 025import java.util.Map; 026import java.util.Properties; 027 028import org.apache.commons.lang3.StringUtils; 029import org.kuali.common.util.CollectionUtils; 030import org.kuali.common.util.PropertyUtils; 031import org.kuali.common.util.project.ProjectService; 032import org.kuali.common.util.property.PropertiesContext; 033import org.kuali.common.util.property.processor.PropertyProcessor; 034import org.kuali.common.util.property.processor.VersionProcessor; 035import org.kuali.common.util.spring.PropertySourceUtils; 036import org.kuali.common.util.spring.service.DefaultPropertySourceService; 037import org.kuali.common.util.spring.service.DefaultSpringService; 038import org.kuali.common.util.spring.service.PropertySourceService; 039import org.kuali.common.util.spring.service.SpringContext; 040import org.slf4j.Logger; 041import org.slf4j.LoggerFactory; 042import org.springframework.core.env.Environment; 043import org.springframework.core.env.PropertySource; 044import org.springframework.util.Assert; 045 046/** 047 * Maven utilities that don't depend on Maven libraries 048 */ 049public class MavenUtils { 050 051 private static final Logger logger = LoggerFactory.getLogger(MavenUtils.class); 052 053 @Deprecated 054 public static final String POM = MavenConstants.POM; 055 056 @Deprecated 057 public static final String PROJECT_VERSION_KEY = MavenConstants.VERSION_KEY; 058 059 @Deprecated 060 public static final String PROJECT_ENCODING_KEY = MavenConstants.ENCODING_KEY; 061 062 @Deprecated 063 public static SpringContext getMavenizedSpringContext(Class<?> propertySourceConfig) { 064 return getMavenizedSpringContext(null, propertySourceConfig); 065 } 066 067 /** 068 * Return a SpringContext that resolves placeholders using the single <code>PropertySource</code> registered with <code>propertySourceConfig</code> 069 */ 070 @Deprecated 071 public static SpringContext getMavenizedSpringContext(Properties mavenProperties, Class<?> propertySourceConfig) { 072 PropertySourceService service = new DefaultPropertySourceService(new DefaultSpringService()); 073 return getMavenizedSpringContext(service, mavenProperties, propertySourceConfig); 074 } 075 076 @Deprecated 077 public static SpringContext getMavenizedSpringContext(PropertySourceService service, Properties mavenProperties, Class<?> propertySourceConfig) { 078 Map<String, Object> beans = CollectionUtils.toEmptyMap(MavenConstants.PROPERTIES_BEAN_NAME, (Object) mavenProperties); 079 List<PropertySource<?>> sources = service.getPropertySources(beans, null, null, propertySourceConfig); 080 Assert.isTrue(sources.size() == 1, "sources.size != 1"); 081 return PropertySourceUtils.getSinglePropertySourceContext(sources.get(0)); 082 } 083 084 /** 085 * 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 086 * build 087 */ 088 @Deprecated 089 public static void augmentProjectProperties(ProjectService service, Properties mavenProperties) { 090 augmentProjectProperties(mavenProperties); 091 } 092 093 /** 094 * 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 095 * build 096 */ 097 public static void augmentProjectProperties(Properties mavenProperties) { 098 // Setup some processors 099 List<PropertyProcessor> processors = newArrayList(); 100 101 // Add some organization, group, and path properties 102 processors.add(new org.kuali.common.util.property.processor.ProjectProcessor()); 103 104 // Tokenize the version number and add properties for each token (major/minor/incremental) 105 // Also add a boolean property indicating if this is a SNAPSHOT build 106 processors.add(new VersionProcessor(asList(MavenConstants.VERSION_KEY), true)); 107 108 // Process default Maven properties and add in our custom properties 109 PropertyUtils.process(mavenProperties, processors); 110 111 // Finish preparing the properties using the encoding from the project 112 String encoding = getRequiredResolvedProperty(mavenProperties, MavenConstants.ENCODING_KEY); 113 prepareContextProperties(mavenProperties, encoding); 114 } 115 116 @Deprecated 117 public static org.kuali.common.util.property.ProjectProperties getMavenProjectProperties(Properties mavenProperties) { 118 org.kuali.common.util.Project project = org.kuali.common.util.ProjectUtils.getProject(mavenProperties); 119 120 PropertiesContext pc = new PropertiesContext(); 121 pc.setProperties(mavenProperties); 122 123 org.kuali.common.util.property.ProjectProperties pp = new org.kuali.common.util.property.ProjectProperties(); 124 pp.setProject(project); 125 pp.setPropertiesContext(pc); 126 return pp; 127 } 128 129 @Deprecated 130 protected static List<String> getList(Properties properties, String key) { 131 String csv = properties.getProperty(key); 132 List<String> list = new ArrayList<String>(); 133 list.addAll(CollectionUtils.getTrimmedListFromCSV(csv)); 134 return list; 135 } 136 137 @Deprecated 138 protected static List<String> getList(Environment env, Properties properties, String key) { 139 String csv = env.getProperty(key); 140 List<String> list = new ArrayList<String>(); 141 list.addAll(CollectionUtils.getTrimmedListFromCSV(csv)); 142 list.addAll(getList(properties, key)); 143 return list; 144 } 145 146 /** 147 * 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. 148 */ 149 public static final boolean skip(boolean forceMojoExecution, boolean skip, String packaging) { 150 if (forceMojoExecution) { 151 logger.info("Forced mojo execution"); 152 return false; 153 } 154 if (skip) { 155 logger.info("Skipping mojo execution"); 156 return true; 157 } 158 if (StringUtils.equalsIgnoreCase(packaging, POM)) { 159 logger.info("Skipping mojo execution for project with packaging type '{}'", POM); 160 return true; 161 } else { 162 return false; 163 } 164 } 165 166}