gpt4 book ai didi

java - 如何在 Maven Mojo 中使用整个插件配置

转载 作者:行者123 更新时间:2023-11-30 06:21:26 25 4
gpt4 key购买 nike

我正在尝试构建一个在内部使用 Spring Boot 应用程序的 Maven 插件。

现在,我想使用 <configuration> 使 Spring Boot 应用程序可配置。标签 pom.xml .

<plugin>
<groupId>org.example</groupId>
<artifactId>my-maven-plugin</artifactId>
<version>0.1.0-SNAPSHOT</version>
<configuration>
<my-maven-plugin.key1>value1</my-maven-plugin.key1>
<my-maven-plugin.key2>value2</my-maven-plugin.key2>
</configuration>
</plugin>

可以使用 @Parameter 将这些属性注入(inject)到 Mojo 中注释。

@Mojo(name = "generate", defaultPhase = LifecyclePhase.SITE)
public class GenerateMojo extends AbstractMojo {

@Parameter(name = "my-maven-plugin.key1", readonly = true)
private String key1;

@Parameter(name = "my-maven-plugin.key2", readonly = true)
private String key2;

@Override
public void execute() {

Properties props = new Properties();
props.setProperty("my-maven-plugin.key1", key1);
props.setProperty("my-maven-plugin.key2", key2);

new SpringApplicationBuilder(GenerateApp.class)
.web(false)
.bannerMode(OFF)
.logStartupInfo(false)
.properties(props)
.run();
}
}

一个Properties正在创建可在 Spring 中使用的对象。但我想避免构建 Properties靠我自己。

虽然我可以重新排列 <configuration>像这样

<configuration>
<my-maven-plugin>
<key1>value1</key1>
<key2>value2</key2>
</my-maven-plugin>
</configuration>

并像这样注入(inject)所有属性

@Parameter(name = "my-maven-plugin", readonly = true)
private Properties myProperties;

我不想将所有属性包装在另一个标签中,因为这在 Maven 插件中感觉更自然。

有没有办法将整个配置注入(inject)单个Properties Maven 中的对象而不将属性包装在另一个标签中?一个Map<String, Object>或类似的对我来说也很好。

最佳答案

我认为没有直接的方法可以满足您的要求。不过,您可以尝试以下替代方案。

1)您可以使用项目属性:

<project>
...
<properties>
<key1>value1</key1>
<key2>value1</key2>
</properties>
...
</project>

2) 有几种方法可以在运行时访问 mojo 配置:参见答案 here .

3) 这更像是一个想法,而不是真正的答案:您可以查看 @Parameter 注释的工作原理,并将代码添加到您的插件中,该插件将使用相同的代码注释并填充 MapProperties 对象。

关于java - 如何在 Maven Mojo 中使用整个插件配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48085705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com