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.spring;
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.kuali.common.util.PropertyUtils;
24  import org.kuali.common.util.property.Constants;
25  import org.kuali.common.util.property.processor.ProjectProcessor;
26  import org.kuali.common.util.property.processor.PropertyProcessor;
27  import org.kuali.common.util.property.processor.VersionProcessor;
28  import org.springframework.beans.factory.annotation.Autowired;
29  import org.springframework.beans.factory.annotation.Qualifier;
30  import org.springframework.context.annotation.Bean;
31  import org.springframework.context.annotation.Configuration;
32  import org.springframework.core.env.PropertiesPropertySource;
33  
34  /**
35   * Augment the default set of properties that ship with Maven and register them as a Spring <code>PropertySource</code>.
36   * 
37   * spring-maven-plugin auto-registers any beans that implement <code>PropertySource</code> as a top level property source
38   */
39  @Configuration
40  public class ProjectPropertySourceConfig {
41  
42  	/**
43  	 * spring-maven-plugin auto-wires Maven properties by default
44  	 */
45  	@Autowired
46  	@Qualifier(Constants.DEFAULT_MAVEN_PROPERTIES_BEAN_NAME)
47  	Properties mavenProperties;
48  
49  	@Bean
50  	public PropertiesPropertySource projectPropertySource() {
51  
52  		// Setup some processors
53  		List<PropertyProcessor> processors = new ArrayList<PropertyProcessor>();
54  
55  		// Add some organization, group, and path properties
56  		processors.add(new ProjectProcessor());
57  
58  		// Tokenize the version number and add properties for each token (major/minor/incremental)
59  		// Also add a boolean property indicating if this is a SNAPSHOT build
60  		processors.add(new VersionProcessor(Arrays.asList("project.version"), true));
61  
62  		// Process default Maven properties and add in our custom properties
63  		PropertyUtils.process(mavenProperties, processors);
64  
65  		// Return the augmented set of Maven properties as a Spring PropertySource
66  		String name = Constants.DEFAULT_MAVEN_PROPERTIES_BEAN_NAME;
67  		return new PropertiesPropertySource(name, mavenProperties);
68  	}
69  }