字符串数组作为 Maven 插件的命令行参数 [英] String arrays as command line arguments for maven plugin

查看:20
本文介绍了字符串数组作为 Maven 插件的命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 maven 插件,它的参数是一个 String[].

I'm writing a maven plugin that has a parameter that's a String[].

像这样:

/**
* @parameter expression="${args}"
*/
protected String[] args;

这可以通过 POM 使用,如下所示:

This can be utilized through the POM like this:

<args>
  <arg>arg1</arg>
  <arg>arg2</arg>
<args>

但是我想从命令行发送它

But I want to send it in from the command line

-Dargs={arg1, arg2}

这可能吗?

推荐答案

据我所知,您不能直接执行此操作,但接受一个分隔的 String 并自己将其拆分为数组是很常见的做法.

You can't do it directly as far as I know, but it is pretty common practice to accept a delimited String and split that into an array yourself.

例如 maven-site-plugin 允许您指定一个逗号分隔的语言环境字符串,而 maven-scala-plugin 通过允许您使用管道分隔符定义参数来处理此问题.您可以查看相关的 Mojo 以了解参数是如何处理的.

For example the maven-site-plugin allows you to specify a comma-delimited String of locales, while the maven-scala-plugin handles this by allowing you to define the arguments with a pipe separator. You can look at the relevant Mojos to see how the argument is processed.

下面的一些示例用法:

站点插件:

-Dlocales=enGB,frFR

scala 插件:

-DaddArgs=arg1|arg2|arg3

更新:如果你想更优雅地处理这个问题,你可以使用 maven-shared-io 来允许定义外部描述符文件,然后将描述符位置作为属性传递.这意味着单个命令行参数可以引用配置结构.

Update: if you want to handle this more elegantly, you could use maven-shared-io to allow definition of an external descriptor file, then pass the descriptor location as a property. This means a single command-line argument can reference a structure of configuration.

如果这听起来对你有用,看看这个 answer 描述了如何在属性插件中使用外部描述符,或者这个 answer 与 xml-maven-plugin 类似.或者您可以查看 assembly-plugin 以获取想法.

If this sounds like it might work for you, have a look at this answer that describes how to use external descriptors in the properties plugin, or this answer that does similar for the xml-maven-plugin. Or you can just look at the assembly-plugin for ideas.

这篇关于字符串数组作为 Maven 插件的命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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