通过 Maven 编译 Java 7 代码

2022-08-31 07:26:06

我的 pom 文件列表

<project>
  <build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
  ...

但是,在,我得到mvn clean install

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] Failure executing javac, but could not parse the error:
javac: invalid target release: 1.7
Usage: javac <options> <source files>

/usr/bin/java -version是 ( 点在这里)which java

java version "1.7.0_10"
Java(TM) SE Runtime Environment (build 1.7.0_10-b18)
Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)

javac还指向正确的 Java 版本

/usr/bin/javac -> /Library/Java/JavaVirtualMachines/jdk1.7.0_10.jdk/Contents/Home/bin/javac

在这台机器上,我正在使用( 返回zshecho $0-zsh)

在我的 中,我定义了:.zshrc

 33 # HOME
 34 JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_10.jdk/Contents/Home
 35 SCALA_HOME=/Library/Scala/current
 36 FORGE_HOME=~/tools/forge/
 37 
 38 # PATH
 39 PATH="/Library/Frameworks/Python.framework/Versions/3.2/bin:${PATH}"
 40 PATH=${PATH}:${JAVA_HOME}/bin
 41 PATH=${PATH}:/bin/
 42 PATH=${PATH}:/sbin/
 43 PATH=${PATH}:/usr/bin/
 44 PATH=${PATH}:/usr/sbin/
 45 PATH=${PATH}:/opt/local/bin/
 46 PATH=${PATH}:/opt/local/sbin/
 47 PATH=${PATH}:/usr/local/git/bin
 48 PATH=${PATH}:/usr/local/git/sbin
 49 PATH=${PATH}:/Applications/Xcode.app/Contents/Developer/usr/bin
 50 PATH=${PATH}:${SCALA_HOME}/bin
 51 PATH=${PATH}:${FORGE_HOME}/bin
 52 
 53 export PATH

当我运行时,我看到实际上我使用Java 6mvn clean install --debug

  1 Apache Maven 3.0.3 (r1075438; 2011-02-28 11:31:09-0600)
  2 Maven home: /usr/share/maven
  3 Java version: 1.6.0_35, vendor: Apple Inc.
  4 Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

在哪里可以定义它?我有源(d)我的.zshrc多次。


答案 1

检查 maven 安装中的脚本,了解它是如何构建命令的。也许你或其他人已经在那里硬编码了一个,并忘记了它。mvnJAVA_HOME


答案 2

尝试使用较新版本的 maven 编译器插件:

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

此外,在 maven 中指定源文件编码最好全局完成:

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

编辑:由于这个答案仍然受到关注,我只想指出,maven编译器插件的最新值(截至最新编辑)为3.2,java为1.8,因为有关通过maven编译java 8代码的问题肯定会很快出现:-)


推荐