Java 1.7中是否可选? Gradle忽略源/目标兼容性?

时间:2015-08-21 15:06:00

标签: java gradle java-8 optional

我的Java 7项目在Gradle中正常运行,即使它取决于java.util.Optional

我非常非常困惑。 java.util.Optional只是Java 8中的一件事,对吧?

我有一个Gradle项目,我已将sourceCompatibilitytargetCompatibility都设置为1.7

在本单元的一个课程中,我导入java.util.Optional,直到Java 8,我认为才被包含在内。

如果我运行gradlew clean assemble,编译器甚至会被正确的选项调用。虽然有一个警告听起来有点可疑。但是,jar可以很好地构建。

15:51:12.998 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter] Executing actions for task ':core:compileJava'.
15:51:13.000 [DEBUG] [org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler] Compiler arguments: -source 1.7 -target 1.7 -d /Users/deejay/workspace/number-jumble/core/build/classes/main -g -encoding UTF-8 -classpath /Users/deejay/workspace/number-jumble/model/build/libs/model-1.0.jar:/Users/deejay/.gradle/caches/modules-2/files-2.1/com.badlogicgames.gdx/gdx/1.3.1/18995f8b7b19118975722370818023c76a86fdf4/gdx-1.3.1.jar:/Users/deejay/.gradle/caches/modules-2/files-2.1/com.badlogicgames.gdx/gdx-freetype/1.3.1/1f647429753c8c91fb8fc477ffe5a4c9cde2f429/gdx-freetype-1.3.1.jar:/Users/deejay/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/18.0/cce0823396aa693798f8882e64213b1772032b09/guava-18.0.jar /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/NumberJumbleGame.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/complexity/ChangeComplexityScreen.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/libgdx/SizeToAndLayoutAction.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/libgdx/TimeLabel.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/services/AppropriatelySizedFontService.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/services/DateService.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/session/GameSessionUi.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/session/HardcodedStreakAssessmentService.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/session/LandscapeGameSessionUi.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/session/PortraitGameSessionUi.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/session/RackScreen.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/session/RackSummaryScreen.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/session/StreakAssessmentService.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/splash/ReturnSplashScreen.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/stats/GdxLocalStatisticsRepository.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/stats/MapBackedStatisticsRepository.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/stats/StatisticsRepository.java /Users/deejay/workspace/number-jumble/core/src/main/java/com/binarytweed/numberjumble/stats/StatisticsService.java
15:51:13.001 [INFO] [org.gradle.api.internal.tasks.compile.jdk6.Jdk6JavaCompiler] Compiling with JDK Java compiler API.
15:51:13.007 [ERROR] [system.err] warning: [options] bootstrap class path not set in conjunction with -source 1.7
15:51:13.185 [ERROR] [system.err] 1 warning
我生气了吗?

1 个答案:

答案 0 :(得分:4)

你不会生气:D

在Java 8中引入了-=。这是正确的。我猜你正在编译Java 8.所以使用(和检查)的类实际上是Java 8的类。

position仅影响使用的语法样式。不是使用的类。如果设置为1.7,则不能使用lambda表达式。但是你可以使用Java 8的类。但是如果你使用新java的类,你的应用程序将在运行时失败。

java.util.Optional仅影响编译语言级别。所以java二进制类代码的版本。同样,在编译期间可以访问java类,但如果使用旧版本的java执行它,则会失败,因为引用的类不存在。