简体   繁体   English

无论我的代码是java,都只为Maven设置JAVA_HOME

[英]setting JAVA_HOME for Maven ONLY regardless of my code java

I am running different java projects. 我正在运行不同的java项目。 Understanding that changing java_home system environment each time I switch between them is a bad idea, I'm starting to set JAVA_HOME specifically for each one of them independently . 了解每次在它们之间切换时改变java_home系统环境是一个坏主意,我开始专门为它们中的每一个独立设置JAVA_HOME。 When I set JAVA_HOME for maven (version 2) runtime (the java maven uses to run, being a java application)? 当我为maven(版本2)运行时设置JAVA_HOME (java maven用来运行,作为java应用程序)? To my understanding, JAVA_HOME for maven should not affect how my code is compiled and run (which are supposed to be configured using maven-compiler-plugin source and target). 据我了解,maven的JAVA_HOME不应该影响我的代码编译和运行的方式(应该使用maven-compiler-plugin源和目标来配置)。 Right? 对?

When I set JAVA_HOME in mvn.bat : 当我在mvn.bat中设置JAVA_HOME时:

@REM Maven2 Start Up Batch script
set JAVA_HOME=C:\dev\tools\jdk-1.6

And my maven-compiler-plugin is configured this way: 我的maven-compiler-plugin以这种方式配置:

<plugin>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <source>1.7</source>
    <target>1.7</target>
  </configuration>
</plugin>

I receive this error: 我收到此错误:

[ERROR] BUILD ERROR
[INFO] --------------------------------------
[INFO] Fatal error compiling
Embedded error: invalid target release: 1.7

Why does JAVA_HOME specified for maven runtime affect my code compilation? 为什么为maven运行时指定的JAVA_HOME会影响我的代码编译? How to separate between the two? 如何将两者分开?

Maven will use the JRE specified in JAVA_HOME By default it will use the JDK from the same folder for compiling .java files Maven将使用JAVA_HOME指定的JRE默认情况下,它将使用同一文件夹中的JDK来编译.java文件

However you can change it in maven-compile-plugin configuration 但是,您可以在maven-compile-plugin 配置中更改它

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <verbose>true</verbose>
          <fork>true</fork>
          <executable><!-- path-to-javac --></executable>
          <compilerVersion>1.3</compilerVersion>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

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

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