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

后端 未结 3 1033
闹比i
闹比i 2020-12-11 14:36

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\",

相关标签:
3条回答
  • 2020-12-11 15:20

    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>
    
    0 讨论(0)
  • 2020-12-11 15:21

    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.

    0 讨论(0)
  • 2020-12-11 15:23

    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"
    
    0 讨论(0)
提交回复
热议问题