Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameter to Maven plugin from CLI?

<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?

like image 489
Aamir Khan Avatar asked Jul 18 '16 11:07

Aamir Khan


People also ask

How do I pass a parameter in maven?

Passing an Argument to MavenMaven will use the value (2.5) passed as an argument to replace the COMMON_VERSION_CMD property set in our pom. xml. This is not limited to the package command — we can pass arguments together with any Maven command, such as install, test, or build.

How do I add VM arguments in POM XML?

1 Answer. Show activity on this post. Using the Maven Compiler Plugin configuration for compiling application code and test code, you can set the required Xmx, Xms, Xss options via the compileArgs configuration entry, available for both compile and testCompile goals.


1 Answers

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.

like image 171
Tunaki Avatar answered Sep 24 '22 13:09

Tunaki