[JAVA] How to set different source / target versions for production code and test code

Introduction

In a Gradle Java project, the production code (Java code under the src / main / java directory) is equivalent to Java 7 equivalent compilation (when you specify the -source 1.7 -target 1.7 option with the javac command. ), But I want to compile the test code (Java code under src / test / java) equivalent to Java 8! In such a case, I didn't know how to specify sourceCompatibility or targetCompatibility in build.gradle, so I tried to verify it.

I didn't have to specify different source / target versions for production and test code, but JUnit 5 requires Java 8 at runtime It seems to be current / user-guide / # overview-java-versions), so it is necessary to compile with Java 7 on the production code side, but I want to use JUnit 5 for the test code! I think that you can refer to this setting in such cases.

How to set sourceCompatibility / targetCompatibility

As you can see in the build.gradle example below

--Production code settings --Specify normally --Test code settings --Specify in the properties of the compileTestJava task

If so, it's OK.

apply plugin: 'java'

//⭐️ source applied to production code/Target compatibility settings
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7

//⭐️ source that applies only to test code/Target compatibility settings
compileTestJava {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

//I tried using the sourceSets property as below, but this didn't work
//sourceSets {
//    main {
//        sourceCompatibility = JavaVersion.VERSION_1_7
//        targetCompatibility = JavaVersion.VERSION_1_7
//    }
//    test {
//        sourceCompatibility = JavaVersion.VERSION_1_8
//        targetCompatibility = JavaVersion.VERSION_1_8
//    }
//}

repositories {
    mavenCentral()
}

dependencies {
    // ...
}

Recommended Posts

How to set different source / target versions for production code and test code
How to set character code and line feed code in Eclipse
How to deal with different versions of rbenv and Ruby
[RSpec] How to write test code
How to use \ t Escape sequences different for mac and Windows-java
[RSpec on Rails] How to write test code for beginners by beginners
I tested how to use Ruby's test / unit and rock-paper-scissors code.
How to set up and use kapt
Needed for iOS 14? How to set NSUserTrackingUsageDescription
Articles and books useful for studying test code
[Java] How to test for null with JUnit
Thinking about how to use Ionic-Native, Hybrid and HTML5 for mobile app production
[Java] (for MacOS) How to set the classpath
How to set up and operate jEnv (Mac)
How to write test code with Basic authentication
How to set up computer vision for tracking images and videos with TrackingJs
How to set Dependency Injection (DI) for Spring Boot
How to write a unit test for Spring Boot 2
How to unit test with JVM with source using RxAndroid
How to check CircleCI code and automatically deploy to Heroku
[RSpec] How to test error messages set by Shoulda-Matchers
How to test including images when using ActiveStorage and Faker
How to specify character code and line feed code in JAXB
How to decompile apk file to java source code with MAC
How to use Font Awesome icon for ul and li
How to set and describe environment variables using Rails zsh
How to test a private method with RSpec for yourself
Procedures for passing RealmObject to Fragment and how to use Parceler
How to create and launch a Dockerfile for Payara Micro
[Integration test code] How to select an element from date_select
How to write good code
How to set Docker nginx
How to set Java constants
[Rails] How to get the URL of the transition source and redirect
How to set the IP address and host name of CentOS8
How to set and use profile in annotation-based Configuration in Spring framework