Maven Resource Filtering with Profile Properties
Asked Answered
L

2

9

I have the following pom.xml:

<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>mycompany</groupId>
    <artifactId>resource-fail</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <foo.bar>BazBat</foo.bar>
    </properties>
    <build>
        <resources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>
    <profiles>
        <profile>
            <id>Development</id>
            <!--
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            -->
            <properties>
                <foo.bar>Development</foo.bar>
            </properties>
        </profile>
    </profiles>
</project>

Notice that the activation element on the profile is commented out

I also have the following in src/main/java/application.properties

myproperty=${foo.bar}

So what I expect Maven to do is simply replace ${foo.bar} with BazBat, unless the Development profile is activiated, then I expect Maven to replace ${foo.bar} with Development

When I run mvn package it sets the value to BazBat. Good, the resource filtering is working as expected.

However when I run mvn package -P Development it sets the value to BazBat

To confuse things further, if uncomment the activation element on the profile and run mvn package it will set ${foo.bar} to Development

I've even run mvn help:active-profiles package to have it output the active profile name and under both scenarios above it says that the Development profile is active, but filtering still does not work without the activeByDefault flag being set.

To sum up:

  • Maven properly overrides properties with values from the profile if the profile is activeByDefault
  • Maven does not override the property if I activate the profile via the command line switch

What is going on?

My environment:

> mvn -version
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T11:29:23-06:00)
Maven home: C:\apache-maven-3.2.5
Java version: 1.8.0_65, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_65\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "dos"

Update: I've upgraded to the latest maven (3.3.3) - the problem persists. My new environment:

> mvn -version
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T06:57:37-05:00)
Maven home: C:\apache-maven-3.3.3
Java version: 1.8.0_65, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_65\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "dos"
Libelant answered 27/10, 2015 at 21:55 Comment(1)
Did you get it working? Even I'm facing same problem #51187463, please reviewEarful
O
0

I had the same problem while running this with m2e in Eclipse. Solved it by enabling the profile in the plugin : Right-click your project in Eclipse, then select Maven -> Select Maven Profiles... Here you can enable the development profile

After I did this, maven filtered my resources using the property values set in the profile. I removed the "-P" argument in my Run Configuration, as this was no longer needed.

This might also work if you define and enable the profile in your settings.xml file, but I didn't try that.

Occultation answered 10/10, 2019 at 14:17 Comment(0)
G
-3

You need to remove the space between -P and development when you execute: mvn package -PDevelopment

Gecko answered 7/1, 2016 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.