Maven - 传递参数以在 exec-maven-plugin 中使用

发布于 2024-09-13 07:37:01 字数 1741 浏览 10 评论 0原文

在我的 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 的参数都不可用吗?

感谢您的帮助,
斯泰因

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>

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 clean package -Dlangdir=C:/somedir

and then alter the pom like:

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩套路吗 2024-09-20 07:37:01

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

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] 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 进行测试。

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] );
    }
}

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>

And here is the output I get:

$ 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] ------------------------------------------------------------------------
...

Tested with Maven 2.2.1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文