简体   繁体   中英

How to manually specify java path in exec-maven-plugin

In a maven project I'm trying to execute a specific class file before generate-test-resources phase.

I don't want to use my JAVA_HOME variable thus I used the following plugin for compilation :

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
            <verbose>true</verbose>
            <fork>true</fork>
            <executable>${JAVA_1_8_HOME}/bin/javac</executable>
            <compilerVersion>1.3</compilerVersion>
        </configuration>
</plugin>  

and the following plugin for running tests :

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.13</version>
    <configuration>
        <jvm>${JAVA_1_8_HOME}/bin/java</jvm>
    </configuration>
</plugin>

All tests run correctly in Java 8. Then I'm trying to specify a class file to run before tests with exec-maven-plugin :

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3</version>
    <executions>
        <execution>
            <id>build-test-environment</id>
            <phase>generate-test-resources</phase>
            <goals>
                <goal>java</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <executable>maven</executable>
        <mainClass>test.server.Server</mainClass>
    </configuration>
</plugin>

The problem is that I'm getting the following error:

java.lang.UnsupportedClassVersionError: test/server/Server : Unsupported major.minor version 52.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:281)
        at java.lang.Thread.run(Thread.java:722) 

Is there any way to manually specify the java version or the java path that the exec-maven-plugin uses?

The exec-maven-plugin:java goal runs in the same VM:

Executes the supplied java class in the current VM with the enclosing project's dependencies as classpath.

As such, it will use the same JAVA_HOME as Maven is using.

If you want to explicitly specify an executable, you need to use the exec-maven-plugin:exec goal, which runs in a separate process. Then you can configure the executable parameter to the executable you want:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3</version>
    <executions>
        <execution>
            <id>build-test-environment</id>
            <phase>generate-test-resources</phase>
            <goals>
                <goal>exec</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <executable>${JAVA_1_8_HOME}/bin/java</executable>
        <arguments>
            <argument>-classpath</argument>
            <classpath/>
            <argument>test.server.Server</argument>
        </arguments>
    </configuration>
</plugin>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM