Core Java

Understanding Maven Predefined Properties

Maven is a widely used build automation tool that helps manage project dependencies, compile code, package applications, and run tests. One of the powerful features of Maven is predefined properties, which provide useful metadata and runtime information about the project, environment, and system. This article explores Maven predefined properties, their various categories, and practical usage examples within a Maven project.

1. What Are Maven Predefined Properties?

Maven provides several built-in properties that can be used within the pom.xml file or passed to plugins during the build process. These properties offer information such as project details, system properties, user-defined properties, and environment variables.

Predefined properties are automatically available and do not require explicit declaration. They are useful for customizing builds, passing information dynamically, and simplifying configurations.

1.1 Types of Maven Predefined Properties

Maven predefined properties fall into the following categories:

  • Project Properties (project.*)
    • These properties provide metadata about the project, such as artifact ID, group ID, version, name, and packaging type.
  • Build Properties (build.*)
    • These properties relate to build-specific details like directory paths, source encoding, and compiler information.
  • System Properties (maven.*)
    • These properties provide information about the Maven runtime, including Maven home directory and user settings file.
  • Java System Properties (java.*)
    • These properties provide information about the Java environment, including version and runtime details.
  • User and Environment Properties (env.* and user.*)
    • These properties allow access to system environment variables and user-related configurations.

2. Project Properties (project.*)

Project properties allow us to access details such as the project’s group ID, artifact ID, version, name, and description. These properties are commonly used in plugins, resource files, and build configurations to maintain consistency across the project. Maven provides several predefined properties related to the project. Some commonly used ones are:

PropertyDescription
${project.groupId}The group ID of the project
${project.artifactId}The artifact ID of the project
${project.version}The project version
${project.name}The project name
${project.description}The project description
${project.packaging}The packaging type (jar, war, etc.)

Example Usage in pom.xml

This example will create a config.properties file containing project metadata like groupId, artifactId, version, and description.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="UTF-8"?>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.jcg</groupId>
    <artifactId>maven-predefined-properties-examples</artifactId>
    <version>1.0</version>
    <name>MavenPredefinedPropertiesApplication</name>
    <description>Demonstration of Maven predefined properties</description>
    <packaging>jar</packaging>
    <properties>
        <config.file>${project.build.outputDirectory}/config.properties</config.file>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
 
</project>

Inside src/main/resources/config.properties, define placeholders that Maven will replace.

1
2
3
4
5
project.groupId=@project.groupId@
project.artifactId=@project.artifactId@
project.version=@project.version@
project.name=@project.name@
project.description=@project.description@

Running the command mvn clean package packages the project, generating a config.properties file inside target/classes with replaced values (@project.groupId@, @project.artifactId@, and other placeholders will be replaced with actual values from pom.xml), demonstrating a practical use of Maven predefined properties dynamically.

3. Build Properties (build.*)

Build properties provide information about the Maven build process, including output directories, source directories, and test directories. These properties are useful for configuring plugins that need to interact with compiled classes or packaged artifacts.

PropertyDescription
${project.build.sourceDirectory}Source code directory (src/main/java)
${project.build.outputDirectory}Compiled class files directory (target/classes)
${project.build.testOutputDirectory}Compiled test classes directory
${project.build.directory}Target build directory (target)
${project.build.finalName}Final name of the packaged artifact

Example Usage in Maven Plugin

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.outputDirectory}/config</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

This will copy all resources to the ${project.build.outputDirectory}/config directory during the process-resources phase.

4. System Properties (maven.*)

System properties allow access to environment variables (env.*) and Java system properties (java.*). These properties enable dynamic configuration based on the system’s runtime environment, making the build adaptable to different machines and deployment setups.

PropertyDescription
${maven.home}The Maven installation directory
${maven.repo.local}The local Maven repository (~/.m2/repository)

5. Java System Properties (java.*)

These properties provide Java runtime details.

PropertyDescription
${java.version}Java version used by Maven
${java.home}Java home directory
${java.vendor}Java vendor name

6. User and Environment Properties (env.* and user.*)

These properties help access user and system environment variables.

PropertyDescription
${env.PATH}System PATH variable
${env.HOME}User home directory ($HOME or %USERPROFILE%)
${user.name}Current system username

7. Custom Properties in pom.xml

In addition to Maven’s predefined properties, you can define custom properties in the pom.xml file using the <properties> section. These properties act like variables, making your Maven build more flexible and maintainable. You can define custom properties inside the <properties> section of pom.xml like this:

1
2
3
4
5
<properties>
    <app.name>My Maven App</app.name>
    <app.version>2.0.0</app.version>
    <java.version>17</java.version>
</properties>

These properties can then be referenced elsewhere in pom.xml using ${property-name} syntax.

8. Conclusion

In this article, we explored Maven predefined properties, their categories, and practical usage in a Maven project. We demonstrated how project properties (project.*), build properties (build.*), and system properties (env.*, java.*) can be leveraged to enhance build automation. By effectively utilizing these properties, we can create more dynamic and configurable Maven builds, making project management and automation more efficient.

Omozegie Aziegbe

Omos Aziegbe is a technical writer and web/application developer with a BSc in Computer Science and Software Engineering from the University of Bedfordshire. Specializing in Java enterprise applications with the Jakarta EE framework, Omos also works with HTML5, CSS, and JavaScript for web development. As a freelance web developer, Omos combines technical expertise with research and writing on topics such as software engineering, programming, web application development, computer science, and technology.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button