简体   繁体   English

如何设置Maven插件中@Parameter的defaultValue作为方法的结果?

[英]How do I set the defaultValue of a @Parameter in a Maven plugin as the result of a method?

I'm working on a custom maven plugin for creating a custom packaging type. 我正在开发用于创建自定义包装类型的自定义Maven插件。 I've got it working and can build the zip file in the format I need. 我已经可以正常工作了,并且可以用我需要的格式来构建zip文件。 However, I've noticed that some configuration elements and variables depend on the name of the zip file. 但是,我注意到一些配置元素和变量取决于zip文件的名称。

The zip file has a special manifest file as part of it's format. 压缩文件中有一个特殊的清单文件,是其格式的一部分。 I want the parameter componentName to be a proper @Parameter so that other properties can depend on it via ${componentName} . 我希望参数componentName是正确的@Parameter以便其他属性可以通过${componentName}依赖于它。

I'd like to get the component name dynamically rather than forcing my users to specify it in another place. 我想动态获取组件名称,而不是强迫用户在另一个地方指定它。 The manifest file can sometimes contain it. 清单文件有时可以包含它。 If not, there is usually another file in ${project.basedir} that will have the same name as the component. 如果不是,通常${project.basedir}中还有另一个文件,该文件的名称与组件相同。

I have a private static final string getComponentName(File baseDir) defined to calculate the component name when one isn't supplied. 我有一个private static final string getComponentName(File baseDir)定义,用于在不提供组件名称时计算组件名称。

However 然而

@Parameter(property = "componentName",
           defaultValue = getComponentName("${project.basedir}"))
protected String componentName;

does not compile with a ParseException: syntax error @[34,46] in file ... 无法使用ParseException: syntax error @[34,46] in file ...编译ParseException: syntax error @[34,46] in file ...

Is there a way to configure this to what I want? 有没有一种方法可以将此配置为我想要的? If so, how? 如果是这样,怎么办?

The defaultValue in a annotation can't be a method cause the injection will set the default value and evaluate some kind of java code there..So you have to go the following path: 注释中的defaultValue不能是一种方法,因为注入将设置默认值并在那里评估某种Java代码。因此,您必须走以下路径:

@Parameter ( defaultValue = "${project.basedir}", property="componentName")
private String componentName;

Furthermore if you need to have a getter which needs to do some than you should simply make the attribute private as i did and use a getter to access it where you can do supplemental things you like.. 此外,如果您需要一个需要执行某些操作的吸气剂,则应像我一样简单地将属性设为私有,并使用吸气剂访问该属性,以便您可以做自己喜欢的补充工作。

The values for defaultValue can be looked up here where you can see what you can use as default values. 可以在此处查找defaultValue的值, 其中可以看到可用作默认值的内容。 Furthermore i would suggest not to put something in the ${project.basedir} cause everything which will be packaged should be located into src/main/... so if you have something which will not being part of your package it should be located src/Supplemental/ instead... 此外,我建议不要在${project.basedir}放置某些内容,因为将要打包的所有内容都应位于src/main/...因此,如果您有某些内容不属于您的包,则应将其放置在其中src/Supplemental/代替...

It's probably overkill, however, I figured out a solution. 但是,我想出了一个解决方案。
I wanted to make it a property with a default value so I could make it more easily configurable. 我想将其设置为具有默认值的属性,以便使其更易于配置。 including some dependent config properties. 包括一些相关的配置属性。

The solution turned out to be adding a new goal to my plugin init . 原来,该解决方案为我的插件init添加了新的目标。 It runs during the initialize phase. 它在初始化阶段运行。 It does the logic to set the componentName and then runs some additional logic to set the properties for the additional dependent fields, so the later lifecycle phase goals can still use the variables as normal. 它执行逻辑来设置componentName ,然后运行一些其他逻辑来设置其他相关字段的属性,因此以后的生命周期阶段目标仍可以照常使用变量。

Steps: 脚步:


Remove the defaultValue from componentName componentName移除defaultValue

@Parameter(property = "componentName")
protected String componentName;

Define the Goal: 定义目标:

@Mojo(name = "init", defaultPhase = LifecyclePhase.INITIALIZE)
public class InitMojo extends AbstractComponentMojo

To have it run by default during the lifecycle add the following to the components.xml file: 要使其在生命周期内默认运行,请将以下内容添加到components.xml文件中:

<initialize>
  org.ucmtwine:ucm-maven-plugin:init
</initialize>

Have init 's execute() do the processing and set the result initexecute()进行处理并设置结果

public void execute() throws MojoExecutionException, MojoFailureException
{
  determineComponentName(); //logic to set componentName

  //set the property with the result //not sure which one is truly necessary
  project.getProperties().setProperty("componentName",       componentName);
  session.getUserProperties().setProperty("componentName",   componentName);
  session.getSystemProperties().setProperty("componentName", componentName);
  getLog().debug("Setting componentName: " + componentName);

  //force reset of the dependent variables
  if ( null == componentFileName || "${componentFileName}".equals(componentFileName) )
  {
     componentFileName = componentName + ".zip";
     project.getProperties().setProperty("componentFileName",       componentFileName);
     session.getUserProperties().setProperty("componentFileName",   componentFileName);
     session.getSystemProperties().setProperty("componentFileName", componentFileName);
     getLog().debug("Setting componentFileName: " + componentFileName);
  }

  // more variables get reset here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM