Skip to content

Improve dependency scope validation error messages for import scope - #10991

Merged
gnodet merged 1 commit into
apache:masterfrom
gnodet:improve-dependency-scope-validation-messages
Sep 3, 2025
Merged

gnodet merged 1 commit into
apache:masterfrom
gnodet:improve-dependency-scope-validation-messages

Conversation

@gnodet

@gnodet gnodet commented Jul 23, 2025 •

Copy link
Copy Markdown
Contributor

Summary

This PR improves Maven's dependency scope validation error messages to provide clearer guidance when the import scope is used incorrectly.

Problem

Currently, when a user incorrectly uses import scope in a regular <dependencies> section, Maven shows a confusing error message:

'dependencies.dependency.scope' must be one of [provided, compile, runtime, test, system] but is 'import'.

This message is misleading because it suggests that import scope is never valid, when in fact it is valid in <dependencyManagement> sections with <type>pom</type>.

Solution

The PR enhances the error message to provide context-aware guidance:

'dependencies.dependency.scope' has scope 'import'. The 'import' scope is only valid in <dependencyManagement> sections.

Key Improvements:

  1. Context-aware validation: Different error messages for regular dependencies vs. dependency management
  2. Clear guidance: Explicitly tells users where import scope is valid
  3. Focused messaging: Removes confusing list of valid scopes and focuses on the specific problem
  4. Consistent implementation: Applied to both Maven 3 (compat) and Maven 4 (impl) for consistency

Changes Made

Core Changes

  • Added validateDependencyScope() method in both implementations that provides context-aware error messages
  • Enhanced error messages with specific guidance for import scope misuse
  • Maintained backward compatibility - only error messages are improved, no functional changes

Files Modified

  • compat/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java
  • compat/maven-model-builder/src/test/java/org/apache/maven/model/validation/DefaultModelValidatorTest.java
  • impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelValidator.java
  • impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelValidatorTest.java

Additional Improvements

  • Fixed grammar in comments (don't → not)
  • Updated tests to verify the improved error messages
  • Applied spotless formatting for code consistency

Example Impact

This change helps users quickly understand how to fix their Maven configuration:

❌ Incorrect usage (triggers new helpful error):

<dependencies>
  <dependency>
    <groupId>org.example</groupId>
    <artifactId>bom</artifactId>
    <version>1.0</version>
    <type>pom</type>
    <scope>import</scope>  <!-- ❌ Invalid here -->
  </dependency>
</dependencies>

✅ Correct usage:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.example</groupId>
      <artifactId>bom</artifactId>
      <version>1.0</version>
      <type>pom</type>
      <scope>import</scope>  <!-- ✅ Valid here -->
    </dependency>
  </dependencies>
</dependencyManagement>

Related Issues

Addresses the confusion reported in faktorips/faktorips.base#70

Testing

  • ✅ All existing tests pass (161 tests, 0 failures)
  • ✅ Updated tests verify the improved error messages
  • ✅ Code compiles successfully with JDK 21
  • ✅ Spotless formatting applied
  • ✅ No checkstyle violations

The validation logic remains unchanged - only the error messages are enhanced to be more helpful and educational.

Comment thread IMPROVEMENT_SUMMARY.md Outdated
Comment thread IMPROVEMENT_SUMMARY.md Outdated
@gnodet
gnodet force-pushed the improve-dependency-scope-validation-messages branch from c8519ab to 5d2db36 Compare July 23, 2025 10:20
- Enhance error message when 'import' scope is used incorrectly in regular dependencies
- Provide clear guidance that 'import' scope is only valid in <dependencyManagement> sections
- Replace generic error message with context-aware validation
- Update both Maven 3 (compat) and Maven 4 (impl) implementations for consistency
- Update tests to verify the improved error messages
- Fix grammar in comments (don't -> not)
- Apply spotless formatting

Before: 'dependencies.dependency.scope' must be one of [provided, compile, runtime, test, system] but is 'import'.
After:  'dependencies.dependency.scope' has scope 'import'. The 'import' scope is only valid in <dependencyManagement> sections.

This addresses the confusion reported in faktorips/faktorips.base#70
where users receive misleading error messages that suggest 'import' scope is never valid,
when it's actually valid in dependency management sections with type=pom.
@gnodet
gnodet force-pushed the improve-dependency-scope-validation-messages branch from 6c749dc to 3647fe8 Compare July 23, 2025 14:12
@cstamas

cstamas commented Aug 5, 2025

Copy link
Copy Markdown
Member

The deprecated code and impl code is diverging (like new maven4 scopes cannot be validated with deprecated code). If this is not a concern, am ok.

@gnodet gnodet added the enhancement New feature or request label Sep 3, 2025
@gnodet
gnodet merged commit cc5d123 into apache:master Sep 3, 2025
@github-actions github-actions Bot added this to the 4.1.0 milestone Sep 3, 2025
gnodet added a commit to gnodet/maven that referenced this pull request May 19, 2026
…pache#10991)

- Enhance error message when 'import' scope is used incorrectly in regular dependencies
- Provide clear guidance that 'import' scope is only valid in <dependencyManagement> sections
- Replace generic error message with context-aware validation
- Update both Maven 3 (compat) and Maven 4 (impl) implementations for consistency
- Update tests to verify the improved error messages
- Fix grammar in comments (don't -> not)
- Apply spotless formatting

Before: 'dependencies.dependency.scope' must be one of [provided, compile, runtime, test, system] but is 'import'.
After:  'dependencies.dependency.scope' has scope 'import'. The 'import' scope is only valid in <dependencyManagement> sections.

This addresses the confusion reported in faktorips/faktorips.base#70
where users receive misleading error messages that suggest 'import' scope is never valid,
when it's actually valid in dependency management sections with type=pom.
gnodet added a commit that referenced this pull request May 19, 2026
* Add Maven version to error message when rejecting model versions (#10921)

Port #10899 to master, using Session.getMavenVersion() instead of reading properties file.

- Modified validateModelVersion() to accept Session parameter and include Maven version in error messages
- Updated error messages to be single-line and consistent with Maven's style
- Enhanced error messages show which Maven version is rejecting the model version
- Added test coverage to verify Maven version is included in error messages

This helps users (especially IDE users like NetBeans) understand version compatibility issues more clearly.

* Improve ProjectBuildingException error messages with detailed problem reporting (#10975)

* Refactor ProjectBuildingException constructors for improved clarity

* Add unit tests for ProjectBuildingException message generation

* Refactor ProjectBuildingException for improved clarity and maintainability

* Add unit tests for ProjectBuildingException to enhance message validation

* style: Apply Spotless formatting

* Refactor createMessage method to improve error counting logic in ProjectBuildingException

* Refactor ProjectBuildingException for improved clarity and maintainability

* Refactor createMessage method to streamline error message formattingRefactor createMessage method to streamline error message formatting

* Refactor:Spotless formating

* Improve dependency scope validation error messages for import scope (#10991)

- Enhance error message when 'import' scope is used incorrectly in regular dependencies
- Provide clear guidance that 'import' scope is only valid in <dependencyManagement> sections
- Replace generic error message with context-aware validation
- Update both Maven 3 (compat) and Maven 4 (impl) implementations for consistency
- Update tests to verify the improved error messages
- Fix grammar in comments (don't -> not)
- Apply spotless formatting

Before: 'dependencies.dependency.scope' must be one of [provided, compile, runtime, test, system] but is 'import'.
After:  'dependencies.dependency.scope' has scope 'import'. The 'import' scope is only valid in <dependencyManagement> sections.

This addresses the confusion reported in faktorips/faktorips.base#70
where users receive misleading error messages that suggest 'import' scope is never valid,
when it's actually valid in dependency management sections with type=pom.

* Fixes #10950: Throw ProjectBuildingException for reactor cycles; keep CycleDetectedException as cause. (#11091)

Non-cycle model problems still use ProjectBuildingException(results).
Remove misleading comment about projectId/pomFile.

* Fix NullPointerException when clearing project properties

Fix #11552

---------

Co-authored-by: Pasan Kavinda Abeysekara <69195287+PasanAbeysekara@users.noreply.github.com>
Co-authored-by: Arturo Bernal <abernal@apache.org>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants