如何从 CLI 将参数传递给 Maven 插件? [英] How to pass parameter to Maven plugin from CLI?

查看:45
本文介绍了如何从 CLI 将参数传递给 Maven 插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<插件><插件><groupId>org.jvnet.jax-ws-commons</groupId><artifactId>jaxws-maven-plugin</artifactId><version>2.3</version><执行><执行><phase>generate-sources</phase><目标><目标>wsimport</目标></目标><id>generate-sei</id><配置><sourceDestDir>${project.basedir}/src/main/java</sourceDestDir></配置></执行></执行><dependencies>...</dependencies></插件></plugins></build>

以上 XML 片段来自 Java 项目中的 POM 文件.在此代码段中,我定义了 jaxws-maven-plugin 以使用 wsdl 文件生成 SEI 代码并将其放置在 src/main/java 目录中.这个插件绑定到 generate-sources 阶段,并且工作正常.

我想这样做,如果我直接发布插件,使用:

mvn jaxws:wsimport

它应该将文件放在上面提到的文件夹中.从插件参考站点 (https://jax-ws-commons.java.net/jaxws-maven-plugin/wsimport-mojo.html),我不知道如何将参数(sourceDestDir)作为命令行参数传递.有什么办法可以做到这一点吗?

解决方案

WARNING/!\

您正在尝试在源文件夹 src/main/java 下生成源.除非有非常充分的理由,不要这样做.所有生成的内容应始终放置在构建目录下(默认为 target)并且不受版本控制.您始终可以使用 <添加生成的源作为源文件夹code>build-helper-maven-plugin:add-source,如果插件没有自己做.

<小时>

为了能够直接在命令行上设置参数,插件需要定义一个用户属性.但是,org.jvnet.jax-ws-commons:jaxws-maven-plugin 没有为 sourceDestDir 参数.这很明显,因为文档没有设置用户属性".

你也可以找到这个在源代码中:

<块引用>

@Parameter(defaultValue = "${project.build.directory}/generated-sources/wsimport")私有文件 sourceDestDir;

@Parameter注解,用于声明Maven插件的参数,没有对应的property.

因此,您需要具备以下条件:

  1. 定义一个Maven属性jaxws.sourceDestDir,值为${project.basedir}/src/main/java with

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

    最好使用 ${project.build.directory}/some/path 而不是 src/main/java.

  2. 配置插件以使用此 Maven 属性:

    <预><代码><配置><sourceDestDir>${jaxws.sourceDestDir}</sourceDestDir></配置>

  3. 如果您想覆盖它,现在可以直接在命令行上使用 -Djaxws.sourceDestDir=/my/new/value 执行此操作.此系统属性将优先于 Maven 属性的值.

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

解决方案

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.

这篇关于如何从 CLI 将参数传递给 Maven 插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆