0

i have a Spring Boot application. I need to connect spring boot profile to maven profile so than when i am calling command

mvn clean install -Pdev 

or

mvn clean install -Pprod

it should call spring boot to load application-dev.yml or application-prod.yml. And when i call

mvn clean install

it should call application-dev.yml file before startup. So spring boot dev profile needs to be called from 2 commands. I have a problem every time i switch profile it builds application from default profile. So can you please help me to wire maven and spring boot profile. Here is my pom.xml

    <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
                <profiles>${spring-profiles}</profiles>
            </configuration>
        </plugin>
    </plugins>
</build>
<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <spring-profiles>dev</spring-profiles>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <spring-profiles>prod</spring-profiles>
        </properties>
    </profile>
</profiles>

Here is application.yml example.

spring:
devtools:
    restart :
        enabled: true
    livereload:
        enabled: false  
datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    database: MYSQL
    show_sql: true
    properties:
        hibernate.cache.use_second_level_cache: true
        hibernate.cache.use_query_cache: false
        hibernate.generate_statistics: true
        hibernate.cache.region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
    hibernate:
        ddl-auto: update


server:
port: 8080
compression:
  enabled: true
  mime-types : application/json,application/xml,text/html,text/xml,text/plain,application/javascript,text/css,text/html
session :
  timeout : 540000
mail:
smtp:
  starttls.enable: true
  auth: true
  port: 587
activation:
  expiration.hours: 24
  template: /mails/activationEmail.html
change-password:
  template: /mails/passwordResetEmail.html

Difference between application-dev.yml, application-prod.yml is only in port. So that I only change port when i change mvn profile before deploy.

1 Answers1

2

I do not understand everything you want but you have two options to get some stuff working.

First, in one of your maven profiles you can set one as per default:

<profile>
   <id>dev</id>
   <activation>
       <activeByDefault>true</activeByDefault>
   </activation>

So there is no need to give a maven profile for mvn clean install

The other option is to set variables to your profile and get those values to your application.properties file when building the app.

For example for your port you can go like that:

<profile>
    <id>dev</id>
    <properties>
        <serverPort>9999</serverPort>
    </properties>
</profile>

And in your application.properties file you can get the value by using @serverPort:

server.port=@serverPort@

If you now build your app by using any profile you will get the value set in your maven profile.

Graham
  • 7,431
  • 18
  • 59
  • 84
Patrick
  • 12,336
  • 15
  • 73
  • 115
  • i want to switch spring boot profiles via switching maven profiles. So when i build project for example with maven prod profile. It uses spring boot profile with name prod to build project by using application-prod.ymp prop file. – Bohdan Levkovych Jul 17 '17 at 11:39
  • yes, this is exactly what is described in documentation. – Patrick Jul 17 '17 at 11:50