Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从命令行重写maven插件中的属性_Java_Maven_H2 - Fatal编程技术网

Java 从命令行重写maven插件中的属性

Java 从命令行重写maven插件中的属性,java,maven,h2,Java,Maven,H2,此maven命令启动H2数据库的旧版本(1.3.162): mvn -debug com.edugility:h2-maven-plugin:1.0:spawn 根据建议,我正在尝试在命令行上覆盖插件的属性……因此我可以使用更新的h2版本: mvn -debug -Dh2Version=1.4.200 com.edugility:h2-maven-plugin:1.0:spawn 此h2Version属性与较旧的h2版本在插件的pom中定义 下面是详细的maven输出的结尾 [DEBUG]

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

mvn -debug com.edugility:h2-maven-plugin:1.0:spawn
根据建议,我正在尝试在命令行上覆盖插件的属性……因此我可以使用更新的h2版本:

mvn -debug -Dh2Version=1.4.200 com.edugility:h2-maven-plugin:1.0:spawn
此h2Version属性与较旧的h2版本在插件的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参数移动到命令行的末尾。我还尝试从本地repo中删除插件,以强制下载,这样H2版本可能会被重新评估……这些都不起作用。 演示了如何在插件中嵌入依赖项,但这比我简单的命令行调用要复杂得多

我做错了什么


使用windows 10、java 9、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>
我做错了什么

1) 当您想使用未维护的插件/库时,请小心。源代码在大约8年后没有更新。这可能有重要的问题

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

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

事实上,在您的案例中,如果进入Mojo实现:,您可以看到配置是如何设置的。
所有属性在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"));
 }
首先调用mojoempty构造函数,然后在创建的实例上调用所有setter。
这意味着您可以在运行时通过提供诸如
${artifactIdPrefixWithoutMavenPlugin}.field

因为maven插件是
h2 maven插件
,所以它是
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:运行插件时从命令行或pom.xml中的插件声明,因为Mojo中没有定义任何属性来实现这一点

如果要更改H2版本,则需要更改插件嵌入的版本。作为初学者,您可以尝试分叉插件GIT存储库,更改pom中使用的h2依赖项以匹配您的需求,并检查是否有可能使用该插件(尽管是gap版本)

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

mvn ... -Dh2Version=1.4.200 
但在这种情况下,您需要检索它。例如,执行从m2中央回购协议下载依赖项的请求。

您还应该确保只使用h2版本的有效范围

这个答案很好,特别是指出调试输出中可用属性的列表,但我接受了davidxxx的答案,因为它还详细说明了前进的方向。thx for sharing.davidxxx,你所说的“因为那不会退出”是什么意思?你是说“存在”而不是“退出”吗?如果有其他问题,请解释,谢谢。
 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);
    }
 }
mvn ... -Dh2Version=1.4.200