zeroTutorials

Java, Server, Database Tutorials

Have a Question?

If you have any question you can ask below or enter what you are looking for!

Dual jar/war build, and deployment into Tomcat for Spring Boot project using maven profiles

The goal of this tutorial is to set up a maven and Spring Boot configuration allowing to build and package the project as jar file as well as war file which can be deployed under an application server like Tomcat.

Through this tutorial, we will be able to build and package the Spring boot project as a jar file for a dev profile and package the project as a war file which can be deployed under an application server like Tomcat for the prod profile.

1. Properties files

At first, we will create properties files under the src/main/resources directory.

  • The application.properties file represents the default properties file.
  • The application-dev.properties file represents the properties file for the dev profile.
  • The application-prod.properties file represents the properties file for the dev profile.

2. Maven profiles

2.1. Add the project.packaging variable in the pom.xml file

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.4.RELEASE</version>
  <relativePath/>
</parent>
<groupId>com.package</groupId>
<artifactId>project-id</artifactId>
<version>1.0.0</version>
<name>project-java</name>

<packaging>${project.packaging}</packaging>

2.2. Declare the dev profile and prod profile in pom.xml file

  • In dev profile :
    • We will assign the jar value at the project.packaging level and the war value for the prod profile.
    • We add maven dependecy spring-boot-starter-aop.
    • We introduce the activatedProperties variable which is used to link the active maven profile with the Spring Boot profile. We assign dev value for this attribute in dev profile.
    • We declare MainJavaApplicationDev as main class in spring-boot-maven-plugin plugin.
  • In prod profile :
    • We will assign the war value at the project.packaging level and the war value for the prod profile.
    • We add maven dependecy spring-boot-starter-tomcat with provided scope.
    • We assign prod value for the activatedProperties attribute in prod profile.
    • We declare MainJavaApplicationProd as main class in spring-boot-maven-plugin plugin.
<profiles>
  <profile>
     <id>dev</id>
     <activation>
       <activeByDefault>true</activeByDefault>
     </activation>
     <properties>
        <project.packaging>jar</project.packaging>
        <activatedProperties>dev</activatedProperties>
     </properties>
     <dependencies>
        <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
     </dependencies>
     <build>
        <finalName>api</finalName>
        <plugins>
           <plugin>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
              <configuration>
                 <mainClass>com.package.MainJavaApplicationDev</mainClass>
              </configuration>
           </plugin>
        </plugins>
     </build>
  </profile>

  <profile>
     <id>prod</id>
     <properties>
        <project.packaging>war</project.packaging>
        <activatedProperties>prod</activatedProperties>
     </properties>
     <dependencies>
        <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-tomcat</artifactId>
           <scope>provided</scope>
        </dependency>
     </dependencies>
     <build>
        <finalName>api</finalName>
        <plugins>
           <plugin>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
              <configuration>
                 <mainClass>com.package.MainJavaApplicationProd</mainClass>
              </configuration>
           </plugin>
        </plugins>
     </build>
  </profile>
</profiles>

you must allow Maven to manipulate resource files as part of the build process. To do this, enable filtering for the Resources plugin in pom.xml.

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

3. Configuration of application.properties file

In the application.properties file, assign the activatedProperties variable which has already been added at the pom.xml to the spring.profiles.active variable.

# Link active maven profile with Spring Boot Profile
spring.profiles.active=@activatedProperties@

4. Java classes

Implement the 2 Main classes :

  • MainJavaApplicationDev: The Main class for the dev profile. The dev main class must be annotated with @Profile(“dev”) annotation.
  • MainJavaApplicationProd: The Main class for the Prod profile. It should be noted that the MainJavaApplicationProd class must extend the SpringBootServletInitializer class in order to allow the war project to run on a Tomcat server. The prod main class must be annotated with @Profile(“prod”) annotation.
@SpringBootApplication
@Profile("dev")
public class MainJavaApplicationDev {

   private static final Logger LOGGER = LoggerFactory.getLogger(MainJavaApplicationDev.class);

   public static void main(String[] args) {
       LOGGER.info("--- DEV PROFILE ---");
       Locale.setDefault(Locale.US);
       TimeZone.setDefault(TimeZone.getTimeZone("Etc/UCT"));

       SpringApplication.run(MainJavaApplicationDev.class, args);
   }

}
@SpringBootApplication
@Profile("prod")
public class MainJavaApplicationProd extends SpringBootServletInitializer {

   private static final Logger LOGGER = LoggerFactory.getLogger(MainJavaApplicationProd.class);

   public static void main(String[] args) {
       LOGGER.info("--- PROD PROFILE ---");
       Locale.setDefault(Locale.US);
       TimeZone.setDefault(TimeZone.getTimeZone("Etc/UCT"));

       SpringApplication.run(MainJavaApplicationProd.class, args);
   }

}

5. Run, Build and packaging using dev profile

5.1. Run the spring boot project using the dev profile

mvn spring-boot:run -P dev

Spring Boot will run the MainJavaApplicationDev class :

5.2. Package the project using the dev profile

mvn clean package -P dev

5.3. Run the build of the project using the dev profile

java -jar api.jar

Spring Boot will run the MainJavaApplicationDev class :

6. Package the project using the prod profile

mvn clean package -P prod

After deploying the war package into Tomcat server, Spring will run the MainJavaApplicationProd class :

Tags:  , , , ,