0

I'm trying to use the 72.1.1 Automatic property expansion using Maven as specified in the documentation using different maven profiles, but I'm not able to make it work with specific profiles.

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>test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <profiles>
        <profile>
            <id>local</id>

            <activation>
                <property>
                    <name>profileName</name>
                    <value>local</value>
                </property>
            </activation>
            <properties>
                <wildcard.for.customproperty>valueA</wildcard.for.customproperty>
            </properties>
        </profile>

        <profile>
            <id>external</id>
            <activation>
                <property>
                    <name>profileName</name>
                    <value>external</value>
                </property>
            </activation>
            <properties>
                <wildcard.for.customproperty>valueB</wildcard.for.customproperty>
            </properties>
        </profile>
    </profiles>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
            <type>pom</type>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.9.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>1.5.9.RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

And this very simple application.properties:

custom.property=@wildcard.for.customproperty@

I run the maven build with Spring Tool Suite with the following goals:

clean install -DprofileName=local

The build SUCCESS, but the application.properties in target folder still contains the unresolved @...@ placeholder.

If I add this simple line in the pom.xml in one of the two profiles:

<activeByDefault>true</activeByDefault>

The placeholder is correctly resolved.

What's wrong with this configuration?

Alessandro C
  • 3,310
  • 9
  • 46
  • 82

2 Answers2

2

You have declared spring-boot-starter-parent as a dependency of your project, rather than using it as the project's parent. It should be declared like this:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>

You should then also remove the <version> declaration from your other Spring Boot dependencies. The resources configuration is also redundant so I'd recommend removing that too. All told, this will leave your pom looking similar to the following:

<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>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <profiles>
        <profile>
            <id>local</id>

            <activation>
                <property>
                    <name>profileName</name>
                    <value>local</value>
                </property>
            </activation>
            <properties>
                <wildcard.for.customproperty>valueA</wildcard.for.customproperty>
            </properties>
        </profile>

        <profile>
            <id>external</id>
            <activation>
                <property>
                    <name>profileName</name>
                    <value>external</value>
                </property>
            </activation>
            <properties>
                <wildcard.for.customproperty>valueB</wildcard.for.customproperty>
            </properties>
        </profile>
    </profiles>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • Two things: 1) I have already tried this solution, the result is the same. I need tag setted to true. 2) If I use a project that already use a parent project, how can I set the spring-boot-starter-parent as the parent of the project? – Alessandro C Dec 05 '17 at 09:32
  • You can't. The documentation you've linked to in the question already explains the configuration that's necessary if you're not using `spring-boot-starter-parent` as your project's parent. – Andy Wilkinson Dec 05 '17 at 18:50
  • I have solved in another way, see my answer below. There is something wrong on STS. – Alessandro C Dec 06 '17 at 08:49
0

I have solved with the following configuration:

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>test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <profiles>
        <profile>
            <id>local</id>

            <activation>
                <property>
                    <name>profileName</name>
                    <value>local</value>
                </property>
            </activation>
            <properties>
                <wildcard.for.customproperty>valueA</wildcard.for.customproperty>
                <activatedProperties>local</activatedProperties>
            </properties>
            <build>
                <finalName>myBuild-local</finalName>
            </build>
        </profile>

        <profile>
            <id>external</id>
            <activation>
                <property>
                    <name>profileName</name>
                    <value>external</value>
                </property>
            </activation>
            <properties>
                <wildcard.for.customproperty>valueB</wildcard.for.customproperty>
                <activatedProperties>external</activatedProperties>
            </properties>
            <build>
                <finalName>myBuild-external</finalName>
            </build>
        </profile>
    </profiles>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.9.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>1.5.9.RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <delimiters>
                        <delimiter>@{*}</delimiter>
                    </delimiters>
                    <useDefaultDelimiters>false</useDefaultDelimiters>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

application.properties:

custom.property=@{wildcard.for.customproperty}
spring.profiles.active=@{activatedProperties}

Finally, most important step:

In STS you need to right clic on the Project -> Properties -> Maven tab

There you need to specify the active profile.

For some reasons, even if you put the -PprofileName or the profile directly in the profile field of the Maven build, STS doesn't apply the profile.

Alessandro C
  • 3,310
  • 9
  • 46
  • 82