Johnny.J
Johnny.J

Reputation: 31

How to specify JDK in maven jetty plugin

How to specify JDK in maven jetty plugin? Like this:

<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>${jetty-maven-plugin.version}</version>
    <configuration>
        <scanIntervalSeconds>2</scanIntervalSeconds>
        <webApp>
            <contextPath>/${project.build.finalName}</contextPath>
            <jettyEnvXml>${project.basedir}/src/over/here/jetty-env.xml</jettyEnvXml>
        </webApp>
        <httpConnector>
            <port>8080</port>
        </httpConnector>
        <!--<executable>${env.JAVA_IBM_HOME}/bin/javac</executable>-->
    </configuration>
</plugin>

Please note: the excutable tag is incorrect, I just use it as an example.

Upvotes: 2

Views: 3398

Answers (2)

Johnny.J
Johnny.J

Reputation: 31

I think I got the answer by myself, but I still don't know how to do it in maven xml file, if you know it, please tell me.

enter image description here

Upvotes: 0

A_Di-Matteo
A_Di-Matteo

Reputation: 27812

To clarify: you actually want to specify an executable, so probably an JRE (Java Runtime Environment) and not a JDK (Java Development Kit). While it is true that JDK serve as JRE as well, this is a required clarification in my opinion.

Coming to your question, there is no such a think to specify a different JDK/JRE used to execute a certain Maven plugin: plugins are executed as part of the mvn command, which in turn is executed using the system JDK/JRE by default (set in the path) and it is actually a script which invokes a plain Java main, yes, Maven it's written in Java and its execution starts from a Java main, the following one to be clear:

org.codehaus.plexus.classworlds.launcher.Launcher#main

If you want to change the JRE used to launch this main, you need to change the JAVA_HOME variable upfront, as also explained by this old SO post.

So you would need the following:

set JAVA_HOME=path_to_your_different_jdk
mvn jetty:run

In your IDE and according to the screenshot you posted in your answer, you are actually doing the same: setting which JVM must be used.

This is true for most of the plugins, unless a specific forking mechanism is foreseen. For instance:

  • The Maven Surefire Plugin provides a fork mechanism via the reuseForks option and the jvm option, which also explains that:

    Option to specify the jvm (or path to the java executable) to use with the forking options. For the default, the jvm will be a new instance of the same VM as the one used to run Maven.

    The specified JVM (JRE or JDK used as JRE) will then be used to executed the tests (basically the same scenario you were looking for concerning the Jetty Maven Plugin.

  • The Maven Compiler Plugin also provides a fork option which can then use an executable option where you can effectively point at a different JDK (and not JRE in this case) to compile your code.

    Sets the executable of the compiler to use when fork is true.

The Jetty Maven Plugin is executed as part of the Maven command (again, a Java main using the JRE specified by the JAVA_HOME variable or the default of your system), as also specified in its official documentation:

The classpath of the running Jetty and its deployed webapp are managed by Maven
...
For example, you might need to run your webapp in a forked instance of Jetty, rather than within the process running Maven;

And indeed a run-forked goal is provided

This goal allows you to start the webapp in a new JVM, optionally passing arguments to that new JVM. This goal supports the same configuration parameters as the jetty:run goal with a couple of extras to help configure the forked process.

This option however is mostly used to pass different arguments to Jetty rather than a different executable (for example, in case you already set the same argument for Maven and you want it with a different value for the Jetty run). So, again, the option is not there (to specify which executable/jvm to use for this forked execution). That's a pity, because they got almost there with this goal.

Upvotes: 2

Related Questions