字符串数组作为 Maven 插件的命令行参数

发布于 2024-08-03 04:49:00 字数 364 浏览 4 评论 0原文

我正在编写一个 Maven 插件,它有一个 String[] 参数。

像这样:

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

这可以通过 POM 来使用,像这样:

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

但我想从命令行发送它

-Dargs={arg1, arg2}

这可能吗?

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

Like this:

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

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}

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暮色兮凉城 2024-08-10 04:49:00

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

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

下面是一些示例用法:

site-plugin:

-Dlocales=enGB,frFR

scala-plugin:

-DaddArgs=arg1|arg2|arg3

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

如果这听起来可能对您有用,请看看这个 答案,描述如何在属性插件中使用外部描述符,或者这个answer 与 xml-maven-plugin 类似。或者您可以查看汇编插件以获取想法。

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.

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.

Some example usages below:

site-plugin:

-Dlocales=enGB,frFR

scala-plugin:

-DaddArgs=arg1|arg2|arg3

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.

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.

请远离我 2024-08-10 04:49:00

最新的 maven (3.0.3) 应该适用于:

-DaddArgs=arg1,arg2,arg3

Latest maven (3.0.3) should works with:

-DaddArgs=arg1,arg2,arg3

z祗昰~ 2024-08-10 04:49:00

要更新一下 @nybon 的答案,它似乎

@Parameter(property="your.param")
private List<String> yourParam;

有效,至少在 Maven 3.5.0 中使用 maven-plugin-annotations:3.5 时是这样。运行 with

-Dyour.param=val1,val2

设置列表。

To update on @nybon’s answer a bit, it seems

@Parameter(property="your.param")
private List<String> yourParam;

works, at least when using maven-plugin-annotations:3.5 in Maven 3.5.0. Running with

-Dyour.param=val1,val2

sets the list.

哎呦我呸! 2024-08-10 04:49:00

根据Sonatype的博客此处,如果您是一名插件开发人员并且

  1. 使用 Maven 3

  2. 并使用以下注释来注释您的数组/集合类型插件参数:

    /** @parameter expression="${args}" */

这样,插件参数可以由 Maven 自动处理并插件用户可以通过 CLI 使用逗号分隔的系统属性(如 mvn myplugin:mygoal -Dargs=a,b,c)提供插件数组/集合类型参数

According to Sonatype's blog here, if you are a plugin developer and

  1. use Maven 3

  2. and annotate your array/collection type plugin parameter using annotation like:

    /** @parameter expression="${args}" */

In this way, the plugin parameter can be processed by Maven automatically and plugin users can provide the plugin array/collection type parameters via CLI using a comma separated system property like mvn myplugin:mygoal -Dargs=a,b,c

少跟Wǒ拽 2024-08-10 04:49:00

通过系统属性为插件指定值列表的方法取决于插件的最新程度。

但是,如果您正在处理正确实现的最新插件,那么为插件指定值数组的正确方法是通过逗号分隔的字符串。

这是一个参考:
http://blog.sonatype.com/2011 /03/configuring-plugin-goals-in-maven-3/

这是参考文献中的引用:

对于许多插件参数,有时指定起来很方便
通过系统属性从命令行获取它们的值。在过去,
这仅限于简单类型的参数,如字符串或布尔值。
最新的 Maven 版本终于允许插件用户进行配置
通过逗号分隔从命令行获取集合或数组
字符串。以这样的插件参数为例:

再进一步,我们可以看更具体的示例。
考虑一下 Wildfly maven 插件。
该插件有一个已弃用的配置属性,称为:
jvmArgs。

预计这将作为空格分隔的值列表传入。
众所周知,在命令行中,乱搞空格并不可爱。
因此,如果我们在插件 mojo 代码中查看此参数的定义,您会发现类似的内容(这里有另一个引用)。

/**
 * A space delimited list of JVM arguments.
 *
 * @deprecated use {@link #javaOpts}
 */
@Parameter(alias = "jvm-args", property = PropertyNames.JVM_ARGS)
@Deprecated
private String jvmArgs;

所以这是旧的做事方式。

现在,如果您使用的是该插件的最新版本(例如Alpha6)。
然后源代码将有一个很好的新字段,称为 javaOpts。
让我们看看代码中该字段是什么样的。

/**
 * The JVM options to use.
 */
@Parameter(alias = "java-opts", property = PropertyNames.JAVA_OPTS)
private String[] javaOpts;

所以我们看到 StartMojo 中有一个很好的数组字段。
该数组字段已正确注释。
Maven 引擎将完成将值设置到 Mojo 中的繁重工作。

当您想通过命令行将数据泵入此字段时,您可以在批处理文件中指定以下形式的内容:

-Dwildfly.javaOpts="-Xmx1536M,-Xms1536M,-XX:MaxMetaspaceSize=512M,-XX:-HeapDumpOnOutOfMemoryError"

如果您使用 sapces 而不是命令尝试相同的操作。
我将向您展示会发生什么:

[INFO] 独立服务器正在启动。最大堆大小无效:
-Xmx1536M -XX:MaxMetaspaceSize=512m -XX:-HeapDumpOnOutOfMemoryError

所以你看,maven 当它吞掉我充满空格的系统属性时,它没有进行字符串分割。因此 Wildfly 尝试设置 jvm 内存设置,就好像最大内存是完整的字符串一样。
另一方面,当我使用逗号分隔它时,Mojo 得到了适当的丰富,我可以在应用程序服务器启动时控制它的内存设置。

当然,对于设置 Jenkins 作业等任务,您希望使用系统属性而不是 pom.xml XML 配置。有了系统属性,你就变得更加灵活。

就是这样。

The way to specify a list of values via system property, for a plugin depends on how up to date the plugin is.

However, if you are dealing with a proper implemented plugin that is up to date, then the correct way of specifying an array of values to a plugin is via comma separated strings.

Here is a reference:
http://blog.sonatype.com/2011/03/configuring-plugin-goals-in-maven-3/

Here is a quote from the reference:

For many plugin parameters it is occasionally convenient to specify
their values from the command line via system properties. In the past,
this was limited to parameters of simple types like String or Boolean.
The latest Maven release finally allows plugin users to configure
collections or arrays from the command line via comma-separated
strings. Take for example a plugin parameter like this:

Going a little bit further, we can look at more concrete example.
Consider, the Wildfly maven plugin.
This plugin has a deprecated configuration property called:
jvmArgs.

This was expected to be passed in as a space separated list of values.
As we all know, in the command line, messing around with spaces is not adorable.
So if we look at the definition of this paramter in the plugin mojo code, you will find something like this (here goes another quote).

/**
 * A space delimited list of JVM arguments.
 *
 * @deprecated use {@link #javaOpts}
 */
@Parameter(alias = "jvm-args", property = PropertyNames.JVM_ARGS)
@Deprecated
private String jvmArgs;

So this is the old way of doing stuff.

Now, if you are using the latest version of this plugin (e.g. Alpha6).
Then the source code will have a nice new field called javaOpts.
Let us look at what the field looks like in the code.

/**
 * The JVM options to use.
 */
@Parameter(alias = "java-opts", property = PropertyNames.JAVA_OPTS)
private String[] javaOpts;

So what we see is that we have a nice array field in the StartMojo.
This array field is properly annoted.
And the maven engine will do the heavy lifting of setting the values into the Mojo.

When you want to pump data into this field via the command line, you would in you batch file specify something of the form:

-Dwildfly.javaOpts="-Xmx1536M,-Xms1536M,-XX:MaxMetaspaceSize=512M,-XX:-HeapDumpOnOutOfMemoryError"

If you try the samething using sapces instead of commans.
I will show you what happens:

[INFO] STANDALONE server is starting up. Invalid maximum heap size:
-Xmx1536M -XX:MaxMetaspaceSize=512m -XX:-HeapDumpOnOutOfMemoryError

So you see, maven when it with swallowed my system property full of spaces it did not do a string split. So Wildfly tried to setup the jvm memory settings as if the max memory was that full string.
On the other hand, when I use commas to separate it, the Mojo is properly enriched and I can take control of the memory settings of the app server when it starts up.

And of course, you want to use system properties and not pom.xml XML configuration, for tasks like setting up Jenkins jobs. With system properties you are rather more flexible.

That is it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文