Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions impl/maven-core/src/main/java/org/apache/maven/ReactorReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,12 @@ private File findInProjectLocalRepository(Artifact artifact) {
* The mojo started event is also captured to determine the lifecycle
* phases the project has been through.
*
* <p>When a project enters its clean phase, its artifacts are cleaned from the
* project-local-repo (per-GAV scope). Since the project-local-repo is located
* under {@code .mvn/} (not under {@code target/}), it is not affected by
* maven-clean-plugin's deletion of {@code target/}, eliminating the race
* condition between clean and install operations in parallel builds.</p>
*
* @param event the execution event
*/
private void processEvent(ExecutionEvent event) {
Expand All @@ -364,9 +370,7 @@ private void processEvent(ExecutionEvent event) {
if (!Objects.equals(phase, phases.peekLast())) {
phases.addLast(phase);
if ("clean".equals(phase)) {
synchronized (project) {
cleanProjectLocalRepository(project);
}
cleanProjectLocalRepository(project);
}
}
}
Expand Down Expand Up @@ -402,6 +406,12 @@ private void installIntoProjectLocalRepository(MavenProject project) {
}
}

/**
* Cleans the project-local-repo artifacts for the given project's GAV coordinates.
* Since the project-local-repo is under {@code .mvn/} and not {@code target/},
* it is not affected by maven-clean-plugin's deletion of {@code target/},
* so there is no race between clean and install operations in parallel builds.
*/
private void cleanProjectLocalRepository(MavenProject project) {
try {
Path artifactPath = getProjectLocalRepo()
Expand Down Expand Up @@ -505,18 +515,7 @@ private Path getArtifactPath(
private Path getProjectLocalRepo() {
if (projectLocalRepository == null) {
Path root = session.getRequest().getRootDirectory();
List<MavenProject> projects = session.getProjects();
if (projects != null) {
projectLocalRepository = projects.stream()
.filter(project -> Objects.equals(root.toFile(), project.getBasedir()))
.findFirst()
.map(project -> project.getBuild().getDirectory())
.map(Paths::get)
.orElseGet(() -> root.resolve("target"))
.resolve(PROJECT_LOCAL_REPO);
} else {
return root.resolve("target").resolve(PROJECT_LOCAL_REPO);
}
projectLocalRepository = root.resolve(".mvn").resolve(PROJECT_LOCAL_REPO);
}
return projectLocalRepository;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.it;

import java.nio.file.Path;

import org.junit.jupiter.api.Test;

/**
* This is a test set for <a href="https://github.com/apache/maven/issues/12646">GH-12646</a>.
*
* Verifies that parallel builds with {@code clean install} do not fail with a race condition
* when the root pom has a parent (super-pom) and other modules write to
* {@code target/project-local-repo} while the root project's clean phase is running.
*
* @since 4.0.0-rc-6
*/
class MavenITgh12646ProjectLocalRepoCleanRaceTest extends AbstractMavenIntegrationTestCase {

/**
* Verify that a parallel {@code clean install} succeeds when the root pom has a parent
* that is also part of the reactor. In this scenario:
* <ol>
* <li>super-pom builds first (no dependencies)</li>
* <li>root pom and module-a..d start in parallel (all depend on super-pom)</li>
* <li>root pom's clean phase runs maven-clean-plugin which deletes target/</li>
* <li>modules complete and install artifacts into target/project-local-repo</li>
* </ol>
* Without the fix, step 3 and 4 race, causing maven-clean-plugin to fail because
* target/project-local-repo is being written to while it tries to delete target/.
*/
@Test
void testParallelCleanInstallWithParentPom() throws Exception {
Path testDir = extractResources("gh-12646-project-local-repo-clean-race");

Verifier verifier = newVerifier(testDir);
verifier.addCliArgument("-T");
verifier.addCliArgument("4");
verifier.addCliArguments("clean", "install");
verifier.execute();
verifier.verifyErrorFreeLog();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.1.0">
<parent>
<groupId>org.apache.maven.its.gh12646</groupId>
<artifactId>super-pom</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../super-pom</relativePath>
</parent>

<artifactId>module-a</artifactId>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package a;

public class A {
public String greet() {
return "Hello from A";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.1.0">
<parent>
<groupId>org.apache.maven.its.gh12646</groupId>
<artifactId>super-pom</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../super-pom</relativePath>
</parent>

<artifactId>module-b</artifactId>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package b;

public class B {
public String greet() {
return "Hello from B";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.1.0">
<parent>
<groupId>org.apache.maven.its.gh12646</groupId>
<artifactId>super-pom</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../super-pom</relativePath>
</parent>

<artifactId>module-c</artifactId>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package c;

public class C {
public String greet() {
return "Hello from C";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.1.0">
<parent>
<groupId>org.apache.maven.its.gh12646</groupId>
<artifactId>super-pom</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../super-pom</relativePath>
</parent>

<artifactId>module-d</artifactId>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package d;

public class D {
public String greet() {
return "Hello from D";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.1.0" root="true">
<parent>
<groupId>org.apache.maven.its.gh12646</groupId>
<artifactId>super-pom</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>super-pom</relativePath>
</parent>

<artifactId>root</artifactId>
<packaging>pom</packaging>

<modules>
<module>super-pom</module>
<module>module-a</module>
<module>module-b</module>
<module>module-c</module>
<module>module-d</module>
</modules>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.1.0">
<groupId>org.apache.maven.its.gh12646</groupId>
<artifactId>super-pom</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
</project>
Loading