致命错误编译:目标版本无效:1.7

时间:2015-03-28 00:54:45

标签: java maven intellij-idea

我在Stackoverflow上搜索了其他解决方案,但我仍然遇到问题。我试图用1.7运行,但Maven正在使用1.6。我现在有点想法了。有没有人有任何其他建议?

的pom.xml:

    

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
      <source>1.7</source>
      <target>1.7</target>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.2</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <createDependencyReducedPom>false</createDependencyReducedPom>
          <artifactSet>
            <excludes>
              <exclude>org.hamcrest:*</exclude>
              <exclude>org.mockito:*</exclude>
              <exclude>org.objenesis:*</exclude>
            </excludes>
          </artifactSet>
          <filters>
            <filter>
              <artifact>*:*</artifact>
              <excludes>
                <exclude>META-INF/LICENSE</exclude>
                <exclude>META-INF/license</exclude>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
              </excludes>
            </filter>
          </filters>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <manifestEntries>
                <Main-Class>com.mycompany.app.App</Main-Class>
                <Build-Number>1</Build-Number>
              </manifestEntries>
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>

当我尝试mvn clean packagemvn clean install时,我得到了:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project my-appy: Fatal error compiling: invalid target release: 1.7 -> [Help 1]

使用mvn -version输出为:

mvn -version

Apache Maven 3.2.3 (33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-11T13:58:10-07:00)
Maven home: /usr/local/Cellar/maven/3.2.3/libexec
Java version: 1.6.0_65, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x", version: "10.10.2", arch: "x86_64", family: "mac"

这是其他人的建议

echo $JAVA_HOME

/Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home

Intellij - &gt;偏好 - &gt;编译器 - &gt; Java编译器

enter image description here

$java -version
java version "1.7.0_55"
Java(TM) SE Runtime Environment (build 1.7.0_55-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.55-b03, mixed mode)

1 个答案:

答案 0 :(得分:2)

显然,您的Maven使用的Java是Java 6,因此它不会使用目标1.7进行编译。

你显然已经设置了JAVA_HOME,但是你需要确保使用正确的java版本,试试java -version并确保它是1.7 +

我通过使用jEnv工具管理多个java环境,解决了OS X上的多个java版本问题。你应该尝试一下,它可以通过Homebrew安装。