slachiewicz opened a new pull request, #14085:
URL: https://github.com/apache/maven/pull/14085

   Fixes #14081. Backport target: `maven-4.0.x`.
   
   ## Problem
   
   With `-b concurrent` and more than one thread, a Maven 3 API mojo late in 
one step (e.g. `spring-boot:repackage` in `package`) can see an empty 
`MavenProject.getArtifacts()` because a mojo of a parallel step of the *same* 
project (e.g. `jacoco:report` at `after:test`) replaces the project's resolved 
artifacts in between. The build still reports SUCCESS; the failure only shows 
up at runtime.
   
   ## Cause
   
   `MojoExecutor` creates a fresh `DependencyContext` per build-plan step. 
`DependencyContext.isResolutionRequiredForCurrentProject()` is an 
object-identity check against `MavenProject.getDependencyArtifacts()`, so a 
fresh context with **no scopes** still reports that resolution is required. 
`ensureDependenciesAreResolved` then calls 
`LifecycleDependencyResolver.resolveProjectDependencies` with empty scope 
collections; `getDependencies` returns an empty set and 
`setResolvedArtifacts(empty)` writes it into the shared project. The later mojo 
of the first step skips its own resolution (its context still sees unchanged 
`dependencyArtifacts`) and filters that empty set through its own artifact 
filter.
   
   The per-project lock from #13591 serializes individual mojo executions; it 
does not protect this step-level assumption, because `war:war` releases the 
lock, `jacoco:report` takes it and empties the state, then `repackage` takes it 
and skips resolution.
   
   ## Fix
   
   Skip `resolveProjectDependencies` when the step has neither scopes to 
collect nor scopes to resolve. The scope check uses `||` so collection-only 
mojos (e.g. `flatten:flatten`) still resolve. The unconditional 
`setArtifactFilter(...)` write is left completely unchanged: a `null` filter is 
meaningful for MNG-3297 and must still be applied to every Maven 3 mojo.
   
   A regression test (`MojoExecutorEmptyScopeTest`) resolves compile+provided 
artifacts for one step, runs a no-scope step, and asserts that the resolver is 
not called for the empty step and that a later mojo of the original step still 
sees both artifacts. The test fails without this change.
   
   ## Verification
   
   - `mvn -o -pl impl/maven-core test`: 742 tests, 0 failures, 1 skipped.
   - `mvn spotless:check`: clean.
   - Reproducer (`-b concurrent -T1C`, Java 27): detector mojo sees 0 artifacts 
before, 1 after; `-Djacoco.skip=true` is also fixed; `-T1`, default builder and 
Maven 3 were already correct.
   
   Workaround until this is released: bind the dependency-free mojo to 
`after:verify`, or use `-T1` / the default builder.
   
   I hereby declare this contribution to be licensed under the [Apache License 
Version 2.0, January 2004](https://www.apache.org/licenses/LICENSE-2.0).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to