How to pass parameter to Maven plugin from CLI?

Aamir Khan picture Aamir Khan · Jul 18, 2016 · Viewed 9.9k times · Source
<build>
    <plugins>
        <plugin>
            <groupId>org.jvnet.jax-ws-commons</groupId>
            <artifactId>jaxws-maven-plugin</artifactId>
            <version>2.3</version>

            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>wsimport</goal>
                    </goals>
                    <id>generate-sei</id>

                    <configuration>
                        <sourceDestDir>${project.basedir}/src/main/java</sourceDestDir>
                    </configuration>
                </execution>
            </executions>
          <dependencies>...</dependencies>
        </plugin>
    </plugins>
</build>

The above XML snippet is from a POM file in a Java project. In this snippet I've defined the jaxws-maven-plugin to use a wsdl file to generate the SEI code and place it in the src/main/java directory. This plugin is bound to the generate-sources phase, and works fine.

I want to make it so that if I issue the plugin directly, using:

mvn jaxws:wsimport

it should place the files in the above mentioned folder. From the plugins reference site (https://jax-ws-commons.java.net/jaxws-maven-plugin/wsimport-mojo.html), I can't figure out how to pass the parameter (sourceDestDir) as a command line argument. Is there someway I can do this?

Answer

Tunaki picture Tunaki · Jul 18, 2016

WARNING /!\

You are trying to generate sources under the source folder src/main/java. Unless there is a very strong reason, don't do this. All generated content should always be placed under the build directory (target by default) and not be version-controlled. You can always add the generated sources as source folder using the build-helper-maven-plugin:add-source, if the plugin does not do it already itself.


To be able to set parameters directly on the command line, the plugin needs to define a user property. However, the org.jvnet.jax-ws-commons:jaxws-maven-plugin does not define a user property for the sourceDestDir parameter. This is noticeable because the documentation does not have a "User Property" set.

You can also find this in the source code:

@Parameter(defaultValue = "${project.build.directory}/generated-sources/wsimport")
private File sourceDestDir;

The @Parameter annotation, used to declare the parameter of the Maven plugin, does not have a corresponding property.

As such, you will need to have the following:

  1. Define a Maven property jaxws.sourceDestDir with a value of ${project.basedir}/src/main/java with

    <properties>
      <jaxws.sourceDestDir>${project.basedir}/src/main/java</jaxws.sourceDestDir>
    </properties>
    

    Preferably, you would have ${project.build.directory}/some/path instead of src/main/java.

  2. Configure the plugin to use this Maven property:

    <configuration>
      <sourceDestDir>${jaxws.sourceDestDir}</sourceDestDir>
    </configuration>
    
  3. If you want to override it, you can now do so directly on the command line with -Djaxws.sourceDestDir=/my/new/value. This system property will take precedence over the value of the Maven property.