@@ -480,24 +480,52 @@ void testModularSourcesWithExplicitResourcesIssuesWarning() throws Exception {
480480 }
481481
482482 /**
483- * Tests mixed source configuration where:
484- * - Modular sources are defined for main Java (should override sourceDirectory)
485- * - Classic testSourceDirectory is used (should be preserved since no modular test sources)
483+ * Tests that legacy sourceDirectory and testSourceDirectory are ignored in modular projects.
484+ * <p>
485+ * In modular projects, legacy directories are unconditionally ignored because it is not clear
486+ * how to dispatch their content between different modules. A warning is emitted if these
487+ * properties are explicitly set (differ from Super POM defaults).
486488 * <p>
487489 * This verifies:
488- * - sourceDirectory is ignored when modular main sources exist
489- * - testSourceDirectory is used when no modular test sources are defined
490+ * - WARNINGs are emitted for explicitly set legacy directories in modular projects
491+ * - sourceDirectory and testSourceDirectory are both ignored
492+ * - Only modular sources from {@code <sources>} are used
490493 * <p>
491- * Acceptance Criterion: AC1 (boolean flags eliminated - uses hasSources() for main/test detection)
494+ * Acceptance Criteria:
495+ * - AC1 (boolean flags eliminated - uses hasSources() for main/test detection)
496+ * - AC7 (legacy directories warning - {@code <sourceDirectory>} and {@code <testSourceDirectory>}
497+ * are unconditionally ignored with a WARNING in modular projects)
492498 *
493499 * @see <a href="https://github.com/apache/maven/issues/11612">Issue #11612</a>
494500 */
495501 @ Test
496502 void testMixedSourcesModularMainClassicTest () throws Exception {
497503 File pom = getProject ("mixed-sources" );
498504
499- MavenSession session = createMavenSession (pom );
500- MavenProject project = session .getCurrentProject ();
505+ MavenSession mavenSession = createMavenSession (null );
506+ ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest ();
507+ configuration .setRepositorySession (mavenSession .getRepositorySession ());
508+
509+ ProjectBuildingResult result = getContainer ()
510+ .lookup (org .apache .maven .project .ProjectBuilder .class )
511+ .build (pom , configuration );
512+
513+ MavenProject project = result .getProject ();
514+
515+ // Verify WARNINGs are emitted for explicitly set legacy directories
516+ List <ModelProblem > warnings = result .getProblems ().stream ()
517+ .filter (p -> p .getSeverity () == ModelProblem .Severity .WARNING )
518+ .filter (p -> p .getMessage ().contains ("Legacy" ) && p .getMessage ().contains ("ignored in modular project" ))
519+ .toList ();
520+
521+ // Should have 2 warnings: one for sourceDirectory, one for testSourceDirectory
522+ assertEquals (2 , warnings .size (), "Should have 2 warnings for ignored legacy directories" );
523+ assertTrue (
524+ warnings .stream ().anyMatch (w -> w .getMessage ().contains ("<sourceDirectory>" )),
525+ "Should warn about ignored <sourceDirectory>" );
526+ assertTrue (
527+ warnings .stream ().anyMatch (w -> w .getMessage ().contains ("<testSourceDirectory>" )),
528+ "Should warn about ignored <testSourceDirectory>" );
501529
502530 // Get main Java source roots - should have modular sources, not classic sourceDirectory
503531 List <SourceRoot > mainJavaRoots = project .getEnabledSourceRoots (ProjectScope .MAIN , Language .JAVA_FAMILY )
@@ -521,77 +549,51 @@ void testMixedSourcesModularMainClassicTest() throws Exception {
521549 .anyMatch (sr -> sr .directory ().toString ().replace ('\\' , '/' ).contains ("src/classic/main/java" ));
522550 assertTrue (!hasClassicMainSource , "Classic sourceDirectory should be ignored" );
523551
524- // Get test Java source roots - should use classic testSourceDirectory since no modular test sources
552+ // Test sources should NOT be added (legacy testSourceDirectory is ignored in modular projects)
525553 List <SourceRoot > testJavaRoots = project .getEnabledSourceRoots (ProjectScope .TEST , Language .JAVA_FAMILY )
526554 .toList ();
527-
528- // Should have 1 test source (from classic testSourceDirectory)
529- assertEquals (1 , testJavaRoots .size (), "Should have 1 test Java source root (classic)" );
530-
531- // The test source should be the classic one (no module)
532- SourceRoot testRoot = testJavaRoots .get (0 );
533- assertTrue (testRoot .module ().isEmpty (), "Classic test source should not have a module" );
534- assertTrue (
535- testRoot .directory ().toString ().replace ('\\' , '/' ).contains ("src/classic/test/java" ),
536- "Should use classic testSourceDirectory" );
555+ assertEquals (0 , testJavaRoots .size (), "Should have no test Java sources (legacy is ignored)" );
537556 }
538557
539558 /**
540- * Tests mixed modular/non-modular sources within the same {@code <sources>} element.
559+ * Tests that mixing modular and non-modular sources within {@code <sources>} is not allowed.
560+ * <p>
561+ * A project must be either fully modular (all sources have a module) or fully classic
562+ * (no sources have a module). Mixing them within the same project is not supported
563+ * because the compiler plugin cannot handle such configurations.
541564 * <p>
542565 * This verifies:
543- * - Both modular sources (with module attribute) are added
544- * - Non-modular sources (without module attribute) are added
566+ * - An ERROR is reported when both modular and non-modular sources exist in {@code <sources>}
545567 * - sourceDirectory is ignored because {@code <source scope="main" lang="java">} exists
546568 * <p>
547- * Acceptance Criterion: AC1 (boolean flags eliminated - uses hasSources() for source detection)
569+ * Acceptance Criteria:
570+ * - AC1 (boolean flags eliminated - uses hasSources() for source detection)
571+ * - AC6 (mixed sources error - mixing modular and classic sources within {@code <sources>}
572+ * triggers an ERROR)
548573 *
549574 * @see <a href="https://github.com/apache/maven/issues/11612">Issue #11612</a>
550575 */
551576 @ Test
552577 void testSourcesMixedModulesWithinSources () throws Exception {
553578 File pom = getProject ("sources-mixed-modules" );
554579
555- MavenSession session = createMavenSession (pom );
556- MavenProject project = session .getCurrentProject ();
557-
558- // Get main Java source roots
559- List <SourceRoot > mainJavaRoots = project .getEnabledSourceRoots (ProjectScope .MAIN , Language .JAVA_FAMILY )
560- .toList ();
561-
562- // Should have 2 main sources: 1 modular (moduleA) + 1 non-modular
563- assertEquals (2 , mainJavaRoots .size (), "Should have 2 main Java source roots (1 modular + 1 non-modular)" );
564-
565- // Count modular vs non-modular sources
566- long modularCount =
567- mainJavaRoots .stream ().filter (sr -> sr .module ().isPresent ()).count ();
568- long nonModularCount =
569- mainJavaRoots .stream ().filter (sr -> sr .module ().isEmpty ()).count ();
570-
571- assertEquals (1 , modularCount , "Should have 1 modular main source" );
572- assertEquals (1 , nonModularCount , "Should have 1 non-modular main source" );
573-
574- // Verify the modular source is moduleA
575- Set <String > mainModules = mainJavaRoots .stream ()
576- .map (SourceRoot ::module )
577- .filter (opt -> opt .isPresent ())
578- .map (opt -> opt .get ())
579- .collect (Collectors .toSet ());
580-
581- assertTrue (mainModules .contains ("org.foo.moduleA" ), "Should have modular source for moduleA" );
580+ MavenSession mavenSession = createMavenSession (null );
581+ ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest ();
582+ configuration .setRepositorySession (mavenSession .getRepositorySession ());
582583
583- // Verify sourceDirectory is NOT used (should be ignored because <sources> has main java)
584- boolean hasIgnoredSource =
585- mainJavaRoots .stream ().anyMatch (sr -> sr .directory ().toString ().contains ("src/should-be-ignored" ));
586- assertTrue (!hasIgnoredSource , "sourceDirectory should be ignored when <sources> has main java" );
584+ ProjectBuildingResult result = getContainer ()
585+ .lookup (org .apache .maven .project .ProjectBuilder .class )
586+ .build (pom , configuration );
587587
588- // Get test Java source roots - should have modular test source for moduleA
589- List <SourceRoot > testJavaRoots = project .getEnabledSourceRoots (ProjectScope .TEST , Language .JAVA_FAMILY )
588+ // Verify an ERROR is reported for mixing modular and non-modular sources
589+ List <ModelProblem > errors = result .getProblems ().stream ()
590+ .filter (p -> p .getSeverity () == ModelProblem .Severity .ERROR )
591+ .filter (p -> p .getMessage ().contains ("Mixed modular and classic sources" ))
590592 .toList ();
591593
592- assertEquals (1 , testJavaRoots .size (), "Should have 1 test Java source root " );
593- assertTrue (testJavaRoots .get (0 ).module ().isPresent ( ), "Test source should be modular " );
594- assertEquals ( "org.foo.moduleA" , testJavaRoots . get (0 ).module ().get ( ), "Test source should be for moduleA " );
594+ assertEquals (1 , errors .size (), "Should have 1 error for mixed modular/classic configuration " );
595+ assertTrue (errors .get (0 ).getMessage ().contains ( "lang=java" ), "Error should mention java language " );
596+ assertTrue ( errors . get (0 ).getMessage ().contains ( "scope=main" ), "Error should mention main scope " );
595597 }
596598
597599 /**
0 commit comments