001    package org.kuali.common.util.spring;
002    
003    import java.io.File;
004    import java.util.List;
005    import java.util.Properties;
006    
007    import org.kuali.common.util.CollectionUtils;
008    import org.kuali.common.util.execute.Executable;
009    import org.kuali.common.util.execute.StorePropertiesExecutable;
010    import org.springframework.beans.factory.annotation.Autowired;
011    import org.springframework.beans.factory.annotation.Value;
012    import org.springframework.context.annotation.Bean;
013    import org.springframework.context.annotation.Configuration;
014    import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
015    import org.springframework.core.env.ConfigurableEnvironment;
016    import org.springframework.util.Assert;
017    
018    @Configuration
019    public class MetaInfProjectPropertiesConfig {
020    
021            @Autowired
022            ConfigurableEnvironment env;
023    
024            @Bean
025            public static PropertySourcesPlaceholderConfigurer pspc() {
026                    return new PropertySourcesPlaceholderConfigurer();
027            }
028    
029            @Value("${project.build.outputDirectory}/META-INF/${project.groupId.path}/${project.artifactId}.properties")
030            File outputFile;
031    
032            @Bean
033            public Properties springProperties() {
034                    return SpringUtils.getAllEnumerableProperties(env);
035            }
036    
037            @Bean(initMethod = "execute")
038            public Executable storePropertiesExecutable() {
039    
040                    // Extract property values from the environment
041                    String encoding = SpringUtils.getProperty(env, "project.encoding");
042                    String includesCSV = SpringUtils.getProperty(env, "project.metainf.includes");
043                    String excludesCSV = SpringUtils.getProperty(env, "project.metainf.excludes");
044    
045                    // Make sure we are configured right
046                    Assert.hasText(encoding);
047                    Assert.hasText(includesCSV);
048                    Assert.hasText(excludesCSV);
049    
050                    // Convert the lists to CSV
051                    List<String> includes = CollectionUtils.getTrimmedListFromCSV(includesCSV);
052                    List<String> excludes = CollectionUtils.getTrimmedListFromCSV(excludesCSV);
053    
054                    // Get the list of all properties spring knows about
055                    Properties properties = springProperties();
056    
057                    // Setup the executable
058                    StorePropertiesExecutable spe = new StorePropertiesExecutable();
059                    spe.setEncoding(encoding);
060                    spe.setOutputFile(outputFile);
061                    spe.setProperties(properties);
062                    spe.setIncludes(includes);
063                    spe.setExcludes(excludes);
064                    return spe;
065            }
066    
067    }