Created
February 10, 2018 14:34
-
-
Save breskeby/fd9cdf54eeb7011c5630e77d035fc6c5 to your computer and use it in GitHub Desktop.
accessing java targetCompatibility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.gradle.api.JavaVersion; | |
import org.gradle.api.Plugin; | |
import org.gradle.api.Project; | |
import org.gradle.api.plugins.JavaPlugin; | |
import org.gradle.api.plugins.JavaPluginConvention; | |
import org.gradle.api.provider.Property; | |
import java.util.concurrent.Callable; | |
public class AcmePlugin implements Plugin<Project> { | |
private Property<JavaVersion> targetCompatibility; | |
@Override | |
public void apply(Project project) { | |
// this expects the java-base plugin is already applied. alternatively you can do | |
// project.getPlugins().apply(JavaBasePlugin.class) explicitly | |
targetCompatibility = project.getObjects().property(JavaVersion.class); | |
project.getPluginManager().apply(JavaPlugin.class); | |
JavaPluginConvention convention = (JavaPluginConvention)project.getConvention().getPlugins().get("java"); | |
// you could access it directly via: | |
JavaVersion initialTargetCompatibiltiy = convention.getTargetCompatibility(); | |
// but that only provides you with the value have access to targetCompatibility at the time your plugin was applied | |
// To take user changes to that value into account made after your plugin was applied you have to use | |
// the project.provider / property api | |
this.targetCompatibility.set(project.provider(new Callable<JavaVersion>() { | |
@Override | |
public JavaVersion call() throws Exception { | |
return convention.getTargetCompatibility(); | |
} | |
})); | |
// now targetCompatibility is a handle to the value. you should pass it down to your task and | |
// access the javaversion value during execution time. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment