Skip to content

Fix: Add test-classes folder to classpath for test-jar workspace dependencies#2175

Open
mnpoonia wants to merge 2 commits into
eclipse-m2e:mainfrom
mnpoonia:fix/test-jar-workspace-resolution
Open

Fix: Add test-classes folder to classpath for test-jar workspace dependencies#2175
mnpoonia wants to merge 2 commits into
eclipse-m2e:mainfrom
mnpoonia:fix/test-jar-workspace-resolution

Conversation

@mnpoonia
Copy link
Copy Markdown

Fixes #2174

Summary

Adds target/test-classes folder to classpath for workspace test-jar dependencies, fixing compilation errors like "TestUtil cannot be resolved".

Problem

When a Maven dependency has <type>test-jar</type> and the dependency project is in the workspace, M2E only added a project reference with WITHOUT_TEST_CODE attribute. Eclipse JDT doesn't automatically include the target/test-classes folder based on this attribute, causing compilation failures.

Solution

After detecting a test-jar workspace dependency, explicitly add the dependency's test-classes output folder as a library entry with source attachment.

Changed file:

  • org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/DefaultClasspathManagerDelegate.java

Implementation Details

  1. Uses existing isTestArtifact() check (already detects "test-jar".equals(type))
  2. Retrieves test output directory from Maven project: mavenProject.getBuild().getTestOutputDirectory()
  3. Adds library entry pointing to target/test-classes folder
  4. Sets source attachment to src/test/java for F3 (go to definition) support
  5. Respects TEST_ATTRIBUTE for proper visibility control
  6. Graceful fallback on error (preserves existing behavior)

Testing

Manual Testing

Minimal reproduction (2 modules):

  • Before: ❌ "TestUtil cannot be resolved"
  • After: ✅ 0 errors

Apache HBase (50+ modules, 191 test-jar dependencies):

  • Before: ❌ 37 compilation errors
  • After: ✅ 0 errors

Maven CLI Compatibility

✅ Matches Maven CLI behavior (uses test-jar from workspace)

Benefits

  • ✅ No hardcoded paths - uses Maven project model
  • ✅ Minimal change - 21 lines added
  • ✅ Backward compatible - only affects test-jar dependencies
  • ✅ Follows existing M2E patterns
  • ✅ Includes source attachment for debugging

Reproduction Project

Created minimal 2-module test case demonstrating the issue and fix:

  • Structure: module-a (produces test-jar) → module-b (depends on test-jar)
  • Before fix: Compilation error in Eclipse
  • After fix: Clean import
  • Maven CLI: Always works (uses repository test-jar)

Available at: /Users/apoonia/Code/github/m2e-testjar-repro

…ndencies

When a Maven dependency has type="test-jar" and the dependency project
is in the workspace, M2E now correctly adds the dependency's
target/test-classes folder to the classpath as a library entry.

Previously, M2E only added a project reference with WITHOUT_TEST_CODE
attribute, but Eclipse JDT doesn't automatically include the test-classes
output folder based on this attribute alone, causing compilation errors
like "TestUtil cannot be resolved".

This fix:
- Detects test-jar dependencies using existing isTestArtifact() check
- Retrieves test output directory from Maven project model
- Adds test-classes folder as a library entry with source attachment
- Gracefully falls back to existing behavior if test output unavailable

Fixes workspace resolution for Maven test-jar artifacts.
@mnpoonia
Copy link
Copy Markdown
Author

@laeubi Please guide me here. I am trying to contribute for the first time.

…ndencies

Adds workspace resolution support for Maven test-jar dependencies, allowing
projects that depend on <type>test-jar</type> artifacts to reference the
workspace project's target/test-classes folder directly.

Changes:
- DefaultClasspathManagerDelegate: Add test-classes library entry for test-jar deps
- TestJarWorkspaceResolutionTest: Comprehensive test coverage (4 test methods)
  - Test type=test-jar in compile scope
  - Test classifier=tests variant
  - Test regular deps don't get test-classes (negative case)
  - Test test-jar in test scope (edge case)

Fixes eclipse-m2e#2174
@mnpoonia mnpoonia force-pushed the fix/test-jar-workspace-resolution branch from 31cb849 to 81d24a7 Compare May 20, 2026 06:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test-jar workspace dependencies not resolved - compilation errors

1 participant