DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building AI Applications With Java and Gradle
  • How To Add Three Photo Filters to Your Applications in Java
  • How To Convert HTML to PNG in Java
  • Deploying a Kotlin App to Heroku

Trending

  • Alternative Structured Concurrency
  • RAG Is Not Enough: Advanced Retrieval Architectures Using Vertex AI Search on GCP
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • Ingesting Fixed-Width Mainframe Files Into Delta Lake: The Details Nobody Writes Down
  1. DZone
  2. Coding
  3. Java
  4. Gradle Goodness: Enabling Preview Features For Java

Gradle Goodness: Enabling Preview Features For Java

Check out how to enable and play with preview Java features in Gradle.

By 
Hubert Klein Ikkink user avatar
Hubert Klein Ikkink
·
Mar. 05, 21 · Code Snippet
Likes (2)
Comment
Save
Tweet
Share
9.7K Views

Join the DZone community and get the full member experience.

Join For Free

Java introduced preview features in the language starting in Java 12. These features can be tried out by developers, but are still subject to change and can even be removed in the next release. By default, the preview features are not enabled when we want to compile and run our Java code. We must explicitly specify that we want to use the preview feature to the Java compiler and Java runtime using the command-line argument --enable-preview. In Gradle, we can customize our build file to enable preview features. We must customize tasks of type JavaCompile and pass --enable-preview to the compiler arguments. Also tasks of type Test and JavaExec must be customized where we need to add the JVM argument --enable-preview.

In the following Gradle build script written in Kotlin, we have a Java project written with Java 15 where we reconfigure the tasks to enable preview features:

Kotlin
 




xxxxxxxxxx
1
48


 
1
plugins {
2
    java
3
    application
4
}
5
6
repositories {
7
    mavenCentral()
8
}
9
10
dependencies {
11
    testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.1")
12
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.1")
13
}
14
15
application {
16
    mainClass.set("mrhaki.Patterns")
17
}
18
19
tasks {
20
    val ENABLE_PREVIEW = "--enable-preview"
21
22
    // In our project we have the tasks compileJava and
23
    // compileTestJava that need to have the
24
    // --enable-preview compiler arguments.
25
    withType<JavaCompile>() {
26
        options.compilerArgs.add(ENABLE_PREVIEW)
27
28
        // Optionally we can show which preview feature we use.
29
        options.compilerArgs.add("-Xlint:preview")
30
31
        // Explicitly setting compiler option --release
32
        // is needed when we wouldn't set the
33
        // sourceCompatiblity and targetCompatibility
34
        // properties of the Java plugin extension.
35
        options.release.set(15)
36
    }
37
38
    // Test tasks need to have the JVM argument --enable-preview.
39
    withType<Test>() {
40
        useJUnitPlatform()
41
        jvmArgs.add(ENABLE_PREVIEW)
42
    }
43
44
    // JavaExec tasks need to have the JVM argument --enable-preview.
45
    withType<JavaExec>() {
46
        jvmArgs.add(ENABLE_PREVIEW)
47
    }
48
}



Written with Gradle 6.8.3

Java (programming language) Gradle

Published at DZone with permission of Hubert Klein Ikkink. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building AI Applications With Java and Gradle
  • How To Add Three Photo Filters to Your Applications in Java
  • How To Convert HTML to PNG in Java
  • Deploying a Kotlin App to Heroku

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook