问题描述
我是Maven的新手.当我尝试引用任何Maven插件文档时,我总是会看到以下格式的参数定义:名称说明{参数名称} {说明} 默认值为:... 用户属性是:...
I am a newbie to Maven.When I try to refer to any maven plugin document, I always see following format for the parameter definition:Name Description{parameter name} {description} Default Value is: ... User property is: ...
在大多数情况下,我看到用户属性与参数名称相同.我想知道用户属性"和参数名称"之间有什么区别.我认为在配置它们时指定相应插件的参数值时,参数名称应为元素标签名称,但是用户属性"的用途是什么?
For most cases, I saw the user property is the same with parameter name.I wonder here what's the difference between "User Property" and "Parameter Name". I think the parameter name should be element tag name when specify the parameter value of the correspond plugin when configure them, but what's the usage of "User Property"?
推荐答案
注意:这是针对Maven 3的.早期版本有所不同.
TL; DR:
用户属性"指定可以使用的 Maven属性的名称设置插件参数.这允许从<configuration>
部分之外配置插件.请注意,这仅在<configuration>
部分中未指定 的情况下有效(请参见 MNG-4979-无法从命令行覆盖配置参数).
"User property" specifies the name of the Maven property that can be used to set a plugin parameter. This allows configuring a plugin from outside the <configuration>
section. Note that this only works if the parameter is not specified in the <configuration>
section (see MNG-4979 - Cannot override configuration parameter from command line).
Maven属性可以在POM的<properties>
部分中设置,也可以在命令行中设置为-DmyProperty=myValue
.
Maven properties can be set in the POM in the section <properties>
, or on the command line as -DmyProperty=myValue
.
长版
在Maven中,通常在POM的 <configuration>
部分中设置插件的参数.插件文档中提供的参数名称"是在配置设置中使用的名称.
In Maven, the parameters of a plugin are usually set in an <configuration>
section in the POM. The "parameter name" given in the plugin's documentation is the name to be used in the configuration setting.
例如,Surefire目标 surefire:test 具有参数"failIfNoSpecifiedTests".要将其设置在POM中,请使用:
For example, the Surefire goal surefire:test has a parameter "failIfNoSpecifiedTests". To set it in the POM, use:
<configuration>
<failIfNoSpecifiedTests>false</failIfNoSpecifiedTests>
</configuration>
但是,有时在<configuration>
部分之外设置插件参数很有用,例如,在命令行或Maven配置文件中进行设置.为此,插件还可以声明它将从Maven属性(如果已设置)中读取参数值.此属性是文档在 User属性中列出的内容.
However, sometimes it is useful to set a plugin parameter from outside the <configuration>
section, for example to set it on the command line, or in a Maven profile. To allow this, a plugin can also declare that it will read the parameter value from a Maven property (if set). This property is what the docs list as the User property.
failIfNoSpecifiedTests的用户属性为surefire.failIfNoSpecifiedTests
.因此,也可以使用属性surefire.failIfNoSpecifiedTests代替上面的<configuration>
部分.例如:
The User property for failIfNoSpecifiedTests is surefire.failIfNoSpecifiedTests
. So instead of the <configuration>
section above, one could also use the property surefire.failIfNoSpecifiedTests. For example:
- 在命令行上
- 通过指定
-Dsurefire.failIfNoSpecifiedTests=false
- 在POM配置文件中:
<properties> <surefire.failIfNoSpecifiedTests> false </surefire.failIfNoSpecifiedTests> ...
- on the command line, by specifying
-Dsurefire.failIfNoSpecifiedTests=false
- in a POM profile:
<properties> <surefire.failIfNoSpecifiedTests> false </surefire.failIfNoSpecifiedTests> ...
请注意,插件必须为每个参数声明User属性,因此并非所有参数都具有一个.例如,参数basedir
没有用户属性,因此无法在命令行上设置.
Note that the User property must be declared by the plugin for each parameter, so not all parameters will have one. For example, the parameter basedir
does not have a User property, so can not be set on the command line.
在插件的源代码中,未明确声明参数名称;它取自Java字段的名称. "failIfNoSpecifiedTests"的源代码为:
In the source code of the plugin, the parameter name not explicitly declared; it is taken from the name of the Java field. The source code for "failIfNoSpecifiedTests" is:
/**
* Set this to "true" to cause a failure if the none of the tests
* specified in -Dtest=... are run. Defaults to "true".
*
* @since 2.12
*/
@Parameter( property = "surefire.failIfNoSpecifiedTests" )
private Boolean failIfNoSpecifiedTests;
您可以看到参数名称"failIfNoSpecifiedTests"是从字段名称中获取的.注释参数属性"定义了要使用的Maven属性.
You can see that the parameter name "failIfNoSpecifiedTests" is taken from the field name. The annotation parameter "property" defines the Maven property to use.
这篇关于Maven插件参数的用户属性是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!