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