Use profiling of Maven or Spring Boot?

1

In my research I could understand that there is a way to perform application profiling (eg homologation and production) in two ways, via maven and spring-boot. The big question is that I have multiple profiles and would like to package each one correctly. Which one should I use? How should I use this feature?

    
asked by anonymous 28.03.2018 / 16:14

1 answer

1

In fact, if you already want to package in a specific profile, you will use both.

The first way is through Spring Boot itself, creating several .properties files in the applications-{profile}.properties

For example, we have a file to setup the production environment application-prd.properties and a file to setup the environment application-hml.properties

And for the application to use production or environment settings, it should be added in the java command:

java -jar -Dspring.profiles.active={profile} aplicacao.jar

For example:

java -jar -Dspring.profiles.active=prd aplicacao.jar

So far we have been able to run our application on every profile we have. But notice that this spring boot profiling feature is to be used in runtime and does not allow you to make packaging already in a specific profile. In this case, you enter the maven profiling in the file pom.xml :

<profiles>
        <profile>
            <id>lcl</id>
            <properties>
                <activatedProperties>lcl</activatedProperties>
            </properties>
        </profile>
        <profile>
            <id>hml</id>
            <properties>
                <activatedProperties>hml</activatedProperties>
            </properties>
        </profile>
        <profile>
            <id>prd</id>
            <properties>
                <activatedProperties>prd</activatedProperties>
            </properties>
        </profile>
</profiles>

But this is not enough, all we did there was to create a "variable" called activatedProperties that depending on the profile it receives a value. Given that, we will create a application.properties , which is the default configuration file for reading Spring Boot and will be read in place of the other 3 that we created and we will inform there the variable of spring spring.profiles.active , which we were configuring during the execution mode, and pass that "%" variable activatedProperties . Our application-properties.xml file would look like this:

spring.profiles.active=@activatedProperties@

Maven will not replace the "variable" delimited between @s until we tell it to filter these files. Thus, in% w /% will also add% w /%:

<build>

    ...

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

Ready, now it is. We can do the package through the command:

mvn package -P{profile}

For example:

mvn package -Pprd

When we execute this command, basically the following will happen:

  • Command pom.xml is executed.
  • Maven will see what profile we are requesting through <resource> which is the profile mvn package -Pprd .
  • Maven will find out the value of the -P{profile} of that profile.
  • Maven will scan all files within prd and will identify in the activatedProperties file the src/main/resources delimiter and will literally replace the application.properties variable.
  • Spring Boot will run
  • Spring Boot will read the default configuration file @activatedProperties@ found.
  • In this file there will be the following configuration: activatedProperties and with this, the file application.properties will be read.
28.03.2018 / 16:14