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

查看:951
本文介绍了字符串数组作为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>

但我想从命令行发送

-Dargs={arg1, arg2}

这是可能吗?

推荐答案

尽管我知道你不能直接做,但是这是很常见的做法,接受一个分隔的字符串并自己将它拆分成一个数组。

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 通过允许您使用管道分隔符定义参数来处理此问题。

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.

下面是一些例子:

site-plugin:

site-plugin:

-Dlocales=enGB,frFR

scala-plugin:

scala-plugin:

-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插件。或者你可以看看装配插件的想法。

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天全站免登陆