2222import org .gradle .api .tasks .SourceSetContainer ;
2323import org .gradle .api .tasks .compile .CompileOptions ;
2424import org .gradle .api .tasks .compile .JavaCompile ;
25+ import org .gradle .api .tasks .javadoc .Javadoc ;
2526import org .gradle .api .tasks .testing .Test ;
27+ import org .gradle .external .javadoc .CoreJavadocOptions ;
2628import org .gradle .jvm .tasks .Jar ;
2729import org .gradle .jvm .toolchain .JavaLanguageVersion ;
2830import org .gradle .jvm .toolchain .JavaToolchainService ;
@@ -73,8 +75,10 @@ public void apply(Project project) {
7375 List <Integer > mainVersions = findSourceVersions (project );
7476 List <String > mainSourceSets = new ArrayList <>();
7577 mainSourceSets .add (SourceSet .MAIN_SOURCE_SET_NAME );
78+ configurePreviewFeatures (project , javaExtension .getSourceSets ().getByName (SourceSet .MAIN_SOURCE_SET_NAME ), 21 );
7679 List <String > testSourceSets = new ArrayList <>(mainSourceSets );
7780 testSourceSets .add (SourceSet .TEST_SOURCE_SET_NAME );
81+ configurePreviewFeatures (project , javaExtension .getSourceSets ().getByName (SourceSet .TEST_SOURCE_SET_NAME ), 21 );
7882 for (int javaVersion : mainVersions ) {
7983 String mainSourceSetName = SourceSet .MAIN_SOURCE_SET_NAME + javaVersion ;
8084 SourceSet mainSourceSet = addSourceSet (project , javaExtension , mainSourceSetName , mainSourceSets , javaVersion );
@@ -124,11 +128,8 @@ private SourceSet addSourceSet(
124128 compileTask .setSourceCompatibility (Integer .toString (javaVersion ));
125129 CompileOptions compileOptions = compileTask .getOptions ();
126130 compileOptions .getRelease ().set (javaVersion );
127- compileOptions .getCompilerArgs ().add ("--enable-preview" );
128- compileOptions .getCompilerArgs ().add ("-Xlint:-preview" );
129-
130- compileTask .doLast (t -> { stripPreviewFromFiles (compileTask .getDestinationDirectory ().getAsFile ().get ().toPath ()); });
131131 });
132+ configurePreviewFeatures (project , sourceSet , javaVersion );
132133
133134 // Since we configure MRJAR sourcesets to allow preview apis, class signatures for those
134135 // apis are not known by forbidden apis, so we must ignore all missing classes. We could, in theory,
@@ -142,6 +143,21 @@ private SourceSet addSourceSet(
142143 return sourceSet ;
143144 }
144145
146+ private void configurePreviewFeatures (Project project , SourceSet sourceSet , int javaVersion ) {
147+ project .getTasks ().withType (JavaCompile .class ).named (sourceSet .getCompileJavaTaskName ()).configure (compileTask -> {
148+ CompileOptions compileOptions = compileTask .getOptions ();
149+ compileOptions .getCompilerArgs ().add ("--enable-preview" );
150+ compileOptions .getCompilerArgs ().add ("-Xlint:-preview" );
151+
152+ compileTask .doLast (t -> { stripPreviewFromFiles (compileTask .getDestinationDirectory ().getAsFile ().get ().toPath ()); });
153+ });
154+ project .getTasks ().withType (Javadoc .class ).named (name -> name .equals (sourceSet .getJavadocTaskName ())).configureEach (javadocTask -> {
155+ CoreJavadocOptions options = (CoreJavadocOptions ) javadocTask .getOptions ();
156+ options .addBooleanOption ("-enable-preview" , true );
157+ options .addStringOption ("-release" , String .valueOf (javaVersion ));
158+ });
159+ }
160+
145161 private void configureSourceSetInJar (Project project , SourceSet sourceSet , int javaVersion ) {
146162 var jarTask = project .getTasks ().withType (Jar .class ).named (JavaPlugin .JAR_TASK_NAME );
147163 jarTask .configure (task -> task .into ("META-INF/versions/" + javaVersion , copySpec -> copySpec .from (sourceSet .getOutput ())));
0 commit comments