【问题标题】:javac invalid target 1.7 mvn install with Java 7 on Macjavac 在 Mac 上使用 Java 7 安装无效目标 1.7 mvn
【发布时间】:2014-07-26 19:47:02
【问题描述】:

我下载了一个 Github 项目,我的团队将为其进行开发工作(它是私有的,因此分享它无济于事)。 它包含一个具有以下依赖关系的 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

我的 Mac 上似乎成功安装了 Java 7,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/ 中解决了他们的问题...我这样做了 - 但没有结果。

为什么不为 Mac 安装 Java 解决这些问题,因为除了在 ~/.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

我可能做错了。

(我的)回答

我想通了——权限不允许访问该 githubproject 中的文件。我只需要更改这些文件的权限,它就成功构建了。 (将编译器插件放在我的 mavin lib 目录下会破坏 maven 一段时间,但我终于摆脱了那个放置不当的 jar)。 感谢所有给予关注的人 - 错误是一个红鲱鱼。

【问题讨论】:

  • 使用maven-compiler-plugin (3.1) 的更新版本。 (顺便说一句:你为​​什么要通过 sudo 调用 mvn?)。
  • 也有这种可能性stackoverflow.com/questions/14043042/… 尝试解决该问题,看看是否有帮助。 (PS:你们是在分享一个 maven 安装吗?!)
  • @khmarbaise 我在工作计算机上偶尔会出现奇怪的权限问题——只需要四处走走清理一下,但我怀疑通过 sudo 运行它是问题所在。将考虑使用更新的 Maven 编译器。我应该把那个plugin.jar放在哪里?
  • @ug_ 我读了它并检查了它——看起来它应该带有正确的 JAVA_HOME,特别是因为我正在定义它。如果脚本也遇到任何逻辑,它也会输出一些东西......所以它似乎不是 mvn 脚本用错误的 Java 覆盖我们都单独安装了 maven - 但都安装了相同的版本(所以,是的- 我们都使用相同的 maven 安装,但不一定是数字相同的文件......所以我们有不同的 mvn 安装将是一个边缘情况。
  • 尝试了 3.1 编译器插件(将 pom.xml 更新为 3.1 而不是 2.3.2),将使用输出更新主要描述...

标签: java macos maven javac


【解决方案1】:

不久前,我用直接从 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

什么是我的专家?使用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

【讨论】:

  • 你的 maven 编译器 3.1 jar 放在哪里了?
  • 所以你没有使用单独的编译器插件?
  • 当然不是。您在 pom 中指定要使用插件的特定版本,maven(在mvm clean install 期间)会自动为您从网络中提取它。就像任何其他依赖一样:/Users/olegs/.m2/repository/org/apache/maven/plugins/maven-compiler-plugin/3.1
  • 啊,这样就搞定了——所以我不需要安装编译器。好-所以问题不存在。
  • 结果如何?
猜你喜欢
  • 1970-01-01
  • 2015-05-28
  • 1970-01-01
  • 2017-01-09
  • 2020-07-01
  • 2012-02-18
  • 2014-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多