gpt4 book ai didi

java - 从命令行覆盖 Maven 插件中的属性

转载 作者:行者123 更新时间:2023-12-01 14:27:54 27 4
gpt4 key购买 nike

此 maven 命令启动 H2 数据库的旧版本 (1.3.162):

mvn -debug com.edugility:h2-maven-plugin:1.0:spawn

推荐here ,我试图在命令行上覆盖插件的属性....因此我可以改用更新的 h2 版本:

mvn -debug -Dh2Version=1.4.200 com.edugility:h2-maven-plugin:1.0:spawn

这个具有旧 h2 版本的 h2Version 属性被定义为 here on github在插件的 pom 中。

这是详细的 maven 输出的结尾

 [DEBUG] Process arguments: [C:\java\jdk-9.0.4\bin\java, -cp, C:\Users\eoste\.m2\repository\com\h2database\h2\1.3.162\h2-1.3.162.jar, org.h2.tools.Server, -tcp, -tcpPassword, h2-maven-plugin, -tcpPort, 9092]
[INFO] H2 server spawned at tcp://localhost:9092

不仅启动了旧的 1.3.162 版本,而且我在命令行上放置的 h2Version 属性的任何地方都没有提及。

我尝试将 -Dh2Version 参数移动到命令行的末尾。我还尝试从本地存储库中删除插件,以强制下载,这样也许 h2Version 会被重新评估....这些都没有用。 This blog展示了如何在插件中嵌入依赖项,但这比我简单的命令行调用要复杂很多。

我做错了什么?

使用 windows 10、java 9、maven 3.6.2

最佳答案

What am I doing wrong?

1) 当你想使用未维护的插件/库时要小心。源代码从大约 8 年没有更新。那可能有重要的问题。

2) 要了解如何使用 maven 插件,请不要查看 pom 声明。您可以在 中找到一些信息,但您会在 mojo 实现/规范中找到更多信息。
但事实上不,你甚至不应该依赖它来理解如何使用插件。

3) 事实上,Maven 插件可能支持可配置的属性:直接在 pom.xml 中,甚至导出它们以供命令行使用。但这不是自动的。但在这两种情况下,必须由插件开发人员预见,并且通常记录在插件或源存储库主页上。

事实上,在您的情况下,如果您进入 Mojo 实现:AbstractH2Mojo ,你可以看到配置是如何设置的。
所有属性在 mojo 构造函数中都有默认值。

 protected AbstractH2Mojo() {
super();
final Service tcpService = new Service("tcp", Service.getDefaultPort("tcp"), false, false);
this.setServices(Collections.singletonList(tcpService));
this.setPort(Service.getDefaultPort("tcp"));
this.setShutdownPassword("h2-maven-plugin");
this.setJava(new File(new File(new File(System.getProperty("java.home")), "bin"), "java"));
}

首先调用 mojo 空构造函数,然后在创建的实例上调用所有 setter。
这意味着您可以通过提供诸如 ${artifactIdPrefixWithoutMavenPlugin}.field.
之类的属性,在运行时覆盖该类中定义的任何这些属性。由于maven插件是h2-maven-pluginthe prefix to referh2

如果你运行它:

mvn -X com.edugility:h2-maven-plugin:1.0:spawn -Dh2.port=8084 -Dh2.useSSL=false

你可以在输出中看到:

[DEBUG] Configuring mojo 'com.edugility:h2-maven-plugin:1.0:spawn' with basic configurator -->[DEBUG]   (s) port = 8084[DEBUG]   (s) shutdownHost = localhost[DEBUG]   (s) shutdownPassword = h2-maven-plugin[DEBUG]   (s) useSSL = false[DEBUG] -- end configuration --[DEBUG] Process arguments: [/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java, -cp, /home/david/.m2/repository/com/h2database/h2/1.3.162/h2-1.3.162.jar, org.h2.tools.Server, -tcp, -tcpPassword, h2-maven-plugin, -tcpPort, 8084]

Concerning the h2 jar used, if you still look in the same class, you will see that part that retrieves the jar file from the classpath :

 public final File getH2() {
final ProtectionDomain pd = Server.class.getProtectionDomain();
assert pd != null;
final CodeSource cs = pd.getCodeSource();
assert cs != null;
final URL location = cs.getLocation();
assert location != null;
try {
return new File(location.toURI());
} catch (final URISyntaxException wontHappen) {
throw (InternalError)new InternalError().initCause(wontHappen);
}
}

这意味着您无法更改使用的 H2 JAR:当您运行插件时从命令行或从 pom.xml 中的插件声明,因为 Mojo 中没有定义属性来实现这一点。

如果要更改H2版本,需要更改插件嵌入的版本。作为初学者,您可以尝试 fork 插件 GIT 存储库,更改 pom 中使用的 h2 依赖项以匹配您的要求,并检查是否尽管存在差距版本,但可以使用该插件。

请注意,您可以添加 Mojo 的新属性以使其完全可配置,例如:

mvn ... -Dh2Version=1.4.200 

但在那种情况下,您将需要检索它。例如,通过执行从 m2 中央仓库下载依赖项的请求。
并且您还应该确保只使用 h2 版本的有效范围。

关于java - 从命令行覆盖 Maven 插件中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60257565/

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