Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 413
Stijn Geukens Avatar asked Aug 06 '10 07:08

Stijn Geukens


People also ask

How to pass command line arguments to Java VM using Maven?

If you want to pass command line arguments to Java VM use <commandlineArgs> tag instead of <arguments>. Maven Exec Plugin AFAIK this is incorrect. <commandlineArgs> overrides anything you set in the <arguments> list; they serve the same purpose. I use the following command line setting to pass arguements to my Main-Class with the exceution plugin.

What are the goals of the Exec Maven plugin?

The plugin provides 2 goals to help execute system and Java programs. General information about the goals. exec:exec execute programs and Java programs in a separate process. exec:java execute Java programs in the same VM. General instructions on how to use the Exec Maven Plugin can be found on the usage page.

What is Maven compiler plugin?

This quick tutorial introduces the compiler plugin, one of the core plugins of the Maven build tool. For an overview of the other core plugins, refer to this article. 2. Plugin Goals The compiler plugin is used to compile the source code of a Maven project.

How do I Configure my Maven plugins?

Maven plugins (build and reporting) are configured by specifying a <configuration> element where the child elements of the <configuration> element are mapped to fields, or setters, inside your Mojo (remember that a plug-in consists of one or more Mojos where a Mojo maps to a goal).


1 Answers

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.

like image 84
Pascal Thivent Avatar answered Oct 16 '22 23:10

Pascal Thivent