Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 31 additions & 0 deletions impl/maven-core/src/main/java/org/apache/maven/ReactorReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.jar.JarOutputStream;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -217,13 +218,43 @@ private File determineBuildOutputDirectoryForArtifact(final MavenProject project
if (projectHasOutputFromPreviousSession || projectCompiledDuringThisSession) {
return outputDirectory;
}

if (hasSiteLifecyclePhase(project) && "jar".equals(artifact.getExtension()) && "jar".equals(type)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This checks whether the producer project has entered a site lifecycle phase. But dependency resolution for a consumer module can happen before the producer's mojos have started, meaning project.hasLifecyclePhase("pre-site") etc. would all return false at the time we need it most.

Wouldn't it be more reliable to check the session's top-level goals instead? Something like:

Suggested change
if (hasSiteLifecyclePhase(project) && "jar".equals(artifact.getExtension()) && "jar".equals(type)) {
if (isSiteGoalRequested() && "jar".equals(artifact.getExtension()) && "jar".equals(type)) {

with a helper that inspects session.getGoals() for site-related goals. That way the check doesn't depend on mojo execution order.

return ensureEmptyArtifactFile(artifact);
}
}

// The fall-through indicates that the artifact cannot be found;
// for instance if package produced nothing or classifier problems.
return null;
}

private File ensureEmptyArtifactFile(final Artifact artifact) {
Path target = getArtifactPath(artifact);
synchronized (this) {
if (Files.isRegularFile(target)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using synchronized(this) here locks the entire ReactorReader for every artifact resolution, which could become a bottleneck in large reactor builds. Since you only need mutual exclusion per target path, consider using a ConcurrentHashMap<Path, Path> with computeIfAbsent to avoid global contention:

private final ConcurrentHashMap<Path, File> emptyArtifactCache = new ConcurrentHashMap<>();

private File ensureEmptyArtifactFile(final Artifact artifact) {
    Path target = getArtifactPath(artifact);
    return emptyArtifactCache.computeIfAbsent(target, p -> {
        // create empty jar ...
    });
}

return target.toFile();
}
try {
Files.createDirectories(target.getParent());
try (JarOutputStream ignored = new JarOutputStream(Files.newOutputStream(target))) {
// create an empty jar for site reports that inspect reactor artifacts before package
}
return target.toFile();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This empty JAR is written to the project-local-repo. If the user subsequently runs mvn package (without clean), findInProjectLocalRepository will find this empty JAR and isPackagedArtifactUpToDate may consider it valid (if no output directory exists yet). This could silently produce a broken build.

Consider either:

  • Writing the empty JAR to a temporary/distinct location that won't be picked up by normal artifact resolution, or
  • Adding metadata (e.g., a marker file or attribute) so the reactor reader knows to ignore it in non-site builds.

} catch (IOException e) {
LOGGER.warn("Unable to create empty reactor artifact for '{}'.", artifact, e);
return null;
}
}
}

private boolean hasSiteLifecyclePhase(MavenProject project) {
return project.hasLifecyclePhase("pre-site")
|| project.hasLifecyclePhase("site")
|| project.hasLifecyclePhase("post-site")
|| project.hasLifecyclePhase("site-deploy");
}

private boolean isPackagedArtifactUpToDate(MavenProject project, File packagedArtifactFile) {
Path outputDirectory = Paths.get(project.getBuild().getOutputDirectory());
if (!outputDirectory.toFile().exists()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.io.File;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;

/**
* This is a test set for <a href="https://github.com/apache/maven/issues/12064">GH-12064</a>.
*/
class MavenITgh12064SiteReactorDependenciesTest extends AbstractMavenIntegrationTestCase {

@Test
void testSiteLifecycleResolvesReactorDependencies() throws Exception {
File testDir = extractResources("/gh-12064-site-reactor-dependencies");

Verifier verifier = newVerifier(testDir.getAbsolutePath());
verifier.setAutoclean(false);
verifier.deleteDirectory("producer/target");
verifier.deleteDirectory("consumer/target");
verifier.deleteArtifacts("org.apache.maven.its.gh12064");
verifier.addCliArgument("site");
verifier.execute();
verifier.verifyErrorFreeLog();

verifier.verifyFilePresent("consumer/target/site/dependencies.html");
assertFalse(new File(testDir, "producer/target/classes").exists(), "site must not require compile first");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.maven.its.gh12064</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>consumer</artifactId>

<dependencies>
<dependency>
<groupId>org.apache.maven.its.gh12064</groupId>
<artifactId>producer</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.its.gh12064;

public class Consumer {
public String message() {
return Producer.message();
}
}
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.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.its.gh12064</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
<module>producer</module>
<module>consumer</module>
</modules>

<properties>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.maven.its.gh12064</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>producer</artifactId>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.its.gh12064;

public class Producer {
public static String message() {
return "producer";
}
}
Loading