【问题标题】:Maven - pass argument to use in exec-maven-pluginMaven - 传递参数以在 exec-maven-plugin 中使用
【发布时间】:2011-03-26 05:09:50
【问题描述】:

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

        <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 时传递输入文件的位置,例如:

mvn clean package -Dlangdir=C:/somedir

然后像这样改变pom:

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

然而,这个参数在这里被忽略了。主类作为参数接收的路径变为 null/PMLanguage.xls 。参数本身在 maven 中可用,我在 antrun 插件中使用 echo 成功测试。回显了正确的路径。

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

感谢您的帮助,
斯蒂金

【问题讨论】:

    标签: maven-2 exec-maven-plugin


    【解决方案1】:

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

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

    还有以下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] 正在扫描项目... [信息] --------------------------------------------- ------------------------- [INFO] 楼 Q3421918 [INFO] 任务段:[clean, package] [信息] --------------------------------------------- ------------------------- ... [信息] 准备执行:java [警告] 删除:java 从分叉生命周期中,以防止递归调用。 [INFO] 项目不需要目标 - 跳过 [信息] [执行:java {执行:默认}] 你好 c:/somedir c:/somedir [信息] --------------------------------------------- ------------------------- [信息] 构建成功 [信息] --------------------------------------------- ------------------------- ...

    使用 Maven 2.2.1 测试。

    【讨论】:

    • 帕斯卡,很抱歉,看来我浪费了您宝贵的时间。我无法解释,但是当我现在重新运行包装时,它工作正常。我没有改变任何东西,所以我不明白为什么昨天它不起作用。所以,对不起,谢谢。
    猜你喜欢
    • 2019-03-26
    • 2018-07-09
    • 2014-12-25
    • 2015-08-26
    • 2017-05-06
    • 1970-01-01
    • 2014-04-23
    • 2021-03-31
    • 2016-01-31
    相关资源
    最近更新 更多