set systemProperty in exec-maven-plugin does not work

Tran Ngu Dang picture Tran Ngu Dang · Apr 12, 2016 · Viewed 17.5k times · Source

Here is my pom.xml file:

<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <profiles>
        <profile>
            <id>my_proj</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>1.4.0</version>
                        <executions>
                            <execution>
                                <phase>install</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>

                            <executable>java</executable>
                            <arguments>
                                <argument>-classpath</argument>
                                <classpath />
                                <argument>com.test.Main</argument>

                            </arguments>
                            <systemProperties>
                                <systemProperty>
                                    <key>someKey</key>
                                    <value>someValue</value>
                                </systemProperty>
                            </systemProperties>
                            <environmentVariables>
                                <someKey>
                                    someValue
                                </someKey>
                            </environmentVariables>
                        </configuration>
                    </plugin>
                </plugins>
            </build>

        </profile>
    </profiles>

</project>

and in Main.java

public static void main(String[] args) {

    System.out.println("hello world" + System.getenv("someKey") + " " + System.getProperty("someKey"));
}

the output when I run

mvn install -Pmy_proj

is

hello worldsomeValue null

I cannot seem to get the systemProperty value. What did I do wrong ?

Answer

A_Di-Matteo picture A_Di-Matteo · Apr 12, 2016

The systemProperty does not work simply because it is not an expected element of the exec goal of the exec-maven-plugin.

Check the official exec goal page, no systemProperties element is specified. As such, your configuration is still valid for Maven simply because is well-formed XML, but it is ignored by the exec-maven-plugin.

From official Maven Pom Reference concerning the plugin configuration element:

It may be good to note that all configuration elements, wherever they are within the POM, are intended to pass values to another underlying system, such as a plugin. In other words: values within a configuration element are never explicitly required by the POM schema, but a plugin goal has every right to require configuration values.


You are making confusion with the systemProperties configuration entry foreseen by its java goal. This option is available there because of its context: it's crafted for java executions. On the other hand, the exec goal is much more generic and as such cannot foresee an option required only by java programs.

To pass system properties to a Java execution via the exec goal, you can use the arguments configuration entry and use the -D notation

-Dproperty=value Sets a system property value.

Further note, as per official Running Java programs with the exec goal documentation, the -D arguments should go first:

<configuration>
    <executable>java</executable>
    <arguments>
        <argument>-DsomeKey2=someValue2</argument>
        <argument>-classpath</argument>
        <classpath />
        <argument>com.test.Main</argument>
    </arguments>
    <environmentVariables>
        <someKey>someValue</someKey>
    </environmentVariables>
</configuration>

Moreover, you should not set the same variable name for environment and as system property, the system property would not be set otherwise.