2016-02-12 98 views
2

在maven项目中,我试图在generate-test-resources阶段之前执行特定的类文件。如何在exec-maven-plugin中手动指定java路径

我不想用我的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> 
    <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) 

有什么办法来手动指定Java版本或Java路径的EXEC-Maven的插件用途?

回答

2

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

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

因此,它会使用相同的JAVA_HOME作为Maven的使用。

如果要明确指定可执行文件,则需要使用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>