overriding property in maven plugin from command line

Erik Ostermueller :

This maven command launches an older version (1.3.162) of the H2 database:

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

As recommended here, I'm trying to override a property of the plugin on the command line....so I can instead use a more recent h2 version:

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

This h2Version property with the older h2 version is defined here on github in the plugin's pom.

Here is the end of the verbose maven output

 [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

Not only does the old 1.3.162 version launch, but the there is zero mention anywhere of the h2Version property that I placed on the command line.

I tried moving the -Dh2Version parameter to the end of the command line. I also tried deleting the plugin from the local repo, to force a download so maybe the h2Version would then get reevaluated....none of these things worked. This blog shows how to embed a dependency inside a plugin, but that's tons more complicated than my simple command line invocation.

What am I doing wrong?

Using windows 10, java 9, maven 3.6.2

davidxxx :

What am I doing wrong?

1) Beware when you want to use plugins/libraries not maintained. The source code was not updated from about 8 years. That may have important issues.

2) To know how to use a maven plugin, don't look in the pom declaration. You can find some information in but you will find much more information in the mojo implementation/specification.
But in fact no, you should not even rely on that to understand how to use a plugin.

3) Indeed a Maven plugin may support configurable properties : directly in the pom.xml and even export them for command line usage. But that is not automatic. But in both cases, that has to be foreseen by the plugin developer and it is generally documented on the plugin or the source repository homepage.

In fact in your case, if you go into the Mojo implementation : AbstractH2Mojo, you can see how the configuration is set.
All properties have default values in the mojo constructor.

 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"));
 }

The mojo empty constructor is first invoked, then all setter are invoked on the created instance.
It means that you can override any of these properties defined in that class at runtime by providing the property such as ${artifactIdPrefixWithoutMavenPlugin}.field.
Since the maven plugin is h2-maven-plugin, the prefix to refer is h2.

If you run that :

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

You could see in the output :

[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);
    }
 }

It means that you have no way to change the H2 JAR used : from the command line when you run the plugin or from the plugin declaration in the pom.xml, since no property is defined in the Mojo to achieve that.

if you will change the H2 version, you need to change the version embedded by the plugin. As a starter, you could try to fork the plugin GIT repository, change the h2 dependency used in the pom to match to your requirement and check whether in spite of the gap version, working with that plugin is a possible thing.

Note that you could add a new property of the Mojo to make it completely configurable such as :

mvn ... -Dh2Version=1.4.200 

But in that case you will need to retrieve that. For example by performing a request to download the dependency from the m2 central repo for example.
And you should also ensure that only valid ranges of the h2 version are used.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=8676&siteId=1