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
10 changes: 10 additions & 0 deletions apache-maven/src/assembly/maven/conf/maven-user.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
${includes} = ?"${maven.user.conf}/maven-user.properties", \
?"${maven.project.conf}/maven-user.properties"

#
# Project-Local Repository
#
# Location of the project-local repository used by the reactor to share artifacts between
# modules during a build. This repository enables partial and resumable builds (e.g.
# mvn verify -r :module) without requiring install. The path may be absolute or relative
# to the root directory.
#
# maven.project.local.repo = ${maven.rootDirectory}/.mvn/target/project-local-repo

#
# Maven Cache Configuration
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,21 @@ public final class Constants {
@Config
public static final String MAVEN_VERSION_FILTER = "maven.session.versionFilter";

/**
* User property for overriding the project-local repository path used by the reactor reader to share
* artifacts between modules during a build. This repository enables partial and resumable builds
* (e.g. {@code mvn verify -r :module}) without requiring {@code install}.
* <p>
* The path may be absolute or relative to the root directory. If relative, it is resolved against
* the root directory of the project.
* </p>
* Default value: <code>${maven.rootDirectory}/.mvn/target/project-local-repo</code>.
*
* @since 4.0.0
*/
@Config(defaultValue = "${maven.rootDirectory}/.mvn/target/project-local-repo")
public static final String MAVEN_PROJECT_LOCAL_REPO = "maven.project.local.repo";

/**
* User property for chained LRM: the new "head" local repository to use, and "push" the existing into tail.
* Similar to <code>maven.repo.local.tail</code>, this property may contain comma separated list of paths to be
Expand Down
34 changes: 21 additions & 13 deletions impl/maven-core/src/main/java/org/apache/maven/ReactorReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.api.Constants;
import org.apache.maven.api.model.Model;
import org.apache.maven.api.services.Lookup;
import org.apache.maven.eventspy.EventSpy;
Expand Down Expand Up @@ -352,6 +353,13 @@ 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/target/} (not under the project's own {@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 +372,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 +408,13 @@ 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/target/} and not the project's
* own {@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,17 +518,12 @@ 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);
String userPath = session.getRequest().getUserProperties().getProperty(Constants.MAVEN_PROJECT_LOCAL_REPO);
if (userPath != null && !userPath.isEmpty()) {
Path path = Paths.get(userPath);
projectLocalRepository = path.isAbsolute() ? path : root.resolve(path);
} else {
return root.resolve("target").resolve(PROJECT_LOCAL_REPO);
projectLocalRepository = root.resolve(".mvn").resolve("target").resolve(PROJECT_LOCAL_REPO);
}
}
return projectLocalRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ void testConsumerPomInProjectLocalRepo() throws Exception {

// Check the consumer POM in the project-local-repo
Path consumerPom = basedir.resolve(Path.of(
".mvn",
"target",
"project-local-repo",
"org.apache.maven.its.bom-property",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ void testConsumerPomWithBomFromSettingsProfileRepo() throws Exception {

// Verify the consumer POM was generated
Path consumerPom = basedir.resolve(Path.of(
".mvn",
"target",
"project-local-repo",
"org.apache.maven.its.cpbom",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void testConsumerPomFiltersScopes() throws Exception {
verifier.verifyErrorFreeLog();

Path consumerPom = basedir.resolve(Paths.get(
".mvn",
"target",
"project-local-repo",
"org.apache.maven.its.gh11162",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ void testMixinsWithFlattening() throws Exception {

// Verify consumer POM was created
Path consumerPom = basedir.resolve(Paths.get(
".mvn",
"target",
"project-local-repo",
"org.apache.maven.its.gh11456",
Expand Down Expand Up @@ -104,6 +105,7 @@ void testMixinsWithPreserveModelVersion() throws Exception {

// Verify consumer POM was created
Path consumerPom = basedir.resolve(Paths.get(
".mvn",
"target",
"project-local-repo",
"org.apache.maven.its.gh11456",
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
Expand Up @@ -18,7 +18,6 @@
*/
package org.apache.maven.it;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

Expand All @@ -44,11 +43,11 @@ public class MavenITmng3043BestEffortReactorResolutionTest extends AbstractMaven
@Test
public void testitTestPhase() throws Exception {
Path testDir = extractResources("mng-3043");
Files.createDirectories(testDir.resolve(".mvn"));

Verifier verifier = newVerifier(testDir);
verifier.setAutoclean(false);
verifier.deleteDirectory("target");
verifier.deleteDirectory(".mvn/target");
verifier.deleteDirectory("consumer-a/target");
verifier.deleteDirectory("consumer-b/target");
verifier.deleteDirectory("consumer-c/target");
Expand Down Expand Up @@ -105,6 +104,7 @@ public void testitPackagePhase() throws Exception {
Verifier verifier = newVerifier(testDir);
verifier.setAutoclean(false);
verifier.deleteDirectory("target");
verifier.deleteDirectory(".mvn/target");
verifier.deleteDirectory("consumer-a/target");
verifier.deleteDirectory("consumer-b/target");
verifier.deleteDirectory("consumer-c/target");
Expand Down Expand Up @@ -165,6 +165,7 @@ public void testitPackagePhasesSlitted() throws Exception {
Verifier verifier = newVerifier(testDir);
verifier.setAutoclean(false);
verifier.deleteDirectory("target");
verifier.deleteDirectory(".mvn/target");
verifier.deleteDirectory("consumer-a/target");
verifier.deleteDirectory("consumer-b/target");
verifier.deleteDirectory("consumer-c/target");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public void testWithPath() throws Exception {
Verifier verifier = newVerifier(testDir);
verifier.setAutoclean(false);
verifier.deleteDirectory("target");
verifier.deleteDirectory(".mvn/target");
verifier.deleteArtifacts("org.apache.maven.its.mng5102");
verifier.addCliArguments("install", "-Dmaven.consumer.pom.flatten=true");
verifier.execute();
Expand All @@ -63,9 +64,9 @@ public void testWithPath() throws Exception {
assertEquals("true", props.getProperty("project.properties.mixin3"));

verifier.verifyFilePresent(
"target/project-local-repo/org.apache.maven.its.mng5102/child/0.1/child-0.1-consumer.pom");
".mvn/target/project-local-repo/org.apache.maven.its.mng5102/child/0.1/child-0.1-consumer.pom");
List<String> lines = verifier.loadLines(
"target/project-local-repo/org.apache.maven.its.mng5102/child/0.1/child-0.1-consumer.pom");
".mvn/target/project-local-repo/org.apache.maven.its.mng5102/child/0.1/child-0.1-consumer.pom");
assertTrue(lines.stream().noneMatch(l -> l.contains("<mixin>")));
}

Expand All @@ -90,6 +91,7 @@ public void testWithGav() throws Exception {
verifier = newVerifier(testDir.resolve("project"));
verifier.setAutoclean(false);
verifier.deleteDirectory("target");
verifier.deleteDirectory(".mvn/target");
verifier.addCliArguments("install", "-Dmaven.consumer.pom.flatten");
verifier.execute();
verifier.verifyErrorFreeLog();
Expand All @@ -99,9 +101,9 @@ public void testWithGav() throws Exception {
assertEquals("true", props.getProperty("project.properties.mixin2"));

verifier.verifyFilePresent(
"target/project-local-repo/org.apache.maven.its.mng5102/gav/0.1/gav-0.1-consumer.pom");
".mvn/target/project-local-repo/org.apache.maven.its.mng5102/gav/0.1/gav-0.1-consumer.pom");
List<String> lines = verifier.loadLines(
"target/project-local-repo/org.apache.maven.its.mng5102/gav/0.1/gav-0.1-consumer.pom");
".mvn/target/project-local-repo/org.apache.maven.its.mng5102/gav/0.1/gav-0.1-consumer.pom");
assertTrue(lines.stream().anyMatch(l -> l.contains("<mixin>")));
}

Expand All @@ -126,6 +128,7 @@ public void testWithClassifier() throws Exception {
verifier = newVerifier(testDir.resolve("project"));
verifier.setAutoclean(false);
verifier.deleteDirectory("target");
verifier.deleteDirectory(".mvn/target");
verifier.addCliArguments("install", "-Dmaven.consumer.pom.flatten");
verifier.execute();
verifier.verifyErrorFreeLog();
Expand All @@ -135,9 +138,9 @@ public void testWithClassifier() throws Exception {
assertEquals("true", props.getProperty("project.properties.mixin4"));

verifier.verifyFilePresent(
"target/project-local-repo/org.apache.maven.its.mng5102/classifier/0.1/classifier-0.1-consumer.pom");
".mvn/target/project-local-repo/org.apache.maven.its.mng5102/classifier/0.1/classifier-0.1-consumer.pom");
List<String> lines = verifier.loadLines(
"target/project-local-repo/org.apache.maven.its.mng5102/classifier/0.1/classifier-0.1-consumer.pom");
".mvn/target/project-local-repo/org.apache.maven.its.mng5102/classifier/0.1/classifier-0.1-consumer.pom");
assertTrue(lines.stream().anyMatch(l -> l.contains("<mixin>")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void testNotPreserving() throws Exception {
"The consumer POM for org.apache.maven.its:mng-8414:jar:1.0.0-SNAPSHOT cannot be downgraded to 4.0.0.");

Path consumerPom = basedir.resolve(Paths.get(
".mvn",
"target",
"project-local-repo",
"org.apache.maven.its",
Expand Down Expand Up @@ -83,6 +84,7 @@ void testPreserving() throws Exception {
verifier.verifyTextNotInLog("cannot be downgraded to 4.0.0.");

Path consumerPom = basedir.resolve(Paths.get(
".mvn",
"target",
"project-local-repo",
"org.apache.maven.its",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ void testIt() throws Exception {

// validate consumer pom
Path consumerPom = basedir.resolve(Paths.get(
".mvn",
"target",
"project-local-repo",
"org.apache.maven.its.mng8645",
Expand Down
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>
Loading
Loading