繁体   English   中英

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

[英]How to manually specify java path in exec-maven-plugin

在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路径?

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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