我下载了一个 Github 项目,我的团队将为其进行开发工作(它是私有(private)的,因此共享它没有帮助)。 它包含一个具有以下依赖项的 pom.xml 文件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
该项目依赖于 Java 7。 在其他人的案例中研究这个问题 - 看起来他们的版本在 java 和 mvn 以及 JAVA_HOME 环境变量之间不一致,所以让我向您展示一些终端输出:
Last login: Thu Jun 5 13:05:34 on console
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home
$ mvn -version
Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T12:37:52-05:00)
Maven home: /usr/local/apache-maven-3.2.1
Java version: 1.7.0_60, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.9", arch: "x86_64", family: "mac"
$ java -version
java version "1.7.0_60"
Java(TM) SE Runtime Environment (build 1.7.0_60-b19)
Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)
$ javac -version
javac 1.7.0_60
看起来 Java 7 已成功安装在我的 Mac 上,JAVA_HOME 的环境变量设置正确,并且 maven 声称拥有我需要的 Java 版本。
在我从 Github 克隆的项目的顶级目录中(我将其称为“githubproject”),我运行:
sudo mvn clean install
它运行了一段时间,输出良好......直到:
[INFO] Compiling 239 source files to /usr/local/gardens/githubproject/target/classes
[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>
use -help for a list of possible options
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 18.793 s
[INFO] Finished at: 2014-06-05T13:08:38-05:00
[INFO] Final Memory: 26M/123M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project hgtvgardens: Compilation failure
[ERROR] Failure executing javac, but could not parse the error:
[ERROR] javac: invalid target release: 1.7
[ERROR] Usage: javac <options> <source files>
[ERROR] use -help for a list of possible options
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
所以这里有一件有趣的事情是,如果我进入 pom.xml 文件 - 我可以将依赖项从 1.7 更改为 1.6,并且 mvn clean install 命令运行得很好 - 构建成功...... 然而,该项目存在各种 Java 错误,需要 Java 7 来编译某些部分(尽管该项目的其他部分仍然有效)。
我最好的猜测是这是一个环境问题 - 但为什么会出现问题,又可能出现在哪里?
在研究中,其他人通过将 tools.jar 复制到/Library/Java/Extensions/中解决了他们的问题......我也这样做了 - 但没有结果。
为什么安装 Java for Mac 不能解决这些问题,因为除了在 ~/.bash_profile 中设置 JAVA_HOME 之外,我没有做太多自定义 Java 设置
是否有我需要更改的系统链接?
链接更新
我的一名团队成员参加了比赛
sudo rm /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/ /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
它修复了他的构建错误 - 但他遇到了同样的问题,看起来项目编译错误。
其他团队成员完全没有问题 - 相同的操作系统、硬件、Github 项目和软件...有些不同 - 我不确定是什么...
Mavan 编译器插件更新
有人建议使用更新的 Maven 编译器。
从http://search.maven.org/#artifactdetails|org.apache.maven.plugins|maven-compiler-plugin|3.1|maven-plugin下载maven-compiler-plugin-3.1.jar 并将其复制到我的 apache-maven/lib 目录中(这是一个完整的猜测)。 然后我更新了 pom.xml 以使用 3.1 而不是 2.3.2
结果:
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-compiler-plugin:3.1:compile
(default-compile) on project githubproject: Execution default-compile of goal
org.apache.maven.plugins:maven-compiler-plugin:3.1:compile failed:
A required class was missing while executing
org.apache.maven.plugins:maven-compiler-plugin:3.1:compile:
org/codehaus/plexus/compiler/util/scan/mapping/SuffixMapping
我可能做错了。
(我的)答案
我发现了 - 权限不允许访问该 github 项目中的文件。我只需更改这些文件的权限即可成功构建。 (将编译器插件放在我的 mavin lib 目录下使 maven 中断了一段时间,但我终于摆脱了那个位置不当的 jar)。 感谢所有给予关注的人 - 这个错误是一个转移注意力的错误。
最佳答案
我已经用不久前直接从 Sun 下载的 1.7 完全替换了我的 Mac OS Java。它确实需要对 CurrentJDK
的链接进行一些修改,但结果如下。希望这会有所帮助,因为您的 Java 安装似乎已损坏:
哪个操作系统:
System Version: OS X 10.9.3 (13D65)
Kernel Version: Darwin 13.2.0
我有哪种 Java 以及它在哪里:
schroedinger:backend olegs$ javac -version
javac 1.7.0_40
schroedinger:backend olegs$ which javac
/usr/bin/javac
那么它实际安装在哪里以及如何安装:
schroedinger:Versions olegs$ ls -la /usr/bin/javac
lrwxr-xr-x 1 root wheel 75 31 Okt 2013 /usr/bin/javac -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javac
schroedinger:Versions olegs$ pwd
/System/Library/Frameworks/JavaVM.framework/Versions
Current
下实际存在哪个 JVM。注意有 CurrentJDK
(指向不同位置的 1.6)和 Current
(指向 1.7)。实际的 1.7 JDK 安装在目录 1.7 中:
schroedinger:Versions olegs$ ls -la
total 64
drwxr-xr-x 11 root wheel 374 31 Okt 2013 .
drwxr-xr-x 12 root wheel 408 21 Apr 19:42 ..
lrwxr-xr-x 1 root wheel 10 31 Okt 2013 1.4 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 31 Okt 2013 1.4.2 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 31 Okt 2013 1.5 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 31 Okt 2013 1.5.0 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 31 Okt 2013 1.6 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 31 Okt 2013 1.6.0 -> CurrentJDK
drwxr-xr-x 8 root wheel 272 31 Okt 2013 1.7
lrwxr-xr-x 1 root wheel 1 31 Okt 2013 Current -> 1.7
lrwxr-xr-x 1 root wheel 59 31 Okt 2013 CurrentJDK -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents
我的 pom 说什么:
schroedinger:backend olegs$ grep jdk.version pom.xml
<jdk.version>1.7</jdk.version>
...
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
我需要JAVA_HOME
吗?否:
schroedinger:backend olegs$ echo $JAVA_HOME
我的 maven 是什么?它与 homebrew
一起安装:
schroedinger:backend olegs$ which mvn
/usr/local/bin/mvn
schroedinger:backend olegs$ ls -la /usr/local/bin/mvn
lrwxr-xr-x 1 dev staff 29 31 Okt 2013 /usr/local/bin/mvn -> ../Cellar/maven/3.0.5/bin/mvn
编译通过吗?
schroedinger:backend olegs$ mvn clean install
[INFO] Scanning for projects...
[INFO]
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.407s
[INFO] Finished at: Thu Jun 05 20:35:48 CEST 2014
关于在 Mac 上使用 Java 7 安装 javac 无效目标 1.7 mvn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24066591/