简体   繁体   English

maven插件参数仅在执行中可用

[英]maven plugin parameter only available in execute

I'm writing a maven plugin with parameters. 我正在编写带有参数的Maven插件。

The parameters are correctly initiated 参数已正确启动

[DEBUG] Configuring mojo '...' with basic configurator -->
[DEBUG]   (f) includes = [subpackage]
[DEBUG]   (f) outputDirectory = /Users/username/dev/project/output
[DEBUG]   (f) sources = [/Users/username/dev/project/src/main/java/io/packagea, /Users/username/dev/project/src/main/java/io/packageb]

The parameters are correctly initiated in method execute , but are then null in called methods : raises NullPointerException. 参数在方法execute中正确初始化,但在被调用的方法中为null :引发NullPointerException。 If I pass the parameter of the mojo as parameters to called methodes, then 如果我将mojo的参数作为参数传递给被调用的方法,则

Here an example parameter : 这里是一个示例参数:

@Parameter
private String[] includes;

I do not identify why. 我不知道为什么。

The full code is on my github repo (on feature branch) : [ branch deleted as answer is clear on the origin of the issue ] 完整的代码在我的github repo上(在功能分支上):[ 删除分支,因为答案在问题的根源上是明确的 ]

plugin can be called with mvn livingdoc:wordcloud on a project. 可以在项目上使用mvn livingdoc:wordcloud调用该插件。

The plugin browse code for @Wordcloud annotations but no need to reproduce issue. 插件浏览@Wordcloud批注的代码,但无需重现问题。

A Maven plugin is represented by a class that implements the Mojo interface (typically extends AbstractMojo ). 一个Maven插件由实现Mojo接口的类表示(通常extends AbstractMojo )。 It's annotated and such, so that Maven can recognize it, instantiate it (create a new instance) and execute it (call the execute method). 它带有这样的注释,以便Maven可以识别它,实例化它(创建一个新实例)并执行它(调用execute方法)。 In your case, this looks something like this: 您的情况如下所示:

@Mojo(name = "wordcloud")
public class WordCloudMojo extends AbstractMojo {

    @Parameter
    private String[] includes;

...

Maven makes sure that dependencies are satisfied, by injecting these into the new instance of your class, for example the includes field above. Maven通过将依赖项注入到类的新实例中来确保满足依赖关系,例如,上面的includes字段。

If you create any other new and different instances of your plugin class, Maven doesn't concern itself with that, and dependencies will not be satisfied. 如果您创建插件类的任何其他新实例和其他实例,则Maven不会对此进行关注,并且将无法满足依赖关系。 In your case, like so: 在您的情况下,如下所示:

public void execute() {
    final WordCloudMojo wordCloudMojo = new WordCloudMojo();
    Arrays.stream(includes).forEach(inc -> getLog().debug(inc));
    wordCloudMojo.scan(sources);

The stream(includes) will still use the Maven-injected dependency. stream(includes)仍将使用Maven注入的依赖项。 But since you call scan on a new instance of WordCloudMojo , dependencies will not be satisfied (the field will likely be null ). 但是,由于您对WordCloudMojo的新实例调用了scan ,因此将无法满足依赖关系(该字段可能为null )。

This would solve that: 这样可以解决:

final WordCloudMojo wordCloudMojo = new WordCloudMojo();
wordCloudMojo.includes = this.includes;

Excuse me for not using a mutator method. 对不起,我没有使用mutator方法。 The this is redundant here, but emphasis the problem a bit. this在这里是多余的,但要强调一点问题。

Another solution would be: don't create a new instance of WordCloudMojo ? 另一个解决方案是:不创建WordCloudMojo的新实例? What's wrong with the one that Maven already made for you? Maven已经为您制作的那是什么问题?

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

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