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