Maven-传递参数以在exec-maven-plugin中使用 [英] Maven - pass argument to use in exec-maven-plugin

查看:233
本文介绍了Maven-传递参数以在exec-maven-plugin中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的pom中,我添加了exec-maven-plugin来调用将生成文件的Java类.此类需要将一些参数传递给main方法,其中之一是输入文件的位置(项目外部).到目前为止,我一直在为此使用相对路径:

in my pom I've added the exec-maven-plugin to call a java class which will generate a file. This class requires some parameters to be passed to the main method, one of those is the location of an input file (outside the project). Until now I've been using a relative path for this which works fine:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <executions>
                <execution>
                    <phase>test</phase>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>com.laco.projectmaster.util.LanguageGenerator</mainClass>
                <arguments>
                    <argument>../PM-Config/dev/PMLanguage.xls</argument>
                    <argument>PM4.0</argument>
                    <argument>${project.build.outputDirectory}/com/laco/projectmaster/props/resources</argument>
                    <argument>ProjectMaster</argument>
                    <argument>Created during maven build (POM Version: ${pom.version})</argument>
                </arguments>
            </configuration>
        </plugin>

现在,我开始使用hudson来安装/打包和部署战争,并且我不能再使用此相对路径了.很简单,我想,在调用maven时,我只是传递输入文件的位置,例如:

Now I'm starting to use hudson to install/package and deploy the wars and I cannot longer use this relative path. Simple I thought, I just pass the location of the input file when invoking maven like:

mvn清洁软件包-Dlangdir = C:/somedir

然后像这样修改pom:

and then alter the pom like:

<argument>${langdir}/PMLanguage.xls</argument>

但是,此参数在这里只是被忽略.主类作为参数接收的路径变为 null/PMLanguage.xls .参数本身在maven中可用,我在antrun插件中使用回显对成功进行了测试.回响了正确的路径.

However, this parameter simply gets ignored here. The path the main class receives as argument becomes null/PMLanguage.xls . The parameter itself is available in maven, I tested with succes using an echo in the antrun plugin. The correct path was echoed.

您传递给Maven的参数是否默认情况下不可用,无论您在pom中的何处引用它们?

Are the paremeters you pass to maven then not available by default no matter where you reference them in the pom?

感谢您的帮助,
斯蒂恩

thanks for any help,
Stijn

推荐答案

我无法重现该问题.我使用了以下测试类:

I can't reproduce the issue. I used the following test class:

package com.stackoverflow.q3421918;

public class Hello
{
    public static void main( String[] args )
    {
        System.out.println( args[0] + " " + args[1] );
    }
}

以及以下pom.xml:

And the following pom.xml:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.stackoverflow.q3421918</groupId>
  <artifactId>Q3421918</artifactId>
  <version>1.0-SNAPSHOT</version>

  <!-- this was a test for a workaround -->
  <properties>
    <myprop>${langdir}</myprop>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2</version>
        <executions>
          <execution>
            <phase>test</phase>
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>com.stackoverflow.q3421918.Hello</mainClass>
          <arguments>
            <argument>${myprop}</argument>
            <argument>${langdir}</argument>
          </arguments>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

这是我得到的输出:


$ mvn clean package -Dlangdir=C:/somedir 
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Q3421918
[INFO]    task-segment: [clean, package]
[INFO] ------------------------------------------------------------------------
...
[INFO] Preparing exec:java
[WARNING] Removing: java from forked lifecycle, to prevent recursive invocation.
[INFO] No goals needed for project - skipping
[INFO] [exec:java {execution: default}]
Hello c:/somedir c:/somedir
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
...

在Maven 2.2.1上进行了测试

Tested with Maven 2.2.1.

这篇关于Maven-传递参数以在exec-maven-plugin中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆