Skip to content

[MNG-6843] Parallel build fails due to missing JAR artifacts in compilePath #8194

Closed

Description

@jira-importer

Stepan Hrbacek opened MNG-6843 and commented

Build of our multi module (57) Java maven project is failing phase when running it as parallel in 4 threads (mvn -T 4 clean install). The failure happens during compilation because packages/classes from compile dependencies cannot be found:

[main] [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project common: Compilation failure: Compilation failure: 
[main] [ERROR] /home/common/src/main/java/com/foo/ZonedDateTimeParser.java:[6,32] package org.apache.commons.lang3 does not exist

After enabling debug logging (with thread names) I have found out that a compile path of the failing module is empty (besides target/classes):

When running in 4 threads (-T 4):

[BuilderThread 2] [DEBUG] (f) compilePath = [/home/common/target/classes]
...
[BuilderThread 2] [DEBUG] Command line options:
[BuilderThread 2] [DEBUG] -d /home/common/target/classes -classpath /home/common/target/classes: -sourcepath /home/common/src/main/java:/home/common/target/generated-sources/annotations: -s /home/common/target/generated-sources/annotations -g -nowarn -target 1.8 -source 1.8 -encoding UTF-8

When running in a single thread (-T 1):

[BuilderThread 0] [DEBUG] (f) compilePath = [/home/common/target/classes, /root/.m2/repository/commons-beanutils/commons-beanutils/1.9.3/commons-beanutils-1.9.3.jar, /root/.m2/repository/commons-collections/commons-collections/3.2.2/commons-collections-3.2.2.jar, /root/.m2/repository/org/apache/commons/commons-collections4/4.3/commons-collections4-4.3.jar, /root/.m2/repository/org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar, /root/.m2/repository/org/jooq/jool-java-8/0.9.14/jool-java-8-0.9.14.jar, /root/.m2/repository/org/slf4j/slf4j-api/1.7.26/slf4j-api-1.7.26.jar, /root/.m2/repository/org/springframework/spring-beans/5.1.8.RELEASE/spring-beans-5.1.8.RELEASE.jar, /root/.m2/repository/org/springframework/spring-core/5.1.8.RELEASE/spring-core-5.1.8.RELEASE.jar, /root/.m2/repository/org/springframework/spring-context/5.1.8.RELEASE/spring-context-5.1.8.RELEASE.jar]
...
[BuilderThread 0] [DEBUG] Command line options:
[BuilderThread 0] [DEBUG] -d /home/common/target/classes -classpath /home/common/target/classes:/root/.m2/repository/commons-beanutils/commons-beanutils/1.9.3/commons-beanutils-1.9.3.jar:/root/.m2/repository/commons-collections/commons-collections/3.2.2/commons-collections-3.2.2.jar:/root/.m2/repository/org/apache/commons/commons-collections4/4.3/commons-collections4-4.3.jar:/root/.m2/repository/org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar:/root/.m2/repository/org/jooq/jool-java-8/0.9.14/jool-java-8-0.9.14.jar:/root/.m2/repository/org/slf4j/slf4j-api/1.7.26/slf4j-api-1.7.26.jar:/root/.m2/repository/org/springframework/spring-beans/5.1.8.RELEASE/spring-beans-5.1.8.RELEASE.jar:/root/.m2/repository/org/springframework/spring-core/5.1.8.RELEASE/spring-core-5.1.8.RELEASE.jar:/root/.m2/repository/org/springframework/spring-context/5.1.8.RELEASE/spring-context-5.1.8.RELEASE.jar: -sourcepath /home/common/src/main/java:/home/common/target/generated-sources/annotations: -s /home/common/target/generated-sources/annotations -g -nowarn -target 1.8 -source 1.8 -encoding UTF-8

After adding custom log messages I have found out that the root cause is that org.apache.maven.project.MavenProject.setArtifactFilter() is called with null artifactFilter parameter. The call happens for the failing module from a thread that is building another module. The call stack is:

"BuilderThread 0@2513" prio=5 tid=0xe nid=NA runnable
 java.lang.Thread.State: RUNNABLE
 at org.apache.maven.project.MavenProject.setArtifactFilter(MavenProject.java:1437)
 at org.apache.maven.lifecycle.internal.MojoExecutor.ensureDependenciesAreResolved(MojoExecutor.java:279)
 at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:202)
 at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
 at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
 at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
 at org.apache.maven.lifecycle.internal.builder.multithreaded.MultiThreadedBuilder$1.call(MultiThreadedBuilder.java:190)
 at org.apache.maven.lifecycle.internal.builder.multithreaded.MultiThreadedBuilder$1.call(MultiThreadedBuilder.java:186)
 at java.util.concurrent.FutureTask.run(FutureTask.java:266)
 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
 at java.util.concurrent.FutureTask.run(FutureTask.java:266)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
 at java.lang.Thread.run(Thread.java:748)

The MavenProject objects are shared among all build threads via MavenSession - member org.apache.maven.execution.MavenSession#projects.
As all the MavenSession objects are created by cloning an initial MavenSession object, I have added cloning of projects into the MavenSession clone() method. With this change the build succeeded:

@Override
public MavenSession clone()
{
    try
    {
        MavenSession thisClone = (MavenSession) super.clone();
        thisClone.setProjects(getProjectsClone());
        return thisClone;
    }
    catch ( CloneNotSupportedException e )
    {
        throw new RuntimeException( "Bug", e );
    }
}

private synchronized List<MavenProject> getProjectsClone()
{
    if (projects == null) {
        return null;
    } else {
        if (projects.isEmpty()) {
            return Collections.emptyList();
        } else {
            List<MavenProject> clonedProjects = new ArrayList<>(projects.size());
            for (MavenProject project : projects) {
                clonedProjects.add(project.clone());
            }            return clonedProjects;
        }
    }
}

Affects: 3.6.3

Issue Links:

  • MNG-7335 [Regression] Parallel build fails due to missing JAR artifacts in compilePath
    ("is cloned by")

  • MNG-7220 [REGRESSION] test-classpath incorrectly resolved
    ("causes")

  • MNG-4526 [regression] Shade plugin includes too much (relative to 2.2.1)
    ("is caused by")

  • MNG-7157 Provide a better design for obtaining artifacts from a MavenProject inside a mojo

  • MNG-7212 Prefer use of InheritableThreadLocal to fix possible issue when mojo starts a thread and tries to access some artifacts

  • MNG-7285 [Regression] MavenProject.getArtifacts() not returning correct value across multiple threads
    ("breaks")

  • MNG-7312 Revert ThreadLocal approach from MNG-6843 and MNG-7251
    ("breaks")

Remote Links:

Backported to: 4.0.0-alpha-2, 3.8.2

7 votes, 19 watchers

Activity

  1. jira-importer commented on Jan 6, 2020

    @jira-importer
    Author

    Stepan Hrbacek commented

    PR with the suggested fix created - #310

  2. jira-importer commented on Jan 6, 2020

    @jira-importer
    Author

    Karl Heinz Marbaise commented

    Do you have an example project which shows the behaviour?

  3. jira-importer commented on Jan 6, 2020

    @jira-importer
    Author

    Stepan Hrbacek commented

    None that I can share at the moment :-(.
    I would have to create a dummy project with the same structure that, IMHO, is causing the race condition I am experiencing.

    Easier would be to debug a build - simulate the race condition via pausing threads at org.apache.maven.project.MavenProject.setArtifactFilter() and then resuming them in a desired order.

  4. jira-importer commented on Feb 3, 2020

    @jira-importer
    Author

    Karl Heinz Marbaise commented

    An example project is the right way to go that we are able to reproduce the situation ....

  5. jira-importer commented on Apr 11, 2020

    @jira-importer
    Author

    Falko Modler commented

    I think I am also affected by this bug! Compilation fails sporadically because some RESTEasy packages cannot be found. Those packages come from a transitive dependency of io.quarkus:quarkus-resteasy.
    When the bug strikes, I can see that another module is built concurrently which does not have any RESTEasy dependencies.

    Btw: It seems that in my case the problem can be worked around by defining the respective dependencies directly instead of relying on being pulled in transitively.

    I consider this a rather severe bug.

    Stepan Hrbacek have you ever managed to create a reproducer? I cannot share my project either (closed source).

  6. jira-importer commented on Apr 23, 2020

    @jira-importer
    Author

    Filip Kaliński commented

    As discussed on the Github, I have tried another workaround by using ThreadLocal for MavenProject dependency artifact list related fields and it works for me, at least. It's fkalinski@b38e3c2.
    I have also created a Maven extension https://github.com/fkalinski/parallel-workaround  to make it possible to use the workaround without modified Maven.

    The problem looks to be triggered by aggregator mojos. In my case by maven-help-plugin:effective-pom, which was executed for every project. Every time it was called, the loop over all projects called MavenProject.setArtifactFilter in MojoExecutor.ensureDependenciesAreResolved(), leading to a very high chance of project dependency artifacts being overwritten. I have just moved the call to effective-pom from the parent project to the top-level project, to have it called once, and it was sufficient.

    Of course Maven needs to properly handle also such cases, for sure there are more legitimate uses of aggregator mojos. Maybe cloning of the projects only for the execution of aggregators would be a good approach?

  7. jira-importer commented on Apr 24, 2020

    @jira-importer
    Author

    Stepan Hrbacek commented

    Stepan Hrbacek have you ever managed to create a reproducer? I cannot share my project either (closed source).

    Falko Modler: I have not. Always after removing proprietary stuff (that cannot be shared) the build timing has changed and build succeeded.

  8. jira-importer commented on Apr 24, 2020

    @jira-importer
    Author

    Filip Kaliński commented

    I have added a reproducible test case at https://github.com/fkalinski/parallel-testbed

    There is a script for generating a number of child modules. For 20 child modules and 8 threads the build always fail for me. After applying the workaround it always succeed.

    Of course there can be problems due to the codebase not really prepared for concurency, that can have different ways of fixing...

  9. jira-importer commented on Aug 13, 2020

    @jira-importer
    Author

    Michael Osipov commented

    FWIW, Filip Kaliński , you generate script ist not portable. It does not work with BSD sed. Please apply the following:

     

    index ddc4dbe..2285a68 100755
    --- a/generate.sh
    +++ b/generate.sh
    @@ -1,4 +1,4 @@
     #!/bin/sh
     COUNT=$1
     find . -maxdepth 1 -type d -name 'child*' | grep -v 'child1$' | xargs rm -rf
    -MODULES=$(for i in `seq 2 $COUNT`; do cp -a child1 child$i; sed -i "s/>child1</>child$i</" child$i/pom.xml; echo "<module>child$i</module>"; done); (cat pom-start; echo $MODULES; cat pom-end) > pom.xml
    +MODULES=$(for i in `seq 2 $COUNT`; do cp -a child1 child$i; sed -i"" -e "s/>child1</>child$i</" child$i/pom.xml; echo "<module>child$i</module>"; done); (cat pom-start; echo $MODULES; cat pom-end) > pom.xml
     
    

    I expect it to work with GNU sed too.

  10. jira-importer commented on May 3, 2021

    @jira-importer
    Author

    Michael Osipov commented

    I confirm that this still happens on Maven master with Resolver 1.7.0-SNAPSHOT. This isn't Resolver related because no dependency resolution happens in the build.

  11. jira-importer commented on May 12, 2021

    @jira-importer
    Author

    Matt Nelson commented

    Btw: It seems that in my case the problem can be worked around by defining the respective dependencies directly instead of relying on being pulled in transitively.

    Not a solution, but can be used as a temporary workaround. Having a compilation dependency transitively provided can be fragile if the top level dependency no longer provides that transitive after an update. The dependency plugin's analyze* goals can alert to this situation with a "used and undeclared" warning.

    https://maven.apache.org/plugins/maven-dependency-plugin/analyze-mojo.html

  12. jira-importer commented on Jun 25, 2021

    @jira-importer
    Author
  13. jira-importer commented on Jun 26, 2021

    @jira-importer
    Author

    Michael Osipov commented

    Falko Modler, do you want to backport your PR back to 3.8.x?

  14. jira-importer commented on Jun 26, 2021

    @jira-importer
    Author

    Falko Modler commented

    Michael Osipov Please see #482 for the backport.

  15. jira-importer commented on Jun 26, 2021

    @jira-importer
    Author

    Michael Osipov commented

    Fixed with 76d5f0d942f52650d3bdf775b6af42d23d69066b for maven-3.8.x branch.

  16. jira-importer commented on Oct 6, 2021

    @jira-importer
    Author

    Mickael Istria commented

    This does unfortunately cause a regression in m2e when moving from 3.8.1 to 3.8.3: eclipse-m2e/m2e-core#346 and eclipse-m2e/m2e-core#347 . Apparently, because of this change, using MavenProject across different threads makes that some classpath references are missing in some cases.

  17. jira-importer commented on Oct 6, 2021

    @jira-importer
    Author

    Michael Osipov commented

    Mickael Istria, please spawn a new issue and lets discuss it there. Ping me then.

  18. jira-importer commented on Oct 6, 2021

    @jira-importer
    Author

    Mickael Istria commented

    please spawn a new issue

    Done with MNG-7285

  19. jira-importer commented on Nov 14, 2021

    @jira-importer
    Author
  20. jira-importer commented on Nov 14, 2021

    @jira-importer
    Author

    Michael Osipov commented

    No, because that would mean that the change has been undone in a previous release. A change in history so to speak. In 3.8.3 it is supposed to work somehow. If this is still broken (again) in 3.8.4 then a new or cloned issue needs to be created.

  21. jira-importer commented on Nov 14, 2021

    @jira-importer
    Author

    Falko Modler commented

    If this is still broken (again) in 3.8.4 then a new or cloned issue needs to be created.

    Just to set the record straight:

    There is no "if" here. MNG-7312 removed the ThreadLocal approach that fixed this issue and no alternative solution has been incorporated.

    So the breakage is a fact, MNG-6843 (this issue) is back in 3.8.4.

  22. jira-importer commented on Nov 14, 2021

    @jira-importer
    Author

    Michael Osipov commented

    Correct. The TL fix caused multiple regressions.

  23. jira-importer commented on Nov 14, 2021

    @jira-importer
    Author

    Michael Osipov commented

    I hope we can get this straight in 3.8.5 or 3.9.0.

  24. jira-importer commented on Nov 15, 2021

    @jira-importer
    Author

    Falko Modler commented

    I created a clone (as suggested): MNG-7335

  25. added
    bug
    priority:major
    on Jul 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugpriority:major

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions