从命令行覆盖 Maven 插件中的 属性
overriding property in maven plugin from command line
此 maven 命令启动旧版本 (1.3.162) 的 H2 数据库:
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
此 h2Version 属性 与较旧的 h2 版本在插件的 pom 中定义 here on github。
这里是详细的 maven 输出的结尾
[DEBUG] Process arguments: [C:\java\jdk-9.0.4\bin\java, -cp, C:\Users\eoste\.m2\repository\com\h2database\h2.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 会被重新评估....none 这些东西起作用了。
This blog 展示了如何在插件中嵌入依赖项,但这比我简单的命令行调用要复杂很多。
我做错了什么?
使用windows10,java9,maven 3.6.2
我认为您无法使用这个特定的 maven 插件来做到这一点。这是对有类似问题的人的另一个答案:.
基本上,h2Version
属性 未定义为用户 属性。
当您启动插件时,使用您提到的命令,有预定义配置属性的输出:
<configuration>
<allowOthers>${h2.allowOthers}</allowOthers>
<baseDirectory>${h2.baseDirectory}</baseDirectory>
<forceShutdown>${h2.forceShutdown}</forceShutdown>
<ifExists>${h2.ifExists}</ifExists>
<java>${h2.java}</java>
<port default-value="9092">${h2.port}</port>
<shutdownAllServers>${h2.shutdownAllServers}</shutdownAllServers>
<shutdownHost default-value="localhost">${h2.shutdownHost}</shutdownHost>
<shutdownPassword default-value="h2-maven-plugin">${h2.shutdownPassword}</shutdownPassword>
<trace>${h2.trace}</trace>
<useSSL>${h2.useSSL}</useSSL>
</configuration>
用户只能定义这些属性。例如,更改 运行 端口:
mvn -debug com.edugility:h2-maven-plugin:1.0:spawn -Dport=9090
What am I doing wrong?
1) 当您想使用 plugins/libraries 未维护时请注意。源代码从大约 8 年没有更新。那可能有重要的问题。
2) 要了解如何使用 maven 插件,请不要查看 pom 声明。您可以在中找到一些信息,但您会在 mojo implementation/specification.
中找到更多信息
但事实上不,你甚至不应该依赖它来理解如何使用插件。
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
.
)在 运行 时覆盖 class 中定义的任何这些属性
由于maven插件是h2-maven-plugin
,the prefix to refer是h2
。
如果你 运行 :
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]
关于使用的 h2 jar,如果您仍然查看相同的 class,您将看到从 class 路径检索 jar 文件的部分:
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 版本的有效范围。
此 maven 命令启动旧版本 (1.3.162) 的 H2 数据库:
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
此 h2Version 属性 与较旧的 h2 版本在插件的 pom 中定义 here on github。
这里是详细的 maven 输出的结尾
[DEBUG] Process arguments: [C:\java\jdk-9.0.4\bin\java, -cp, C:\Users\eoste\.m2\repository\com\h2database\h2.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 会被重新评估....none 这些东西起作用了。 This blog 展示了如何在插件中嵌入依赖项,但这比我简单的命令行调用要复杂很多。
我做错了什么?
使用windows10,java9,maven 3.6.2
我认为您无法使用这个特定的 maven 插件来做到这一点。这是对有类似问题的人的另一个答案:
基本上,h2Version
属性 未定义为用户 属性。
当您启动插件时,使用您提到的命令,有预定义配置属性的输出:
<configuration>
<allowOthers>${h2.allowOthers}</allowOthers>
<baseDirectory>${h2.baseDirectory}</baseDirectory>
<forceShutdown>${h2.forceShutdown}</forceShutdown>
<ifExists>${h2.ifExists}</ifExists>
<java>${h2.java}</java>
<port default-value="9092">${h2.port}</port>
<shutdownAllServers>${h2.shutdownAllServers}</shutdownAllServers>
<shutdownHost default-value="localhost">${h2.shutdownHost}</shutdownHost>
<shutdownPassword default-value="h2-maven-plugin">${h2.shutdownPassword}</shutdownPassword>
<trace>${h2.trace}</trace>
<useSSL>${h2.useSSL}</useSSL>
</configuration>
用户只能定义这些属性。例如,更改 运行 端口:
mvn -debug com.edugility:h2-maven-plugin:1.0:spawn -Dport=9090
What am I doing wrong?
1) 当您想使用 plugins/libraries 未维护时请注意。源代码从大约 8 年没有更新。那可能有重要的问题。
2) 要了解如何使用 maven 插件,请不要查看 pom 声明。您可以在中找到一些信息,但您会在 mojo implementation/specification.
中找到更多信息
但事实上不,你甚至不应该依赖它来理解如何使用插件。
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
.
)在 运行 时覆盖 class 中定义的任何这些属性
由于maven插件是h2-maven-plugin
,the prefix to refer是h2
。
如果你 运行 :
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]
关于使用的 h2 jar,如果您仍然查看相同的 class,您将看到从 class 路径检索 jar 文件的部分:
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 版本的有效范围。