Using Maven or Spring Boot?

Asked

Viewed 711 times

2

In my researches I could understand that there is how to perform application Profiling (e.g. 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?

2 answers

3


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 model applications-{profile}.properties

For example, we have a file for configuration of the production environment application-prd.properties and a file for configuration of the type-approval environment application-hml.properties

And for the application to use the 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’ve been able to run our application on every profile we have. But note that this spring boot Profiling feature is to be used at runtime (Runtime) and does not allow you to do Packaging already on a specific profile. It is in this case that the Maven’s Profiling enters 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 that’s not enough, all we did there was create a "variable" called activatedProperties that depending on the profile receives a value. Before 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 we created and we will inform there the spring variable spring.profiles.active, the one we were setting up during the execution mode, and pass this "variable" activatedProperties. Our archive application-properties.xml would look like this:

spring.profiles.active=@activatedProperties@

Maven will not replace the "variable" delimited between the @s until we inform it to filter those files. Thus, also on pom.xml we’ll add one <resource>:

<build>

    ...

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

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

mvn package -P{profile}

For example:

mvn package -Pprd

When we execute this command the following will basically happen:

  • Commando mvn package -Pprd is executed.
  • Maven will see which profile we are requesting through -P{profile} which is the profile prd.
  • Maven will find the value of the variable activatedProperties of that profile.
  • The Maven will go scan all files within src/main/resources and identify in the file application.properties the delimiter @activatedProperties@ and will literally replace the value by the variable activatedProperties.
  • Spring Boot will run
  • Spring Boot will read the default configuration file application.properties found.
  • In this file there will be the following configuration: spring.profiles.active=prd and, with it, the file application-prd.properties will be read.

1

You can also configure in just one application.yml

spring:
  profiles:
    active: ${activatedProperties}

---

spring:
  profiles: homolog    
logging:
  file: /springLogs/autenticador.log
  level:
    org:
      hibernate:
        SQL: debug
  pattern:
    console: '%d{yyyy-MM-dd HH:mm:ss} %-5level %msg%n'

---

spring:
  profiles: production    
logging:
  file: E:/java/logs/wildfly-10.0.0/autenticador/autenticador.log
  level:
    org:
      hibernate:
        SQL: debug
  pattern:
    console: '%d{yyyy-MM-dd HH:mm:ss} %-5level %msg%n'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.