@@ -72,18 +72,19 @@ public void apply(Project project) {
7272 var javaExtension = project .getExtensions ().getByType (JavaPluginExtension .class );
7373 var isIdeaSync = System .getProperty ("idea.sync.active" , "false" ).equals ("true" );
7474 var ideaSourceSetsEnabled = project .hasProperty (MRJAR_IDEA_ENABLED ) && project .property (MRJAR_IDEA_ENABLED ).equals ("true" );
75+ int minJavaVersion = Integer .parseInt (buildParams .getMinimumCompilerVersion ().getMajorVersion ());
7576
7677 // Ignore version-specific source sets if we are importing into IntelliJ and have not explicitly enabled this.
7778 // Avoids an IntelliJ bug:
7879 // https://youtrack.jetbrains.com/issue/IDEA-285640/Compiler-Options-Settings-language-level-is-set-incorrectly-with-JDK-19ea
7980 if (isIdeaSync == false || ideaSourceSetsEnabled ) {
80- List <Integer > mainVersions = findSourceVersions (project );
81+ List <Integer > mainVersions = findSourceVersions (project , minJavaVersion );
8182 List <String > mainSourceSets = new ArrayList <>();
8283 mainSourceSets .add (SourceSet .MAIN_SOURCE_SET_NAME );
83- configurePreviewFeatures (project , javaExtension .getSourceSets ().getByName (SourceSet .MAIN_SOURCE_SET_NAME ), 21 );
84+ configurePreviewFeatures (project , javaExtension .getSourceSets ().getByName (SourceSet .MAIN_SOURCE_SET_NAME ), minJavaVersion );
8485 List <String > testSourceSets = new ArrayList <>(mainSourceSets );
8586 testSourceSets .add (SourceSet .TEST_SOURCE_SET_NAME );
86- configurePreviewFeatures (project , javaExtension .getSourceSets ().getByName (SourceSet .TEST_SOURCE_SET_NAME ), 21 );
87+ configurePreviewFeatures (project , javaExtension .getSourceSets ().getByName (SourceSet .TEST_SOURCE_SET_NAME ), minJavaVersion );
8788 for (int javaVersion : mainVersions ) {
8889 String mainSourceSetName = SourceSet .MAIN_SOURCE_SET_NAME + javaVersion ;
8990 SourceSet mainSourceSet = addSourceSet (project , javaExtension , mainSourceSetName , mainSourceSets , javaVersion , true );
@@ -103,6 +104,7 @@ public void apply(Project project) {
103104 }
104105
105106 private void configureMrjar (Project project ) {
107+
106108 var jarTask = project .getTasks ().withType (Jar .class ).named (JavaPlugin .JAR_TASK_NAME );
107109 jarTask .configure (task -> { task .manifest (manifest -> { manifest .attributes (Map .of ("Multi-Release" , "true" )); }); });
108110
@@ -222,7 +224,7 @@ private void createTestTask(
222224 project .getTasks ().named ("check" ).configure (checkTask -> checkTask .dependsOn (testTaskProvider ));
223225 }
224226
225- private static List <Integer > findSourceVersions (Project project ) {
227+ private static List <Integer > findSourceVersions (Project project , int minJavaVersion ) {
226228 var srcDir = project .getProjectDir ().toPath ().resolve ("src" );
227229 List <Integer > versions = new ArrayList <>();
228230 try (var subdirStream = Files .list (srcDir )) {
@@ -231,7 +233,23 @@ private static List<Integer> findSourceVersions(Project project) {
231233 String sourcesetName = sourceSetPath .getFileName ().toString ();
232234 Matcher sourcesetMatcher = MRJAR_SOURCESET_PATTERN .matcher (sourcesetName );
233235 if (sourcesetMatcher .matches ()) {
234- versions .add (Integer .parseInt (sourcesetMatcher .group (1 )));
236+ int version = Integer .parseInt (sourcesetMatcher .group (1 ));
237+ if (version < minJavaVersion ) {
238+ // NOTE: We allow mainNN for the min java version so that incubating modules can be used without warnings.
239+ // It is a workaround for https://bugs.openjdk.org/browse/JDK-8187591. Once min java is 22, we
240+ // can use the SuppressWarnings("preview") in the code using incubating modules and this check
241+ // can change to <=
242+ throw new IllegalArgumentException (
243+ "Found src dir '"
244+ + sourcesetName
245+ + "' for Java "
246+ + version
247+ + " but multi-release jar sourceset should have version "
248+ + minJavaVersion
249+ + " or greater"
250+ );
251+ }
252+ versions .add (version );
235253 }
236254 }
237255 } catch (IOException e ) {
0 commit comments