StackTips

Working with Profiles in Maven

nilan avtar

Written by

Nilanchala,  5 min read,  2.55K views, updated on Jan. 08, 2024
💡
Chapter 7- Working with Profiles in Maven, delves into managing profiles in Maven to customize the project’s build configuration for different environments or conditions.

Maven allows you to customize a project’s build configuration for different environment or conditions. This can be done through profiles.

Profiles allow you to define a set of configuration options that are activated only under certain conditions, such as a specific environment or build context. Profiles can be defined in a project's pom.xml file, or an external XML file. Here's an example of a profile defined in a pom.xml file:

<project>
  ...
  <profiles>
    
    <profile>
      <id>dev</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <database.driver>com.mysql.jdbc.Driver</database.driver>
        <database.url>jdbc:mysql://localhost/devdb</database.url>
        <database.username>devuser</database.username>
        <database.password>devpass</database.password>
      </properties>
    </profile>

    <profile>
      <id>prod</id>
      <properties>
        <database.driver>com.mysql.jdbc.Driver</database.driver>
        <database.url>jdbc:mysql://localhost/proddb</database.url>
        <database.username>produser</database.username>
        <database.password>prodpass</database.password>
      </properties>
    </profile>
  </profiles>

  ...
</project>

In this example, two profiles are defined: dev and prod. The dev profile is active by default and sets the database configuration properties for a development environment. The prod profile sets the database configuration properties for a production environment.

To activate a profile, you can use the -P command line option when running Maven. For example, to activate the prod profile, you can run the following command:

mvn clean install -Pprod

This will activate the prod profile, and use the database configuration properties defined in that profile.

In addition to property settings, profiles can also define other configuration options such as plugin configurations, build profiles, and more. By using profiles, you can create a flexible and customizable build configuration that can be tailored to different environments and situations.

Wrap up

The Maven course is an essential training program for Java developers to create robust, maintainable, and scalable Java applications. Hope this course helps you to to learn maven build process for your next Java application.

Getting Started with Spring Boot- Beginner's Guide

This course covers the fundamentals of Spring Boot an you will gain the necessary skills to develop various types of applications using the powerful features of the Spring Boot framework.

>> CHECK OUT THE COURSE

Continue reading..