在mvn exec:exec中传递命令行参数

时间:2011-03-22 19:31:02

标签: maven-plugin

令我感到惊讶的是,本来应该是一项非常轻松的工作对我来说是一项非常烦人的任务。我需要的是将几个命令行参数传递给我的maven exec:exec插件。不幸的是,几小时的谷歌搜索根本没有帮助。

这是我的插件

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-instrument</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
    <configuration>
        <executable>java</executable>
        <arguments>
            <argument>-classpath</argument>
            <classpath />
            <argument>-javaagent:${settings.localRepository}/org/springframework/spring-instrument/${spring.version}/spring-instrument-${spring.version}.jar</argument>
            <argument>-Xmx256m</argument>
            <argument>com.myPackage.Myclass</argument>
        </arguments>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>exec</goal>
            </goals>
        </execution>
    </executions>
</plugin>

现在从命令提示符输入:

mvn exec:exec -Dexec.args=-Dmy.property=myProperty

我也尝试过:

mvn exec:exec -Dexec.arguments=-Dmy.property=myProperty

还有很多其他的事情。但似乎没有任何工作。我知道exec:exec运行在一个单独的VM中,但根据文档-Dexec.args应该适合我。有人可以建议我哪里出错吗?

7 个答案:

答案 0 :(得分:23)

将命令行参数传递给mvn:exec:

的两种方法

方法1,在命令行上:

mvn exec:java -Dexec.mainClass="com.myPackage.myClass" -Dexec.args="command line arguments"

方法2,在maven POM文件中:

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <configuration>
        <mainClass>com.myPackage.myClass</mainClass>
        <commandlineArgs>command line arguments</commandlineArgs>
      </configuration>
    </plugin>
  </plugins>
</build>

然后在命令行上你要做的就是运行:

mvn exec:java
祝你好运。

答案 1 :(得分:19)

在阅读this article之后,我能够使用以下内容为exec:exec运行JVM args:

    <build>
      <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>java</executable>
                <arguments>
                    <argument>-Dhttp.proxyHost=myproxy.example.com</argument>
                    <argument>-Dhttp.proxyPort=8080</argument>
                    <argument>-classpath</argument>
                    <classpath />
                    <argument>com.example.Main</argument>
                </arguments>
            </configuration>
        </plugin>
      </plugins>
    </build>

答案 2 :(得分:4)

为什么不使用系统属性?

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.3.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>bobo.Abc</mainClass>
                  <arguments>
                        <argument>argument1</argument>
                    </arguments>
                    <systemProperties>
                        <systemProperty>
                            <key>jvmProperty1</key>
                            <value>dev</value>
                        </systemProperty>
                    </systemProperties>
                </configuration>
            </plugin>

答案 3 :(得分:1)

如果要将命令行参数传递给Java VM,请使用<commandlineArgs>标记而不是<arguments>Maven Exec Plugin

干杯

答案 4 :(得分:0)

我使用以下命令行设置通过执行插件将参数传递给我的Main-Class。

mvn clean install -Dexec.arguments="arg0"

答案 5 :(得分:0)

问题是您在命令行上使用-Dexec.args,并且它覆盖了<arguments>中给出的pom.xml。您可以使用它们中的任何一个,不能同时使用。

答案 6 :(得分:0)

我认为所选答案不能解决问题。这是我可行的解决方案:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-instrument</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
    <configuration>
        <executable>java</executable>
        <arguments>
            <argument>-classpath</argument>
            <classpath />
            <argument>-Xmx256m</argument>
            <argument>com.myPackage.Myclass</argument>
            <argument>${myProperty1}</argument> <!-- variable args here!!! -->
            <argument>${myProperty2}</argument>

        </arguments>
    </configuration>
    <executions>
        <execution>
            <id>myExecution</id> <!-- defined an id here! -->
            <goals>
                <goal>exec</goal>
            </goals>
        </execution>
    </executions>
</plugin>

现在,您可以简单地执行传递参数的操作。

mvn exec:exec@myExecution -DmyProperty1=XXX -DmyProperty2=YYY
相关问题