Maven - pass argument to use in exec-maven-plugin

2019-01-25 17:54发布

问题:

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

回答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.