How to pass systemProperties when invoking exec:ja

2019-01-20 07:43发布

问题:

I want to use the exec:java plugin to invoke the main class from command line. I can pass arguments from the command line using -Dexec.args="arg0 arg1 arg2", I don't know how to pass system properties. I tried '-Dexec.systemProperties="key=value"` but with no effect.

pom.xml looks like this:

  <plugin>  
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <configuration>
      <mainClass>ibis.structure.Structure</mainClass>
    </configuration>  
  </plugin>

回答1:

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"


回答2:

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>


回答3:

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.