【问题标题】:Pass command line Params in mvn exec:exec在 mvn exec:exec 中传递命令行参数
【发布时间】:2011-03-22 19:31:01
【问题描述】:

我很惊讶,本来应该很轻松的工作却变成了对我来说非常烦人的任务。我需要的只是将一些命令行参数传递给我的 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 应该对我有用。有人可以建议我哪里出错了吗?

【问题讨论】:

    标签: maven-plugin


    【解决方案1】:

    将命令行参数传递给 mvn:exec: 的两种方法:

    方法一,命令行:

    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
    

    祝你好运。

    【讨论】:

    • 最后,以命令行方式工作的例子。问题是这个插件的官方文档指定了 commandlineArgs 属性,而事实上它是 args
    • 目标文档在同一个表中指定了Maven属性和绑定的用户属性。 mojo 文档读起来有点特别,但由于它们是自动生成的,所以它们非常正确(对于它们生成的版本)
    【解决方案2】:

    在阅读this article 后,我能够使用以下内容让 JVM args 为 exec:exec 工作:

        <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>
    

    【讨论】:

      【解决方案3】:

      为什么不使用系统属性?

                  <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>
      

      【讨论】:

        【解决方案4】:

        如果您想将命令行参数传递给 Java VM,请使用 &lt;commandlineArgs&gt; 标签而不是 &lt;arguments&gt;Maven Exec Plugin

        干杯

        【讨论】:

        • AFAIK 这是不正确的。 &lt;commandlineArgs&gt; 覆盖您在&lt;arguments&gt; 列表中设置的任何内容;它们的用途相同。
        • 链接失效
        【解决方案5】:

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

        mvn clean install -Dexec.arguments="arg0"
        

        【讨论】:

          【解决方案6】:

          我认为所选答案不能解决问题。这是我的有点老套的解决方案:

          <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
          

          【讨论】:

            【解决方案7】:

            问题是您在命令行上使用了-Dexec.args,它覆盖了pom.xml 中给出的&lt;arguments&gt;。您可以使用其中任何一个,不能同时使用。

            【讨论】:

              猜你喜欢
              • 2017-09-11
              • 2016-03-25
              • 1970-01-01
              • 1970-01-01
              • 2012-09-09
              • 1970-01-01
              • 2012-07-11
              • 1970-01-01
              • 2020-08-24
              相关资源
              最近更新 更多