TExcelTAD
Home › Java Builds: Maven & Gradle › Properties & Profiles

Properties & Profiles

A POM describes one build, but builds differ a little between a laptop, CI and a release: a different database URL in test resources, an extra check in CI, a signing step only for releases. Maven's tools for this are properties (values you can override) and profiles (blocks of POM that switch on under a condition).

Properties and where they come from

${name} in a POM is replaced by a property, looked up in this order (first found wins):

  1. -Dname=value on the command line (user properties);
  2. <properties> in the POM, its profiles and its parents;
  3. built-ins: ${project.version}, ${project.basedir}, ${env.HOME}, Java system properties such as ${java.version}.

Because the command line wins, every property is a knob: -Dmaven.compiler.release=17, -DskipTests, -Dgreeting=hi.

Resource filtering

With filtering on, the resources plugin replaces ${…} in resource files too, which is how build information gets into a running program:

bash · try it yourself
mkdir -p info/src/main/java/demo info/src/main/resources && cd info
cat > src/main/resources/build.properties <<'RES'
app.version=${project.version}
app.env=${deploy.env}
RES
cat > src/main/java/demo/Info.java <<'SRC'
package demo;

import java.util.Properties;

public class Info {
    public static void main(String[] args) throws Exception {
        Properties p = new Properties();
        p.load(Info.class.getResourceAsStream("/build.properties"));
        System.out.println("version=" + p.getProperty("app.version") + " env=" + p.getProperty("app.env"));
    }
}
SRC
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>info</artifactId>
  <version>2.3.0</version>
  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <deploy.env>laptop</deploy.env>
  </properties>
  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
  </build>
</project>
POM
mvn -q compile && java -cp target/classes demo.Info
mvn -q compile -Ddeploy.env=staging && java -cp target/classes demo.Info
version=2.3.0 env=laptop
version=2.3.0 env=staging

Note: Filter only the files that need it. A filtered binary file (an image, a keystore) gets corrupted, and a filtered Spring or shell file loses its own ${…} placeholders. Put files to filter in a separate folder such as src/main/filtered-resources.

Profiles

A profile is a named block that can change almost anything in the POM: properties, dependencies, plugins, modules, dependencyManagement. It applies when it is activated:

Activation Example
explicitly mvn -P ci verify, several with -P ci,release, disable with -P !ci
by a property <property><name>env</name><value>prod</value></property> → active with -Denv=prod
by the JDK <jdk>[21,)</jdk>
by the OS <os><family>windows</family></os>
by a file <file><exists>${basedir}/local.properties</exists></file>
by default <activeByDefault>true</activeByDefault>
bash · try it yourself
mkdir -p prof && cd prof
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>prof</artifactId>
  <version>1.0</version>
  <properties>
    <deploy.env>laptop</deploy.env>
  </properties>
  <profiles>
    <profile>
      <id>local-db</id>
      <activation><activeByDefault>true</activeByDefault></activation>
      <properties><db.url>jdbc:h2:mem:dev</db.url></properties>
    </profile>
    <profile>
      <id>ci</id>
      <properties><deploy.env>ci</deploy.env></properties>
    </profile>
    <profile>
      <id>prod</id>
      <activation><property><name>env</name><value>prod</value></property></activation>
      <properties><deploy.env>production</deploy.env><db.url>jdbc:postgresql://db/prod</db.url></properties>
    </profile>
  </profiles>
</project>
POM
show() {
  local active=$(mvn -o help:active-profiles "$@" | grep -oE '^ - [a-z-]+' | sed 's/^ - //' | paste -sd, -)
  local env=$(mvn -q -o help:evaluate -Dexpression=deploy.env -DforceStdout "$@")
  local db=$(mvn -q -o help:evaluate -Dexpression=db.url -DforceStdout "$@")
  printf '%-14s active=%-10s env=%-11s db=%s\n' "${*:-(none)}" "$active" "$env" "$db"
}
show
show -P ci
show -Denv=prod
show -P ci,local-db
(none)         active=local-db   env=laptop      db=jdbc:h2:mem:dev
-P ci          active=ci         env=ci          db=null object or invalid expression
-Denv=prod     active=prod       env=production  db=jdbc:postgresql://db/prod
-P ci,local-db active=local-db,ci env=ci          db=jdbc:h2:mem:dev

Read the four lines carefully:

  • With no options, the activeByDefault profile is on.
  • -P ci turned ci on and turned local-db off: an activeByDefault profile is deactivated as soon as any other profile in the same POM is activated (explicitly or by a condition, as -Denv=prod shows). This surprises everyone once. db.url vanished because nothing else defines it (help:evaluate reports null object or invalid expression). To keep it, list it too: -P ci,local-db, the last line.
  • When two active profiles set the same property, the one declared later in the POM wins.

Profiles: use sparingly

Profiles make a build behave differently depending on who runs it, which is the opposite of what you want from a build. Good uses: a release profile that signs and attaches sources and Javadoc, a ci profile that turns on slower checks, an OS profile that picks a native library. Bad uses: different dependencies per environment (build one artifact and configure it at deploy time instead), or profiles that must be remembered for the build to work at all.

Test yourself

A POM sets <deploy.env>laptop</deploy.env>. What does ${deploy.env} resolve to with mvn -Ddeploy.env=ci package?

  1. laptop
  2. ci
  3. Both
  4. The build fails

A profile has activeByDefault true. You run mvn -P ci verify, where ci is another profile in the same POM. Is the default profile active?

  1. Yes, always
  2. No: activeByDefault profiles switch off when another profile in the POM is activated
  3. Only if ci doesn't exist
  4. Only in CI

What does resource filtering do?

  1. Removes unused resources
  2. Replaces ${…} placeholders in resource files with property values while copying them
  3. Compresses resources
  4. Excludes test resources from the JAR