在Maven插件中获取Mojo参数 [英] get mojo parameters in maven plugin

查看:563
本文介绍了在Maven插件中获取Mojo参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在执行方法内访问插件属性?

Is there any way to access a plugins properties within the execution method?

我有一个基本的mojo,它具有一些属性,例如:

I have a base mojo that has some properties, like so:

@Parameter(defaultValue = "DEV", property = "dbEnvironment", required = true)
protected Environment dbEnvironment;

@Parameter(defaultValue = "true", property = "validate")
protected boolean validate;

然后,子项mojo添加了一些其他属性.我希望能够读取所有这些属性,以对其进行验证,但是如何做到这一点并不明显.当我运行它并进行调试时,会看到以下内容:

The child mojo then adds some additional properties. I'd like to be able to read all of these properties, to validate them, but it's not obvious how to do so. When I run it, with debug, I see this:

[DEBUG] Configuring mojo 'com.company.tools:something-maven-plugin:0.2.11-SNAPSHOT:export-job' with basic configurator -->
[DEBUG]   (f) dbEnvironment = DEV
[DEBUG]   (f) jobName = scrape_extract
[DEBUG]   (f) project = MavenProject: com.company.tools:something-maven-plugin-it:1.0-SNAPSHOT @ /Users/selliott/intellij-workspace/tools-something-maven-plugin/something-maven-plugin/src/it/simple-it/pom.xml
[DEBUG]   (f) session = org.apache.maven.execution.MavenSession@3fd2322d
[DEBUG]   (f) validate = true
[DEBUG] -- end configuration --

所以,看起来那些道具在某个地方,但是在哪里?我试图从session,session.settings,session.request中获取它们无济于事.

So, it looks like those props are somewhere, but where? I've tried getting them from the session, session.settings, session.request to no avail.

推荐答案

好吧,经过大量调试,我能够根据AbstractConfigurationConverter的工作原理(特别是fromExpression方法)来弄清楚.

Ok, after much debugging, I was able to figure it out based on how AbstractConfigurationConverter works, specifically the fromExpression method.

要获取这些属性,您需要将以下内容添加到您的mojo中:

To get the properties you need the following added to your mojo:

@Parameter(defaultValue = "${session}")
protected MavenSession session;

@Parameter(defaultValue = "${mojoExecution}")
protected MojoExecution mojoExecution;

现在,您可以在此处创建一个评估程序和配置(不确定,也许可以直接将其注入),并可以执行以下操作:

From there, you can now create an evaluator and configuration (maybe you can inject them in directly, I'm not sure), and with that you can do this:

    PluginParameterExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator(session, mojoExecution);
    PlexusConfiguration pomConfiguration = new XmlPlexusConfiguration(mojoExecution.getConfiguration());

    for (PlexusConfiguration plexusConfiguration : pomConfiguration.getChildren()) {
        String value = plexusConfiguration.getValue();
        String defaultValue = plexusConfiguration.getAttribute("default-value");
        try {
            String evaluated = defaultIfNull(expressionEvaluator.evaluate(defaultIfBlank(value, defaultValue)), "").toString();
            System.out.println(plexusConfiguration.getName() + " -> " + defaultIfBlank(evaluated, defaultValue));
        } catch (ExpressionEvaluationException e) {
            e.printStackTrace();
        }
    }

这篇关于在Maven插件中获取Mojo参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆