如何在exec-maven-plugin中手动指定java路径

时间:2016-02-12 15:13:16

标签: java maven

在maven项目中,我尝试在 generate-test-resources 阶段之前执行特定的类文件。

我不想使用我的JAVA_HOME变量,因此我使用以下插件进行编译:

<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>  

以及用于运行测试的以下插件:

<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>

所有测试都在Java 8中正确运行。 然后我尝试在使用 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>

问题是我收到以下错误:

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) 

有没有办法手动指定 exec-maven-plugin 使用的java版本或java路径?

1 个答案:

答案 0 :(得分:3)

exec-maven-plugin:java目标在同一个VM中运行:

  

在当前VM中执行提供的java类,并将封闭项目的依赖项作为类路径。

因此,它将使用与Maven使用的相同的JAVA_HOME

如果要显式指定可执行文件,则需要使用exec-maven-plugin:exec目标,该目标在单独的进程中运行。然后,您可以将executable参数配置为所需的可执行文件:

<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>