diff --git a/core/src/main/java/io/fabric8/maven/core/util/DummySpringBootApplication.java b/core/src/main/java/io/fabric8/maven/core/util/DummySpringBootApplication.java new file mode 100644 index 0000000000..0076abcabb --- /dev/null +++ b/core/src/main/java/io/fabric8/maven/core/util/DummySpringBootApplication.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2016 Red Hat, Inc. + * + * Red Hat licenses this file to you under the Apache License, version + * 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package io.fabric8.maven.core.util; + +/** + * @author kameshs + */ +public class DummySpringBootApplication { + public static void main(String[] args) { + //nothing here... + } +} 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 37cd769a21..0c24a286b8 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 @@ -26,7 +26,6 @@ import org.springframework.boot.env.PropertySourcesLoader; import org.springframework.boot.env.YamlPropertySourceLoader; 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.core.io.Resource; @@ -71,29 +70,34 @@ public static Properties runAndLoadPropertiesUsingEnv(List activeProfile SpringApplication sbBuilder; if (activeProfiles != null && activeProfiles.size() > 0) { - sbBuilder = new SpringApplicationBuilder(AnnotationConfigApplicationContext.class) + 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(AnnotationConfigApplicationContext.class) + sbBuilder = new SpringApplicationBuilder(DummySpringBootApplication.class) .web(false) .headless(true) .bannerMode(Banner.Mode.OFF) .build(); } - ConfigurableApplicationContext ctx = sbBuilder.run(); - Properties applicationProperties = new Properties(); + try { + ConfigurableApplicationContext ctx = sbBuilder.run(); + + applicationProperties = new Properties(); - for (PropertySource propertySource : ctx.getEnvironment().getPropertySources()) { + for (PropertySource propertySource : ctx.getEnvironment().getPropertySources()) { - if (propertySource != null && propertySource instanceof MapPropertySource) { - applicationProperties.putAll(((MapPropertySource) propertySource).getSource()); + if (propertySource != null && propertySource instanceof MapPropertySource) { + applicationProperties.putAll(((MapPropertySource) propertySource).getSource()); + } } + } catch (Exception e) { + //swallow it .. } return applicationProperties; 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 10412a57d6..0a5710e022 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 @@ -130,6 +130,14 @@ public void testPropertiesInclude() throws Exception { 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()); assertNotEquals(0, props.size()); 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 993711c2f8..85a74b5145 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 @@ -121,7 +121,13 @@ protected List getExtraJavaOptions() { //Spring boot active profiles String strActiveProfiles = getConfig(activeProfiles); if (strActiveProfiles != null) { - opts.add("-Dspring.profiles.active=\"" + strActiveProfiles + "\""); + //String[] profiles = strActiveProfiles.split(","); + //Quoting is having issue with spring boot considering the string with quotes as profile name + //if (profiles.length > 1) { + opts.add("-Dspring.profiles.active=" + strActiveProfiles); + //} else { + // opts.add("-Dspring.profiles.active=" + strActiveProfiles); + //} } return opts; } 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 16ae111039..042282911d 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 @@ -90,7 +90,7 @@ public void javaOptionsWithActiveProfiles() throws IOException, MojoExecutionExc Map env = configs.get(0).getBuildConfiguration().getEnv(); String javaOptions = env.get("JAVA_OPTIONS"); assertNotNull(javaOptions); - assertTrue(javaOptions.equals("-Dspring.profiles.active=\"dev,qa\"")); + assertTrue(javaOptions.equals("-Dspring.profiles.active=dev,qa")); } @Test @@ -105,7 +105,7 @@ public void javaOptionsWithActiveProfileWithSpaces() throws IOException, MojoExe Map env = configs.get(0).getBuildConfiguration().getEnv(); String javaOptions = env.get("JAVA_OPTIONS"); assertNotNull(javaOptions); - assertTrue(javaOptions.equals("-Dspring.profiles.active=\"dev, qa\"")); + assertTrue(javaOptions.equals("-Dspring.profiles.active=dev, qa")); }