How to check java version when running gradle?

One of my projects requires Java 1.8, but sometimes we have not noticed that we are using older Java to get some strange errors.

I want to add verification to build.gradle , so when we run a task, it first checks the version and prints an error and exits immediately.

I tried adding verification directly to build.gradle in the first line, but still performs some other tasks, for example. ( clean , compileJava ) before checking when I run:

 $ ./gradlew 

How to do it right?

+5
source share
1 answer

If you set the check very early in the build life cycle (the usual check at the beginning of your build.gradle file or in the plugin application method), you should not see any completed tasks.

you can use the JavaVersion enumeration for what is part of the gradle api:

 if(JavaVersion.current() != JavaVersion.VERSION_1_8){ throw new GradleException("This build must be run with java 8") } 
+15
source

Source: https://habr.com/ru/post/1211583/


All Articles