Maven profiling for your java app

Hi,

Suppose you’re working on a maven project in your local machine using some local credentials, like database connections, username-passwords and different kind of keys that aren’t the same as your server credentials.

What do you do? change each and every crendentials one by one before you deploy it on server? No. You need something like maven profiling to make your life easy.

Configure some profiles and you can active them by passing an argument when you build or package your application.

1. Create Profiles

<profiles>

    <profile>
        <id>local</id>
        <activation>
            <!-- this profile is active by default -->
            <activeByDefault>true</activeByDefault>
            <!-- activate if system properties 'env=dev' -->
            <property>
                <name>env</name>
                <value>local</value>
            </property>
        </activation>
        <properties>
            <packaging.type>jar</packaging.type>
            <db.driverClassName>com.mysql.jdbc.Driver</db.driverClassName>
            <db.url>jdbc:mysql://localhost/localdb?useUnicode=yes&amp;characterEncoding=UTF-8&amp;characterSetResults=UTF-8
                &amp;createDatabaseIfNotExist=true
            </db.url>
            <db.username>local_db_user</db.username>
            <db.password>local_db_pass</db.password>
        </properties>
    </profile>

    <profile>
        <id>dev</id>
        <activation>                <!-- activate if system properties 'env=dev' -->
            <property>
                <name>env</name>
                <value>dev</value>
            </property>
        </activation>
        <properties>
            <packaging.type>jar</packaging.type>
            <db.driverClassName>com.mysql.jdbc.Driver</db.driverClassName>
            <db.url>jdbc:mysql://localhost/devdb?useUnicode=yes&amp;characterEncoding=UTF-8&amp;characterSetResults=UTF-8
                &amp;createDatabaseIfNotExist=true
            </db.url>
            <db.username>dev_user</db.username>
            <db.password>dev_pass</db.password>
        </properties>
    </profile>

    <profile>
        <id>prod</id>
        <activation>
            <!-- activate if system properties 'env=prod' -->
            <property>
                <name>env</name>
                <value>prod</value>
            </property>
        </activation>
        <properties>
            <packaging.type>jar</packaging.type>
            <db.driverClassName>com.mysql.jdbc.Driver</db.driverClassName>
            <db.url>jdbc:mysql://localhost/productiondb?useUnicode=yes&amp;characterEncoding=UTF-8&amp;characterSetResults=UTF-8
                &amp;createDatabaseIfNotExist=true
            </db.url>
            <db.username>prod_user</db.username>
            <db.password>prod_pass</db.password>
        </properties>
    </profile>
</profiles>

Here we’ve configured some profiles local, dev, prod in our pom.xml file

Each profile will be activated by passing -Denv argument in for mvn command.

For example mvn clean package -Denv=prod will activate prod  profile and use that configuration.

2. Use configs in application.properties

We can access these db propeties in our application.properties file like this

#DATASOURCE
[email protected]@
[email protected]@
[email protected]@
[email protected]@

3. Showing active profile

Add this plugin inside <plugins></plugins> section. This will show active profiles when you ompile.

<!-- display active profile in compile phase -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-help-plugin</artifactId>
    <executions>
        <execution>
            <id>show-profiles</id>
            <phase>compile</phase>
            <goals>
                <goal>active-profiles</goal>
            </goals>
        </execution>
    </executions>
</plugin>

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *