-1

I need to create 2 maven profiles where I can set two different system properties. These profiles are just to set system property and not related any plugin.

like

<profile>
   <id> profile1 to set system property 1</id>
   .... set system property1
</profile>
<profile>
   <id> profile2 to set system property 2</id>
   .... set system property2
</profile>
user2030632
  • 111
  • 1
  • 1
  • 7
  • 1
    Possible duplicate of [Setting a system variable within a maven profile](http://stackoverflow.com/questions/25604969/setting-a-system-variable-within-a-maven-profile) – kryger Nov 05 '15 at 23:39

1 Answers1

5

You can, but it depends on what you need it for. This is the most common way of doing it:

  <profiles>
    <profile>
      <id>profile-1</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0-alpha-2</version>
            <executions>
              <execution>
                <goals>
                  <goal>set-system-properties</goal>
                </goals>
                <configuration>
                  <properties>
                    <my-prop>Yabadabadoo!</my-prop>
                  </properties>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

but this only sets the system property during Maven's execution, so if you want (for instance) this class to pick it up:

package org.example;

public class App {
    public static void main( String[] args)      {
        System.out.println("-->" + System.getProperty("my-prop"));
    }
}

you need to run it with mvn -P profile-1 compile exec:java -Dexec.mainClass=org.example.App and it will yield the following result:

[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ sys-prop ---
-->Yabadabadoo!

Running it without the compile goal will give you a null because the exec plugin is not bound to any build phase in this instance.

But if you need system properties for (say) unit tests, then the surefire plugin is what you need instead.

Daniel
  • 4,033
  • 4
  • 24
  • 33