diff --git a/core/pom.xml b/core/pom.xml index 39b19b2836..2dafcf08d9 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -120,13 +120,6 @@ json-path-assert - - - org.springframework.boot - spring-boot - ${version.spring-boot} - - diff --git a/core/src/main/java/io/fabric8/maven/core/util/SpringBootUtil.java b/core/src/main/java/io/fabric8/maven/core/util/SpringBootUtil.java index 0c24a286b8..f6548a3004 100644 --- a/core/src/main/java/io/fabric8/maven/core/util/SpringBootUtil.java +++ b/core/src/main/java/io/fabric8/maven/core/util/SpringBootUtil.java @@ -17,29 +17,20 @@ package io.fabric8.maven.core.util; import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.util.CollectionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.boot.Banner; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.boot.env.PropertiesPropertySourceLoader; -import org.springframework.boot.env.PropertySourcesLoader; -import org.springframework.boot.env.YamlPropertySourceLoader; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.core.env.MapPropertySource; -import org.springframework.core.env.PropertySource; -import org.springframework.core.io.Resource; -import org.springframework.core.io.UrlResource; -import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.SafeConstructor; import java.io.IOException; +import java.io.InputStream; import java.net.URL; import java.net.URLClassLoader; import java.util.*; /** * Utility methods to access spring-boot resources. - * TODO: remove unwanted methods after Roland reviews and okays logic */ public class SpringBootUtil { @@ -52,121 +43,135 @@ public class SpringBootUtil { * @param activeProfiles - the comma separated String of profiles * @return properties - the merged properties of all profiles */ - public static Properties getApplicationProperties(MavenProject project, String activeProfiles) throws IOException { + public static Properties getApplicationProperties(MavenProject project, String activeProfiles) { return getApplicationProperties(project, getActiveProfiles(activeProfiles)); } - /** - * TODO: need to check with Roland to see if we can use this approach - * A simple short method where we simply run a small spring boot application during build and load the environment - * to get the property sources, then load the properties from - * - * @param activeProfiles - the active profiles to be used if {@code null}, its not passed to spring boot application run - * @return Properties - the application environment properties that will be used during image building process + * Returns the spring boot configuration (supports `application.properties` and `application.yml`) + * or an empty properties object if not found */ - public static Properties runAndLoadPropertiesUsingEnv(List activeProfiles) { - - SpringApplication sbBuilder; - - if (activeProfiles != null && activeProfiles.size() > 0) { - sbBuilder = new SpringApplicationBuilder(DummySpringBootApplication.class) - .web(false) - .headless(true) - .bannerMode(Banner.Mode.OFF) - .profiles(activeProfiles.toArray(new String[activeProfiles.size()])) - .build(); - } else { - sbBuilder = new SpringApplicationBuilder(DummySpringBootApplication.class) - .web(false) - .headless(true) - .bannerMode(Banner.Mode.OFF) - .build(); - } - - Properties applicationProperties = new Properties(); - try { - ConfigurableApplicationContext ctx = sbBuilder.run(); - - applicationProperties = new Properties(); + public static Properties getApplicationProperties(MavenProject project, List activeProfiles) { + URLClassLoader compileClassLoader = MavenUtil.getCompileClassLoader(project); - for (PropertySource propertySource : ctx.getEnvironment().getPropertySources()) { + Properties props = new Properties(); + addApplicationProperties(activeProfiles, compileClassLoader, props, null); - if (propertySource != null && propertySource instanceof MapPropertySource) { - applicationProperties.putAll(((MapPropertySource) propertySource).getSource()); - } + //If the profiles are available load the profile resources as well + if (activeProfiles != null) { + for (String profile : activeProfiles) { + addApplicationProperties(activeProfiles, compileClassLoader, props, profile); } - } catch (Exception e) { - //swallow it .. } - - return applicationProperties; - } - - /** - * Returns the spring boot configuration (supports `application.properties` and `application.yml`) - * or an empty properties object if not found - */ - public static Properties getApplicationProperties(MavenProject project, List activeProfiles) - throws IOException { - - return runAndLoadPropertiesUsingEnv(activeProfiles); - //Properties props = new Properties(); -// addApplicationProperties(project, props, null); -// -// //If the profiles are available load the profile resources as well -// if (activeProfiles != null && !activeProfiles.isEmpty()) { -// for (String profile : activeProfiles) { -// addApplicationProperties(project, props, profile); -// } -// } - //return props; + return props; } /** * Returns the given properties file on the project classpath if found or an empty properties object if not */ - public static Properties getPropertiesFile(MavenProject project, String propertiesFileName) throws IOException { + public static Properties getPropertiesFile(MavenProject project, String propertiesFileName) { URLClassLoader compileClassLoader = MavenUtil.getCompileClassLoader(project); URL resource = compileClassLoader.findResource(propertiesFileName); - return getPropertiesResource(resource, null); + return getPropertiesResource(resource); } /** * Returns the given properties resource on the project classpath if found or an empty properties object if not */ - @SuppressWarnings("unchecked") - protected static Properties getPropertiesResource(URL resource, String profile) throws IOException { - Properties properties = new Properties(); + protected static Properties getPropertiesResource(URL resource) { + Properties answer = new Properties(); if (resource != null) { - PropertiesPropertySourceLoader propertySourceLoader = new PropertiesPropertySourceLoader(); - PropertySource propertySource = (PropertySource) propertySourceLoader.load(resource.getFile(), - new UrlResource(resource), profile); - if (propertySource != null) { - properties.putAll(propertySource.getSource()); + try (InputStream stream = resource.openStream()) { + answer.load(stream); + } catch (IOException e) { + throw new IllegalStateException("Error while reading resource from URL " + resource, e); } } - return properties; + return answer; + } + + /** + * Returns a {@code Properties} representation of the given Yaml file on the project classpath if found or an empty properties object if not + */ + public static Properties getPropertiesFromYamlFile(MavenProject project, String yamlFileName, + List activeProfiles) { + URLClassLoader compileClassLoader = MavenUtil.getCompileClassLoader(project); + URL resource = compileClassLoader.findResource(yamlFileName); + return getPropertiesFromYamlResource(resource, activeProfiles); } /** * Returns a {@code Properties} representation of the given Yaml resource or an empty properties object if the resource is null */ - @SuppressWarnings("unchecked") - protected static Properties getPropertiesFromYamlResource(URL resource, String profile) throws IOException { - Properties properties = new Properties(); + protected static Properties getPropertiesFromYamlResource(URL resource, List activeProfiles) { if (resource != null) { - YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader(); + try (InputStream yamlStream = resource.openStream()) { + Yaml yaml = new Yaml(new SafeConstructor()); + + Map profileDocs = new HashMap<>(); + + Properties properties = new Properties(); + + Iterable yamlDoc = yaml.loadAll(yamlStream); + + Iterator yamlDocIterator = yamlDoc.iterator(); + + int docCount = 0; + while (yamlDocIterator.hasNext()) { + Map docRoot = (Map) yamlDocIterator.next(); + + String profiles = null; - PropertySource propertySource = (PropertySource) - yamlPropertySourceLoader.load(resource.getFile(), - new UrlResource(resource), profile); + if (docRoot.containsKey("spring")) { + + LinkedHashMap value = (LinkedHashMap) docRoot.get("spring"); + + Object profilesValue = value.get("profiles"); + + if (profilesValue instanceof Map) { + Map profileMap = (Map) profilesValue; + if (activeProfiles.isEmpty() && docCount > 0) { + if (profileMap.containsKey("active")) { + activeProfiles.addAll(getActiveProfiles((String) profileMap.get("active"))); + } + } + } else if (profilesValue instanceof String) { + profiles = (String) profilesValue; + } + } + + if (profiles != null) { + String[] profileSplit = profiles.split("\\s*,\\s*"); + if (!CollectionUtils. + intersection(Arrays.asList(profileSplit), activeProfiles) + .isEmpty()) { + //if the profiles is in the list of active profiles we add it to our list of docs + profileDocs.put(profiles, docRoot); + } + } else if (docCount == 0) { + //the root doc + profileDocs.put("default", docRoot); + } + + docCount++; + } - if (propertySource != null) { - properties.putAll(propertySource.getSource()); + LOG.debug("Spring Boot Profile docs:{}" + profileDocs); + + properties.putAll(getFlattenedMap(profileDocs.get("default"))); + + for (String activeProfile : activeProfiles) { + if (profileDocs.containsKey(activeProfile)) { + properties.putAll(getFlattenedMap(profileDocs.get(activeProfile))); + } + } + + return properties; + } catch (IOException e) { + throw new IllegalStateException("Error while reading Yaml resource from URL " + resource, e); } } - return properties; + return new Properties(); } /** @@ -179,8 +184,47 @@ public static String getSpringBootDevToolsVersion(MavenProject mavenProject) { /** * Determine the spring-boot major version for the current project */ - public static String getSpringBootVersion(MavenProject mavenProject) {return MavenUtil.getDependencyVersion(mavenProject, SpringBootConfigurationHelper.SPRING_BOOT_GROUP_ID, SpringBootConfigurationHelper.SPRING_BOOT_ARTIFACT_ID); + public static String getSpringBootVersion(MavenProject mavenProject) {return MavenUtil.getDependencyVersion(mavenProject, SpringBootConfigurationHelper.SPRING_BOOT_GROUP_ID, + SpringBootConfigurationHelper.SPRING_BOOT_ARTIFACT_ID);} + /** + * Build a flattened representation of the Yaml tree. The conversion is compliant with the spring-boot rules. + */ + private static Map getFlattenedMap(Map source) { + Map result = new LinkedHashMap<>(); + buildFlattenedMap(result, source, null); + return result; + } + + @SuppressWarnings("unchecked") + private static void buildFlattenedMap(Map result, Map source, String path) { + for (Map.Entry entry : source.entrySet()) { + String key = entry.getKey(); + if (path != null && path.trim().length() > 0) { + if (key.startsWith("[")) { + key = path + key; + } else { + key = path + "." + key; + } + } + Object value = entry.getValue(); + if (value instanceof String) { + result.put(key, value); + } else if (value instanceof Map) { + + Map map = (Map) value; + buildFlattenedMap(result, map, key); + } else if (value instanceof Collection) { + Collection collection = (Collection) value; + int count = 0; + for (Object object : collection) { + buildFlattenedMap(result, + Collections.singletonMap("[" + (count++) + "]", object), key); + } + } else { + result.put(key, (value != null ? value : "")); + } + } } public static List getActiveProfiles(String strActiveProfiles) { @@ -204,125 +248,32 @@ private static URL getResourceFromClasspath(URLClassLoader compileClassLoader, S return urlResource; } - private static void runAndLoadEnv(String... profiles) { - SpringApplication mvnSpringApplication = - new SpringApplicationBuilder() - .profiles(profiles) - .build(); - mvnSpringApplication.setWebEnvironment(false); - - } - /** * Method to add the application properties from spring boot profile resources and merge them as one * - * @param project - the maven project of the build - * @param mergedProperties - the merged properties container - * @param profile - the profile to use when searching the spring boot resources + * @param activeProfiles - the active profiles list typically a comma separated string of profile names + * @param compileClassLoader - the classloader in which the resource will be searched + * @param mergedProperties - the merged properties container + * @param profile - the profile to use when searching the spring boot resources */ - private static void addApplicationProperties(MavenProject project, Properties mergedProperties, - String profile) throws IOException { - - PropertySourcesLoader propertySourcesLoader = new PropertySourcesLoader(); - - //Applications can override the config file name using this system property - //Ref: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html - - String configFileName = System.getProperty("spring.config.name", "application"); - - Set includesResources = new LinkedHashSet<>(); + private static void addApplicationProperties(List activeProfiles, URLClassLoader compileClassLoader, + Properties mergedProperties, String profile) { + URL ymlResource; + URL propertiesResource; + Properties profileProperties; if (profile == null) { - scanForApplicationPropertySources(project, configFileName, includesResources, null); - + ymlResource = compileClassLoader.findResource("application.yml"); + propertiesResource = compileClassLoader.findResource("application.properties"); + mergedProperties = getPropertiesFromYamlResource(ymlResource, activeProfiles); + mergedProperties.putAll(getPropertiesResource(propertiesResource)); } else { - scanForApplicationPropertySources(project, configFileName, includesResources, null); - } - - - Properties profileProperties = new Properties(); - - for (Resource resource : includesResources) { - MapPropertySource propertySource = (MapPropertySource) propertySourcesLoader.load(resource); - if (propertySource != null) { - profileProperties.putAll(propertySource.getSource()); - } - } - - mergedProperties.putAll(profileProperties); - } - - /** - * A utility method to scan for spring boot application sources {@code *.properties,*.yaml,*.json,*.yml} - * - * @param mavenProject - the maven project which houses the spring boot application - * @param configFileName - the configuration file name {@code spring.config.name} system property or defaults to application - * @param includesResources - the collection to which the scanned resources will be added - * @param profile - the Spring profile - * @throws IOException - any exception that might occur while scanning the resource - */ - private static void scanForApplicationPropertySources(MavenProject mavenProject, String configFileName, - Set includesResources, - String profile) throws IOException { - - URLClassLoader classLoader = MavenUtil.getCompileClassLoader(mavenProject); - PathMatchingResourcePatternResolver scanner = new PathMatchingResourcePatternResolver(classLoader); - String propertiesFilePattern; - - //PROPERTIES - if (profile == null) { - propertiesFilePattern = "**/" + configFileName + ".properties"; - } else { - propertiesFilePattern = "**/" + configFileName + "-" + profile + ".properties"; - } - - //PROPERTIES - Resource[] propertiesResources = scanner.getResources(propertiesFilePattern); - - if (propertiesResources != null) { - includesResources.addAll(Arrays.asList(propertiesResources)); - } - - //YML - if (profile == null) { - propertiesFilePattern = "**/" + configFileName + ".yml"; - } else { - propertiesFilePattern = "**/" + configFileName + "-" + profile + ".yml"; - } - - Resource[] ymlResources = scanner.getResources(propertiesFilePattern); - if (ymlResources != null) { - includesResources.addAll(Arrays.asList(ymlResources)); - } - - //YAML - if (profile == null) { - propertiesFilePattern = "**/" + configFileName + ".yaml"; - } else { - propertiesFilePattern = "**/" + configFileName + "-" + profile + ".yaml"; - } - - Resource[] yamlResources = - scanner.getResources(propertiesFilePattern); - includesResources.addAll(Arrays.asList(yamlResources)); - - if (yamlResources != null) { - includesResources.addAll(Arrays.asList(ymlResources)); - } - - //JSON - if (profile == null) { - propertiesFilePattern = "**/" + configFileName + ".json"; - } else { - propertiesFilePattern = "**/" + configFileName + "-" + profile + ".json"; - } - - Resource[] jsonResources = - scanner.getResources(propertiesFilePattern); - includesResources.addAll(Arrays.asList(yamlResources)); - - if (jsonResources != null) { - includesResources.addAll(Arrays.asList(jsonResources)); + ymlResource = compileClassLoader.findResource("application-" + profile + ".yml"); + profileProperties = getPropertiesFromYamlResource(ymlResource, activeProfiles); + propertiesResource = getResourceFromClasspath(compileClassLoader, + "application-" + profile + ".properties"); + profileProperties.putAll(getPropertiesResource(propertiesResource)); + mergedProperties.putAll(profileProperties); } } diff --git a/core/src/test/java/io/fabric8/maven/core/util/SpringBootUtilTest.java b/core/src/test/java/io/fabric8/maven/core/util/SpringBootUtilTest.java index 0a5710e022..18b390be44 100644 --- a/core/src/test/java/io/fabric8/maven/core/util/SpringBootUtilTest.java +++ b/core/src/test/java/io/fabric8/maven/core/util/SpringBootUtilTest.java @@ -15,50 +15,27 @@ */ package io.fabric8.maven.core.util; +import java.util.*; + import io.fabric8.utils.PropertiesHelper; -import org.apache.maven.model.Build; -import org.apache.maven.project.MavenProject; -import org.codehaus.plexus.util.FileUtils; -import org.junit.Test; -import org.springframework.boot.Banner; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.core.env.MapPropertySource; -import org.springframework.core.env.PropertySource; -import org.springframework.util.ResourceUtils; -import java.io.File; -import java.io.IOException; -import java.net.URL; -import java.nio.file.Files; -import java.util.Collections; -import java.util.Properties; -import java.util.UUID; +import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; /** * Checking the behaviour of utility methods. */ public class SpringBootUtilTest { - @Test - public void testYamlToPropertiesParsing() throws Exception { - - MavenProject project = new MavenProject(); - Build build = new Build(); - - setMavenProject(project, build); - - URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/util/test-application.yml"); - - FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes", - "application.yml")); - Properties props = SpringBootUtil.getApplicationProperties(project, Collections.emptyList()); + @Test + public void testYamlToPropertiesParsing() { + Properties props = SpringBootUtil.getPropertiesFromYamlResource( + SpringBootUtilTest.class.getResource("/util/test-application.yml"), Collections.emptyList()); assertNotEquals(0, props.size()); assertEquals(new Integer(8081), PropertiesHelper.getInteger(props, "management.port")); @@ -71,162 +48,55 @@ public void testYamlToPropertiesParsing() throws Exception { } @Test - public void testYamlToPropertiesMerge() throws Exception { - - MavenProject project = new MavenProject(); - Build build = new Build(); - - setMavenProject(project, build); - - URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/util/test-application-merge-multi.yml"); + public void testYamlToPropertiesParsingWithActiveProfiles() { - FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes", - "application.yml"), "UTF-8", null, true); - - Properties props = SpringBootUtil.getApplicationProperties(project, Collections.emptyList()); + List activeProfiles = new ArrayList() {{ + add("dev"); + add("qa"); + }}; + Properties props = SpringBootUtil.getPropertiesFromYamlResource( + SpringBootUtilTest.class.getResource("/util/test-application-multi.yml"), activeProfiles); assertNotEquals(0, props.size()); assertEquals(new Integer(9090), PropertiesHelper.getInteger(props, "server.port")); assertEquals("Hello", props.getProperty("my.name")); - assertEquals("Foo", props.getProperty("their.name")); - } - - @Test - public void testWithDifferentConfigName() throws Exception { - - System.setProperty("spring.config.name", "foo"); - - MavenProject project = new MavenProject(); - Build build = new Build(); - - setMavenProject(project, build); - - URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/util/test-application-named.yml"); - - FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes", - "foo.yml"), "UTF-8", null, true); - - Properties props = SpringBootUtil.getApplicationProperties(project, Collections.emptyList()); - - assertNotEquals(0, props.size()); - - assertEquals(new Integer(9090), PropertiesHelper.getInteger(props, "server.port")); - assertEquals("Foo", props.getProperty("their.name")); - - System.getProperties().remove("spring.config.name"); + assertEquals("Hola!", props.getProperty("their.name")); } @Test - public void testPropertiesInclude() throws Exception { - - MavenProject project = new MavenProject(); - Build build = new Build(); - - setMavenProject(project, build); - - URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/util/test-application-include.yml"); + public void testYamlToPropertiesParsingWithActiveProfiles2() { - FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes", - "application.yml"), "UTF-8", null, true); - - SpringApplication sbBuilder = new SpringApplicationBuilder(AnnotationConfigApplicationContext.class) - .web(false) - .headless(true) - .bannerMode(Banner.Mode.OFF) - .build(); - - ConfigurableApplicationContext ctx = sbBuilder.run(); - - Properties props = SpringBootUtil.getApplicationProperties(project,Collections.emptyList()); + List activeProfiles = new ArrayList() {{ + add("qa"); + add("dev"); + }}; + Properties props = SpringBootUtil.getPropertiesFromYamlResource( + SpringBootUtilTest.class.getResource("/util/test-application-multi.yml"), activeProfiles); assertNotEquals(0, props.size()); - assertEquals(new Integer(2020), PropertiesHelper.getInteger(props, "my.port")); - assertEquals("bar", props.getProperty("my.name")); - assertEquals("foo", props.getProperty("name")); - } - - - @Test - public void testProfilePropertiesForDev() throws Exception { - - MavenProject project = new MavenProject(); - Build build = new Build(); - - setMavenProject(project, build); - - URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/util/test-application-multi.yml"); - - FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes", - "application.yml"), "UTF-8", null, true); - - Properties props = SpringBootUtil.getApplicationProperties(project,"dev"); - assertEquals(new Integer(8080), PropertiesHelper.getInteger(props, "server.port")); assertEquals("Hello", props.getProperty("my.name")); + assertEquals("Hola!", props.getProperty("their.name")); } @Test - public void testProfilePropertiesForQa() throws Exception { - - MavenProject project = new MavenProject(); - Build build = new Build(); - - setMavenProject(project, build); + public void testNonExistentYamlToPropertiesParsing() { - URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/util/test-application-multi.yml"); + Properties props = SpringBootUtil.getPropertiesFromYamlResource( + SpringBootUtilTest.class.getResource("/this-file-does-not-exist") + , Collections.emptyList()); + assertNotNull(props); + assertEquals(0, props.size()); - FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes", - "application.yml"), "UTF-8", null, true); - - Properties props = SpringBootUtil.getApplicationProperties(project,"qa"); - - assertNotEquals(0, props.size()); - - assertEquals(new Integer(9090), PropertiesHelper.getInteger(props, "server.port")); - assertEquals("Hola!", props.getProperty("their.name")); } -// // @Test TODO SK Remove this after Roland Review - this will not happen at all -// public void testNonExistentYamlToPropertiesParsing() throws Exception { -// -// Properties props = SpringBootUtil.getPropertiesFromYamlResource( -// SpringBootUtilTest.class.getResource("/this-file-does-not-exist") -// , null); -// -// MavenProject project = new MavenProject(); -// Build build = new Build(); -// -// setMavenProject(project, build); -// -// URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/this-file-does-not-exist"); -// -// FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes", -// "application.yml"), "UTF-8", null, true); -// -// Properties props = SpringBootUtil.getApplicationProperties(project,"qa"); -// assertNotNull(props); -// assertEquals(0, props.size()); -// -// } - @Test - public void testPropertiesParsing() throws Exception { - - MavenProject project = new MavenProject(); - Build build = new Build(); - - setMavenProject(project, build); - - URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/util/test-application.properties"); - - FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes", - "application.properties"), "UTF-8", null, true); - - Properties props = SpringBootUtil.getApplicationProperties(project,Collections.emptyList()); - + public void testPropertiesParsing() { + Properties props = SpringBootUtil.getPropertiesResource( + SpringBootUtilTest.class.getResource("/util/test-application.properties")); assertNotEquals(0, props.size()); assertEquals(new Integer(8081), PropertiesHelper.getInteger(props, "management.port")); @@ -236,21 +106,13 @@ public void testPropertiesParsing() throws Exception { } -// @Test TODO SK Remove this after Roland Review -// public void testNonExistentPropertiesParsing() throws IOException { -// -// Properties props = SpringBootUtil.getPropertiesResource(SpringBootUtilTest.class.getResource( -// "/this-file-does-not-exist"), null); -// assertNotNull(props); -// assertEquals(0, props.size()); -// } + @Test + public void testNonExistentPropertiesParsing() { + + Properties props = SpringBootUtil.getPropertiesResource(SpringBootUtilTest.class.getResource("/this-file-does-not-exist")); + assertNotNull(props); + assertEquals(0, props.size()); - public void setMavenProject(final MavenProject project, final Build build) throws IOException { - //Set Build Dir - final String outputTempDir = Files.createTempDirectory(UUID.randomUUID().toString()).toFile().getAbsolutePath(); - new File(outputTempDir).mkdirs(); - build.setOutputDirectory(outputTempDir); - project.setBuild(build); } } diff --git a/core/src/test/resources/util/test-application-include.yml b/core/src/test/resources/util/test-application-include.yml deleted file mode 100644 index 456275f76e..0000000000 --- a/core/src/test/resources/util/test-application-include.yml +++ /dev/null @@ -1,14 +0,0 @@ ---- -spring: - active: - profiles: foo - profiles: - include: bar -name: foo - ---- -my: - name: bar - port: 2020 -spring: - profiles: bar \ No newline at end of file diff --git a/core/src/test/resources/util/test-application-merge-multi.yml b/core/src/test/resources/util/test-application-merge-multi.yml deleted file mode 100644 index 4f0328a7f9..0000000000 --- a/core/src/test/resources/util/test-application-merge-multi.yml +++ /dev/null @@ -1,9 +0,0 @@ ---- -my: - name: "Hello" - ---- -server: - port: 9090 -their: - name: Foo diff --git a/core/src/test/resources/util/test-application-multi.yml b/core/src/test/resources/util/test-application-multi.yml index 6bca6cd1df..6efadbc32f 100644 --- a/core/src/test/resources/util/test-application-multi.yml +++ b/core/src/test/resources/util/test-application-multi.yml @@ -1,3 +1,7 @@ +spring: + profiles: + active: dev,qa + --- spring: diff --git a/core/src/test/resources/util/test-application-named.yml b/core/src/test/resources/util/test-application-named.yml deleted file mode 100644 index f54c389134..0000000000 --- a/core/src/test/resources/util/test-application-named.yml +++ /dev/null @@ -1,4 +0,0 @@ -server: - port: 9090 -their: - name: Foo \ No newline at end of file diff --git a/enricher/fabric8/src/test/java/io/fabric8/maven/enricher/fabric8/AbstractSpringBootHealthCheckEnricherSupport.java b/enricher/fabric8/src/test/java/io/fabric8/maven/enricher/fabric8/AbstractSpringBootHealthCheckEnricherSupport.java index 3520e0cea5..5491316165 100644 --- a/enricher/fabric8/src/test/java/io/fabric8/maven/enricher/fabric8/AbstractSpringBootHealthCheckEnricherSupport.java +++ b/enricher/fabric8/src/test/java/io/fabric8/maven/enricher/fabric8/AbstractSpringBootHealthCheckEnricherSupport.java @@ -374,7 +374,7 @@ public boolean hasAllClasses(MavenProject project, String ... classNames) { private void withProjectProperties(final Properties properties) { new MockUp() { @Mock - public Properties getSpringBootApplicationProperties(MavenProject project) { + public Properties getApplicationProperties(MavenProject project, String activeProfiles) { return properties; } }; diff --git a/generator/spring-boot/src/main/java/io/fabric8/maven/generator/springboot/SpringBootGenerator.java b/generator/spring-boot/src/main/java/io/fabric8/maven/generator/springboot/SpringBootGenerator.java index 85a74b5145..90a2f61990 100644 --- a/generator/spring-boot/src/main/java/io/fabric8/maven/generator/springboot/SpringBootGenerator.java +++ b/generator/spring-boot/src/main/java/io/fabric8/maven/generator/springboot/SpringBootGenerator.java @@ -62,9 +62,7 @@ public class SpringBootGenerator extends JavaExecGenerator { private static final String DEFAULT_SERVER_PORT = "8080"; public enum Config implements Configs.Key { - color {{ - d = "false"; - }}, + color {{ d = "false"; }}, // comma separated list of spring boot profile(s) that would be passed set as -Dspring.profiles.active activeProfiles; @@ -101,7 +99,8 @@ public List customize(List configs, bool protected Map getEnv(boolean prePackagePhase) throws MojoExecutionException { Map res = super.getEnv(prePackagePhase); if (getContext().isWatchMode()) { - Properties properties = getSpringBootProperties(); + String strActiveProfiles = getContext().getConfig().getConfig("spring-boot", "activeProfiles"); + Properties properties = SpringBootUtil.getApplicationProperties(getProject(), strActiveProfiles); // adding dev tools token to env variables to prevent override during recompile String secret = properties.getProperty(SpringBootConfigurationHelper.DEV_TOOLS_REMOTE_SECRET); if (secret != null) { @@ -144,8 +143,12 @@ protected boolean isFatJar() throws MojoExecutionException { protected List extractPorts() { List answer = new ArrayList<>(); - Properties properties = getSpringBootProperties(); - //TODO SK - do we need to handle the parsing of port properties like ${PORT:1234} + String strActiveProfiles = getConfig(activeProfiles); + + Properties properties = SpringBootUtil.getApplicationProperties(getContext().getProject(), + SpringBootUtil.getActiveProfiles(strActiveProfiles)); + + //TODO SK - do we need to handle the parsin of port properties like ${PORT:1234} SpringBootConfigurationHelper propertyHelper = new SpringBootConfigurationHelper(SpringBootUtil.getSpringBootVersion(getProject())); String port = properties.getProperty(propertyHelper.getServerPortPropertyKey(), DEFAULT_SERVER_PORT); addPortIfValid(answer, getConfig(JavaExecGenerator.Config.webPort, port)); @@ -157,7 +160,10 @@ protected List extractPorts() { // ============================================================================= private void ensureSpringDevToolSecretToken() throws MojoExecutionException { - Properties properties = getSpringBootProperties(); + String strActiveProfiles = getConfig(activeProfiles); + + Properties properties = SpringBootUtil.getApplicationProperties(getContext().getProject(), + SpringBootUtil.getActiveProfiles(strActiveProfiles)); String remoteSecret = properties.getProperty(DEV_TOOLS_REMOTE_SECRET); if (Strings.isNullOrEmpty(remoteSecret)) { addSecretTokenToApplicationProperties(); @@ -305,15 +311,6 @@ private boolean isSpringBootRepackage() { return false; } - private Properties getSpringBootProperties() { - try { - String strActiveProfiles = getContext().getConfig().getConfig("spring-boot", "activeProfiles"); - return SpringBootUtil.getApplicationProperties(getProject(), strActiveProfiles); - } catch (IOException e) { - throw new IllegalStateException("Failed to load Spring Boot properties", e); - } - } - private File getSpringBootDevToolsJar() throws IOException { String version = SpringBootUtil.getSpringBootDevToolsVersion(getProject()); if (version == null) { diff --git a/generator/spring-boot/src/test/java/io/fabric8/maven/generator/springboot/SpringBootGeneratorTest.java b/generator/spring-boot/src/test/java/io/fabric8/maven/generator/springboot/SpringBootGeneratorTest.java index 042282911d..e4b2b3ec9a 100644 --- a/generator/spring-boot/src/test/java/io/fabric8/maven/generator/springboot/SpringBootGeneratorTest.java +++ b/generator/spring-boot/src/test/java/io/fabric8/maven/generator/springboot/SpringBootGeneratorTest.java @@ -39,6 +39,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; /** * @author roland diff --git a/parent/pom.xml b/parent/pom.xml index c3ec753930..29a3863c5f 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -62,7 +62,6 @@ 0.7.8 3.0.12 3.2.0 - 1.5.3.RELEASE 0.0.13 0.22.1 0.1.7 diff --git a/watcher/standard/src/main/java/io/fabric8/maven/watcher/standard/SpringBootWatcher.java b/watcher/standard/src/main/java/io/fabric8/maven/watcher/standard/SpringBootWatcher.java index a10a891a77..708c16533d 100644 --- a/watcher/standard/src/main/java/io/fabric8/maven/watcher/standard/SpringBootWatcher.java +++ b/watcher/standard/src/main/java/io/fabric8/maven/watcher/standard/SpringBootWatcher.java @@ -172,12 +172,7 @@ private void runRemoteSpringApplication(String url) { log.info("Running RemoteSpringApplication against endpoint: " + url); String strActiveProfiles = getContext().getConfig().getConfig("spring-boot", "activeProfiles"); - Properties properties = null; - try { - properties = SpringBootUtil.getApplicationProperties(getContext().getProject(), strActiveProfiles); - } catch (IOException e) { - throw new IllegalStateException("Failed to load Spring Boot properties",e); - } + Properties properties = SpringBootUtil.getApplicationProperties(getContext().getProject(), strActiveProfiles); String remoteSecret = properties.getProperty(DEV_TOOLS_REMOTE_SECRET, System.getProperty(DEV_TOOLS_REMOTE_SECRET)); if (Strings.isNullOrBlank(remoteSecret)) {