Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until tomorrow:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until tomorrow:

>> GET ACCESS NOW

1. Overview

In this quick tutorial, we're going to see how to read environment variables from Maven‘s pom.xml to customize the build process.

2. Environment Variables

To refer to environment variables from the pom.xml, we can use the ${env.VARIABLE_NAME} syntax.

For instance, let's use it to externalize the Java version in the build process:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>${env.JAVA_VERSION}</source>
                <target>${env.JAVA_VERSION}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

We should remember to pass the Java version information via environment variables. If we fail to do so, then we won't be able to build the project.

To run the Maven goals or phases against such a build file, we should first export the environment variable. For instance:

$ export JAVA_VERSION=9
$ mvn clean package

On Windows, we should use set VAR=value” syntax to export the environment variable.

In order to provide a default when the JAVA_VERSION environment variable is missing we can use a Maven profile:

<profiles>
    <profile>
        <id>default-java</id>
        <activation>
            <property>
                <name>!env.JAVA_VERSION</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

As shown above, we're creating a profile and making it active only if the JAVA_VERSION environment variable is missing — !env.JAVA_VERSION part. If that happens, then this new plugin definition will override the existing one.

3. Conclusion

In this short tutorial, we saw how to customize the build process by passing environment variables to the pom.xml.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until tomorrow:

>> GET ACCESS NOW

Maven footer banner
1 Comment
Oldest
Newest
Inline Feedbacks
View all comments
Comments are closed on this article!