springspring-bootmavenpom.xmlprofile

Profiles in pom.xml doesn't work when i use "-P" command


I have a pom.xml with profiles like this:

<profiles>
        <profile>
            <id>server1</id>
            <properties>
                <api.server.ip>serveraddr1</api.server.ip>
                <finalName>springboot</finalName>
            </properties>
        </profile>
        <profile>
            <id>server2</id>
            <properties>
                <api.server.ip>serveraddr2</api.server.ip>
                <finalName>springbootnew</finalName>
            </properties>
        </profile>
    </profiles>

And i am trying to use it to set variable and make the name of jar

public static final String IP_ADDR="${api.server.ip}";

But when i use the command "mvn clean package -Pserver2", it doesn't work at all, neither the variable is set, nor the jar is named correctly. And i find in IDEA it is like this: profiles in IDEA I didn't see "MyProfile" anywhere. And it still doesn,t work when i trying to use IDEA to select profile.

tried command "mvn clean package -Pserver2" and selecting profile in IDEA


Solution

  • You cannot use Maven profiles to read properties in your Java code. You can use them to filter resources that you can read in your Java code.

    But as @M.Deinum already said, there are probably much better ways to achieve what you want to achieve.