How to pass systemProperties when invoking exec:java plugin in maven?

微笑、不失礼 提交于 2019-11-28 13:16:46
Sean Patrick Floyd

There is no way to set the <systemProperties> parameter on the command line.

However, since exec:java is not forked, you can just pass a system property to maven and it will be picked up by exec:java as well.

mvn -Dkey=value exec:java -Dexec.mainClass=com.yourcompany.yourclass \
    -Dexec.args="arg1 arg2 arg3"

Try following for me it works properly

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <configuration>
                <mainClass>ibis.structure.Structure</mainClass>
                <systemProperties>
                    <systemProperty>
                        <key>someKey</key>
                        <value>someValue</value>
                    </systemProperty>
                </systemProperties>
            </configuration>
        </plugin>

I just ran into a similar problem and I wanted to write a full answer for others that might come across this question.

Even though the question is not about pom.xml but about command line - it does not state how to do the same with pom.xml so here it is

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>

                <goals>
                    <goal>java</goal>
                </goals>

                <configuration>
                     <mainClass>myPackage.MyMain</mainClass>
                      <systemProperties>
                          <property>
                              <key>myKey</key>
                              <value>myValue</value>
                          </property>
                      </systemProperties>
                </configuration>

            </plugin>
        </plugins>
    </build>

For the command line - I think Sean Patrick Floyd's answer is good - however, if you have something already defined in your pom.xml it will override it.

So running

 mvn exec:java -DmyKey=myValue

should also work for you.

You should also note that the exec plugin's documentations states the following

A list of system properties to be passed. 
Note: as the execution is not forked, some system properties required 
by the JVM cannot be passed here. 
Use MAVEN_OPTS or the exec:exec instead. See the user guide for more information.

So you can also do something like this

export MAVEN_OPTS=-DmyKey=myValue
mvn exec:java

and it should work the same way.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!