Vetouz
Vetouz

Reputation: 159

Spring load prod profile

I have a Spring application that I have to deploy in production. Unfortunately I am unable to turn the prod profile on. I have an application-dev.yml and application-prod.yml file as well as application.yml. My application.yml is as follows:

# ===================================================================
# Spring Boot configuration.
#
# This configuration will be overridden by the Spring profile you use,
# for example application-dev.yml if you use the "dev" profile.
#
# More information on profiles: http://www.jhipster.tech/profiles/
# More information on configuration properties: http://www.jhipster.tech/common-application-properties/
# ===================================================================

# ===================================================================
# Standard Spring Boot properties.
# Full reference is available at:
# http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
# ===================================================================


eureka:
    client:
        enabled: true
        healthcheck:
            enabled: true
        fetch-registry: true
        register-with-eureka: true
        instance-info-replication-interval-seconds: 10
        registry-fetch-interval-seconds: 10
    instance:
        appname: majurca
        instanceId: majurca:${spring.application.instance-id:${random.value}}
        lease-renewal-interval-in-seconds: 5
        lease-expiration-duration-in-seconds: 10
        status-page-url-path: ${management.context-path}/info
        health-check-url-path: ${management.context-path}/health
        metadata-map:
            zone: primary # This is needed for the load balancer
            profile: ${spring.profiles.active}
            version: ${info.project.version}
ribbon:
    eureka:
        enabled: true
management:
    security:
        roles: ADMIN
    context-path: /management
    info:
        git:
            mode: full
    health:
        mail:
            enabled: false # When using the MailService, configure an SMTP server and set this to true
spring:
    profiles:
        default: prod
        active: prod
    application:
        name: majurca
    jackson:
        serialization.write_dates_as_timestamps: false
    jpa:
        open-in-view: false
        hibernate:
            ddl-auto: none
            naming:
                physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
                implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
    messages:
        basename: i18n/messages
    mvc:
        favicon:
            enabled: false
    thymeleaf:
        mode: XHTML
security:
    basic:
        enabled: false

server:
    session:
        cookie:
            http-only: true

info:
    project:
        version: #project.version#

# ===================================================================
# JHipster specific properties
#
# Full reference is available at: http://www.jhipster.tech/common-application-properties/
# ===================================================================

jhipster:
    async:
        core-pool-size: 2
        max-pool-size: 50
        queue-capacity: 10000
    # By default CORS is disabled. Uncomment to enable.
    #cors:
        #allowed-origins: "*"
        #allowed-methods: "*"
        #allowed-headers: "*"
        #exposed-headers: "Authorization,Link,X-Total-Count"
        #allow-credentials: true
        #max-age: 1800
    mail:
        from: majurca@localhost
    swagger:
        default-include-pattern: /api/.*
        title: majurca API
        description: majurca API documentation
        version: 0.0.1
        terms-of-service-url:
        contact-name:
        contact-url:
        contact-email:
        license:
        license-url:
    ribbon:
        display-on-active-profiles: dev

# ===================================================================
# Application specific properties
# Add your own application properties here, see the ApplicationProperties class
# to have type-safe configuration, like in the JHipsterProperties above
#
# More documentation is available at:
# http://www.jhipster.tech/common-application-properties/
# ===================================================================

application:
    forms:
        register:
            autocomplete-cp-nbitems: 20


google:
  recaptcha:
    url: https://www.google.com/recaptcha/api/siteverify
    secret: 6Lci63UUAAAAAIsluk6G3ueNiJ_ET0m4luMsC8O5
    key: 6Lci63UUAAAAADFyeZFTkEaHshVK2LQgYV63PPD_

    #key: 6LfUG3UUAAAAAMxScj7ZFY-OXedoWLRzl0wryrrF
    #secret: 6LfUG3UUAAAAANsSIsCFOi3X9uYzS72De8EqKqNM

So you can see I have spring.profiles.default and spring.pofiles.active set to prod. I also tried to build the app with maven and the option -Dspring.profiles.active=prod but despite this each time I run the war generated it says The following profiles are active: swagger,dev.

Anyone has a idea why the prod profile is not loaded ? Thanks in advance.

Upvotes: 1

Views: 735

Answers (2)

Mark Bramnik
Mark Bramnik

Reputation: 42491

I've verified on spring-boot 2.1.4 the following configurations:

application.yaml:

spring:
 profiles:
   active: dev

application-dev.yaml:

sample:
  value: development

application-prod.yaml:

sample:
  value: production

And the following (very simple) spring boot application:

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.event.EventListener;

@SpringBootApplication
public class DemoApplication {

    @Value("${sample.value}")
    private String sampleValue;
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);

    }

    @EventListener
    public void onAppStarted(ApplicationStartedEvent evt) {
        System.out.println("Sample Value: " + sampleValue);
    }

}

This configuration works, even if I don't supply any flags: java -jar myapp.jar

So there must be something else here, related to your code.

Its hard to say whats wrong without seeing the application, however I did found one "suspicious" statement:

You say, that no matter what you try:

The following profiles are active: swagger,dev

Now where does the 'swagger' profile come from? I don't see any reference to it. If you run the application with java -jar myapp.jar one possibility I can see is that there is somewhere spring.factories file that defines an EnvironmentPostProcessor - its a hook where you can "fiddle" with profiles and, among other things add active profiles "manually" (in code).

So please check this possibility, but again - what you've done is correct in spring boot (well, technically you don't need spring.profiles.default entry but it doesn't harm)

Upvotes: 1

Ken Chan
Ken Chan

Reputation: 90457

Spring Boot can load the same properties from many locations , which you can refer its loading order in the docs at here.

The upper part in the list have a higher loading priority than the lower part.That means what you define in the application-xxx.yml can be overridden by the same properties that are also defined in the OS env variable or JVM properties or etc .....

As the command line arguments has a pretty high loading order , which means basically you can try to use it to set the profile by adding --spring.profiles.active=prod to the command that start the application such as :

$ java -jar myproject.jar --spring.profiles.active=prod

Upvotes: 0

Related Questions