0

I'm using Spring Boot 1.4.3.RELEASE and gradle 2.13 for developing an api. On local environment, 'local' profile is working perfectly, but I'm getting issue while deploying code in Linux/Unix server.

Here is my application.yml with two profiles : dev and local

---

spring:
  profiles:
    active: dev

server:
  context-path: /api/v1
  port: 8080

build:
  version: 1.0.0

cache:
  storage:
    path: '/home/user/content/storage/'


---

spring:
  profiles:
    active: local

server:
  context-path: /content-delivery/api/v1
  port: 8081

build:
  version: 1.0.0

cache:
  storage:
    path: '/Yogen/api/code/cachedData'

The command I'm using to deploy my war file is:

jdk1.8.0_112/bin/java -jar _-Dspring.profiles.active=dev content-delivery.jar

When I run my war it was working fine with only one profile, but once i added another profile, I'm getting values get mixed up causing error as below:

    01:56:16.557 INFO  ContentDeliveryApplication - The following profiles are active: dev
............
02:00:48.182 INFO  PropertyPlaceholderConfigurer - Loading properties file from file [/home/542596/content-api/resources/app-dev.properties]
    02:00:51.939 INFO  PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.ws.config.annotation.DelegatingWsConfiguration' of type [class org.springframework.ws.config.annotation.DelegatingWsConfiguration$$EnhancerBySpringCGLIB$$290c5e2d] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    02:00:55.736 INFO  AnnotationActionEndpointMapping - Supporting [WS-Addressing August 2004, WS-Addressing 1.0]
    02:01:25.559 INFO  PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [class org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$a08e9c2b] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    02:01:56.522 INFO  TomcatEmbeddedServletContainer - Tomcat initialized with port(s): 8081 (http)
    02:01:57.669 INFO  StandardService - Starting service Tomcat
    02:01:57.877 INFO  StandardEngine - Starting Servlet Engine: Apache Tomcat/8.5.6
    02:02:11.228 INFO  [/content-delivery/api/v1] - Initializing Spring embedded WebApplicationContext
    02:02:11.228 INFO  ContextLoader - Root WebApplicationContext: initialization completed in 353263 ms
    02:02:19.412 INFO  ServletRegistrationBean - Mapping servlet: 'dispatcherServlet' to [/]
    02:02:19.517 INFO  ServletRegistrationBean - Mapping servlet: 'messageDispatcherServlet' to [/services/*]
    02:02:19.829 INFO  FilterRegistrationBean - Mapping filter: 'metricsFilter' to: [/*]
    02:02:19.829 INFO  FilterRegistrationBean - Mapping filter: 'characterEncodingFilter' to: [/*]
    02:02:19.829 INFO  FilterRegistrationBean - Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
    02:02:19.829 INFO  FilterRegistrationBean - Mapping filter: 'httpPutFormContentFilter' to: [/*]
    02:02:19.829 INFO  FilterRegistrationBean - Mapping filter: 'requestContextFilter' to: [/*]
    02:02:19.829 INFO  FilterRegistrationBean - Mapping filter: 'webRequestLoggingFilter' to: [/*]
    02:02:19.829 INFO  FilterRegistrationBean - Mapping filter: 'applicationContextIdFilter' to: [/*]
    02:02:46.283 ERROR EhcacheManager - Initialize failed.
    02:02:46.284 WARN  AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jsonObjectCacheManager': Invocation of init method failed; nested exception is org.ehcache.StateTransitionException: Directory couldn't be created: /Yogen/api/code/cachedData/cachedData
    02:02:46.285 INFO  StandardService - Stopping service Tomcat
    02:02:47.528 INFO  AutoConfigurationReportLoggingInitializer -

    Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
    02:02:48.103 ERROR SpringApplication - Application startup failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jsonObjectCacheManager': Invocation of init method failed; nested exception is org.ehcache.StateTransitionException: Directory couldn't be created: /Yogen/api/code/cachedData/cachedData
            at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]

The PropertyPlaceholderConfigurer has been configured as follows:

@Configuration
public class PropertyConfiguration {

    @Bean
    @Profile("dev")
    public static PropertyPlaceholderConfigurer developmentPropertyPlaceholderConfigurer() {
        PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
        configurer.setLocation(new FileSystemResource("/home/542596/content-api/resources/app-dev.properties"));
        configurer.setIgnoreUnresolvablePlaceholders(true);
        return configurer;
}
}

The console shows that properties file being read is correct also the profile is 'dev', but the port started and context-path and other values are being fetched from 'local' profile instead 'dev' profile.

What I'm missing?? Thanks.

Yogen Rai
  • 2,961
  • 3
  • 25
  • 37
  • Have you tried in that yml file to just use `spring.profiles: dev` instead of `spring.profiles.active: dev`? Same for the local one – Magd Kudama Jan 14 '17 at 02:45
  • You shouldn't be registering your own `PropertyPlaceholderConfigurer` use the spring boot provided features instead of working around them. – M. Deinum Jan 15 '17 at 14:15
  • @MagdKudama it didn't work and it shouldn't work because I've already registered as 'active' and key 'spring.profiles.active:' can't be mapped as 'spring.profiles:' :) – Yogen Rai Jan 16 '17 at 04:18
  • @M.Deinum In Github issue [#6457](https://github.com/spring-projects/spring-boot/issues/6457), snicoll has clearly mentioned we can use `PropertyPlaceholderConfigurer` instead `PropertySourcesPlaceholderConfigurer` :) and it suits me very well :) – Yogen Rai Jan 16 '17 at 04:26
  • I nowhere said you couldn't... I say you shouldn't and better use the default way of loading files using `@PropertySource` instead. – M. Deinum Jan 16 '17 at 07:48

3 Answers3

1

Your profiles are getting overridden.

Better Boostrap your application using bootstrap.yml

and have two application.yml as follows :

application-dev.yml
application-local.yml

set profiles in bootstrap.yml to decide which property file to load:

    spring:
         profiles:
             active : dev # choose which application properties to load dev/local
Barath
  • 5,093
  • 1
  • 17
  • 42
  • Thank you @Barath, i separated the environment configs on their corresponding files and it worked for me. But I don't want to use `bootstrap.yml` because i need to work on externalization later :) – Yogen Rai Jan 16 '17 at 04:29
0

Like @Barath stated, you cannot separate profile properties within a single application.yml file. Create a yml file for each profile, name it as follows

application-{profile}.yml

I would not use bootstrap.yml to specify which profile to load as you'll want to externalize this if possible, so passing it to the VM via -Dspring.profiles.active=dev would be preferable.

lane.maxwell
  • 5,002
  • 1
  • 20
  • 30
0

We also have another option similar to @Maxwell answer instead of passing the profile through VM property :

you can have following files:

application.yml 
application-dev.yml
application-local.yml

And define profile in application.yml :

spring:
    profiles:
       active : dev 

This ensures loading of application.yml as well as application-dev.yml.

Barath
  • 5,093
  • 1
  • 17
  • 42