如何从 CLI 将参数传递给 Maven 插件?

编程入门 行业动态 更新时间:2024-10-13 00:30:56
本文介绍了如何从 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

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

解决方案

WARNING/!\

您正在尝试在源文件夹 src/main/java 下生成源.除非有非常充分的理由,不要这样做.所有生成的内容应始终放置在构建目录下(默认为 target)并且不受版本控制.您始终可以使用 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.

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

  • 定义一个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.

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

    <sourceDestDir>${jaxws.sourceDestDir}</sourceDestDir></配置>

  • 如果您想覆盖它,现在可以直接在命令行上使用 -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 (jax-ws-commons.java/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:

  • 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.

  • Configure the plugin to use this Maven property:

    <configuration> <sourceDestDir>${jaxws.sourceDestDir}</sourceDestDir> </configuration>

  • 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 插件?

    本文发布于:2023-11-24 02:19:35,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1623613.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:插件   参数   CLI   Maven

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!