Java 如何将Maven插件中@参数的defaultValue设置为方法的结果?
我正在开发一个自定义maven插件,用于创建自定义的打包类型。 我已经让它工作,可以建立我需要的格式的zip文件。 但是,我注意到一些配置元素和变量取决于zip文件的名称 zip文件有一个特殊的清单文件作为其格式的一部分。我希望参数componentName是一个合适的Java 如何将Maven插件中@参数的defaultValue设置为方法的结果?,java,maven,maven-3,maven-plugin,Java,Maven,Maven 3,Maven Plugin,我正在开发一个自定义maven插件,用于创建自定义的打包类型。 我已经让它工作,可以建立我需要的格式的zip文件。 但是,我注意到一些配置元素和变量取决于zip文件的名称 zip文件有一个特殊的清单文件作为其格式的一部分。我希望参数componentName是一个合适的@参数,以便其他属性可以通过${componentName}依赖它 我希望动态获取组件名称,而不是强制用户在其他位置指定它。清单文件有时可以包含它。如果没有,则在${project.basedir}中通常会有另一个与组件同名的文件
@参数
,以便其他属性可以通过${componentName}
依赖它
我希望动态获取组件名称,而不是强制用户在其他位置指定它。清单文件有时可以包含它。如果没有,则在${project.basedir}
中通常会有另一个与组件同名的文件
我定义了一个私有静态最终字符串getComponentName(File baseDir)
,用于在未提供组件名称时计算组件名称
然而
@Parameter(property = "componentName",
defaultValue = getComponentName("${project.basedir}"))
protected String componentName;
未使用文件中的ParseException:syntax error@[34,46]进行编译…
有没有办法将其配置为我想要的?如果是,怎么做?注释中的defaultValue不能是一个方法,因为注入将设置默认值并在那里计算某种java代码。因此,您必须走以下路径:
@参数(defaultValue=“${project.basedir}”,property=“componentName”)
私有字符串组件名;
此外,如果你需要一个getter,它需要做一些事情,那么你应该像我一样简单地将属性私有化,并使用getter访问它,在那里你可以做你喜欢的事情
defaultValue
的值可以在您可以看到可以用作默认值的地方进行查找。此外,我建议不要把东西放在${project.basedir}
中,因为所有要打包的东西都应该放在src/main/…
中,所以如果你有一些东西不是你的包的一部分,那么应该放在src/Supplemental/
中,而不是…这可能有些过分了,我想出了一个解决办法。我想让它成为一个具有默认值的属性,这样我就可以更容易地配置它。包括一些依赖配置属性 解决方案是为我的插件
init
添加一个新目标。它在初始化阶段运行。它执行逻辑来设置组件名
,然后运行一些附加逻辑来设置附加依赖字段的属性,这样以后的生命周期阶段目标仍然可以正常使用变量
步骤:
从
componentName
@Parameter(property = "componentName")
protected String componentName;
确定目标:
@Mojo(name = "init", defaultPhase = LifecyclePhase.INITIALIZE)
public class InitMojo extends AbstractComponentMojo
要使其在生命周期中默认运行,请将以下内容添加到components.xml文件:
org.ucmtwine:ucm maven插件:init
让init
的execute()
进行处理并设置结果
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
}
在`${project.basedir}`中,有一个文件manifest.hda可以包含组件名称,或者如果它不起作用,则应该有一个${componentName}.hda文件,我可以使用它来确定组件的名称。