Skip to content

Commit

Permalink
Fix parallel builds [DI-128] (#75)
Browse files Browse the repository at this point in the history
Fixes cryptic errors in other plugins when the multimodule project is
built using a parallel build.
  • Loading branch information
kwart authored May 24, 2024
1 parent 057f155 commit 30f79f1
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 117 deletions.
16 changes: 5 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,12 @@
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-java</artifactId>
<version>1.1.1</version>
<version>1.2.0</version>
</dependency>

<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-artifact-transfer</artifactId>
<version>0.13.1</version>
<exclusions>
<exclusion>
<groupId>org.apache.maven</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.16.0</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -122,6 +115,7 @@
<version>3.3.0</version>
<executions>
<execution>
<id>checkstyle</id>
<phase>validate</phase>
<goals>
<goal>checkstyle</goal>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
package com.hazelcast.maven.attribution;

import com.hazelcast.maven.attribution.resolver.AttribResourceResolver;
import org.apache.commons.io.FileUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingRequest;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
Expand All @@ -16,7 +29,9 @@
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -30,19 +45,6 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.apache.commons.io.FileUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingRequest;

public abstract class AbstractAttributionMojo extends AbstractMojo {

protected static final String DEFAULT_COPYRIGHT_PATTERN = "(?i)^([\\s/*]*)(((\\(c\\))|(copyright))\\s+\\S[^;{}]*)$";
Expand Down Expand Up @@ -125,7 +127,7 @@ public abstract class AbstractAttributionMojo extends AbstractMojo {
protected List<String> exclusionPatterns;

@Component
private ResolverComponent resolverComponent;
protected AttribResourceResolver resourceResolver;

public void execute() throws MojoExecutionException, MojoFailureException {
if (skip) {
Expand Down Expand Up @@ -341,42 +343,46 @@ private static byte[] toByteArray(InputStream in) throws IOException {
}
}

protected File resolve(Artifact artifact) {
if (!SOURCES_CLASSIFIER.equals(artifact.getClassifier())) {
return null;
}

Artifact resolvedArtifact = null;
try {
resolvedArtifact = resolverComponent.getResolver().resolveArtifact(getProjectBuildingRequest(project), artifact)
.getArtifact();
getLog().debug("Resolved source jar: " + resolvedArtifact.getFile());
} catch (Exception e1) {
getLog().info("Resolving failed for " + artifact);
protected Set<String> getReactorProjectGaSet() {
Set<String> gaSet = new HashSet<>();
if (reactorProjects != null) {
for (final MavenProject p : reactorProjects) {
String projectGaKey = gaKey(p.getGroupId(), p.getArtifactId());
gaSet.add(projectGaKey);
}
}
return resolvedArtifact == null ? null : resolvedArtifact.getFile();
getLog().debug("Reactor GAs: " + gaSet);
return gaSet;
}

protected Artifact createResourceArtifact(final Artifact artifact, final String classifier) {
@SuppressWarnings("deprecation")
final DefaultArtifact a = (DefaultArtifact) resolverComponent.getArtifactFactory().createArtifactWithClassifier(
artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), "jar", classifier);

a.setRepository(artifact.getRepository());

return a;
}

private ProjectBuildingRequest getProjectBuildingRequest(MavenProject currentProject) {
protected ProjectBuildingRequest getProjectBuildingRequest(MavenProject currentProject) {
return new DefaultProjectBuildingRequest(session.getProjectBuildingRequest())
.setRemoteRepositories(currentProject.getRemoteArtifactRepositories());
}

protected List<Artifact> getRuntimeAndCompileScopedArtifacts(MavenProject p) {
Set<Artifact> artifacts = p.getArtifacts();
List<Artifact> list = new ArrayList<>(artifacts.size());
for (Artifact a : artifacts) {
if (Artifact.SCOPE_COMPILE.equals(a.getScope()) || Artifact.SCOPE_RUNTIME.equals(a.getScope())) {
list.add(a);
}
}
getLog().debug("Project " + gavKey(p.getArtifact()) + " artifacts: " + list);
return list;
}

protected static String gaKey(Artifact artifact) {
if (artifact == null) {
return "[null GA]";
}
return gaKey(artifact.getGroupId(), artifact.getArtifactId());
}

protected static String gavKey(Artifact artifact) {
if (artifact == null) {
return "[null GAV]";
}
return gavKey(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,62 +1,65 @@
package com.hazelcast.maven.attribution;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingRequest;

import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;

/**
* Generates an aggregated attribution file for a (possibly) multi-module project.
*/
@Mojo(name = "aggregate", defaultPhase = LifecyclePhase.PACKAGE, aggregator = true, inheritByDefault = false,
requiresDependencyCollection = ResolutionScope.RUNTIME, threadSafe = true)
@Mojo(name = "aggregate", defaultPhase = LifecyclePhase.PROCESS_RESOURCES,
aggregator = true, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME,
requiresDependencyResolution = ResolutionScope.NONE,
threadSafe = true)
public class AggregateAttributionMojo extends AbstractAttributionMojo {

@Override
protected Map<String, File> resolveSourceJars() {
Set<String> projectGaSet = new HashSet<>();

Set<Artifact> artifacts = new HashSet<>();
final Map<String, MavenProject> projectMap = new HashMap<>();
Set<String> artifacts = new HashSet<>();
Map<String, File> result = new HashMap<String, File>();
if (reactorProjects != null) {
Set<String> projectGaSet = getReactorProjectGaSet();
for (final MavenProject p : reactorProjects) {
String projectGaKey = gaKey(p.getGroupId(), p.getArtifactId());
projectGaSet.add(projectGaKey);
List<Artifact> projectArtifacts = p.getRuntimeArtifacts();
artifacts.addAll(projectArtifacts);
getLog().debug("Project " + projectGaKey + " artifacts: " + projectArtifacts);
List<Artifact> projectArtifacts = getRuntimeAndCompileScopedArtifacts(p);
getLog().debug("Project " + gaKey(p.getGroupId(), p.getArtifactId()) + " artifacts: " + projectArtifacts);

ProjectBuildingRequest pbr = getProjectBuildingRequest(p);
for (Artifact artifact : projectArtifacts) {
String gaKey = gaKey(artifact);
String gavKey = gavKey(artifact);
if (artifacts.contains(gavKey)) {
getLog().debug("Artifact already resolved " + gavKey);
continue;
}
artifacts.add(gavKey);
if (projectGaSet.contains(gaKey)) {
// don't include (sub)project sources
getLog().debug("Skipping (sub)project artifact " + gaKey);
continue;
}
File sourceFile = resourceResolver.resolveSourceFromArtifact(pbr, p, artifact);
if (sourceFile == null) {
getLog().debug("No source file resolved for " + gavKey);
continue;
}
getLog().debug("Resolved " + sourceFile);
result.put(gavKey, sourceFile);
}
}
} else {
getLog().info("Null reactorProjects");
}
getLog().debug("Project GAs: " + projectGaSet);
getLog().debug("Artifacts size: " + artifacts.size());

Map<String, File> result = new HashMap<String, File>();
for (Artifact artifact : artifacts) {
String gaKey = gaKey(artifact);
if (projectGaSet.contains(gaKey)) {
// don't include (sub)project sources
getLog().debug("Skipping (sub)project artifact " + gaKey);
continue;
}
String gavKey = gavKey(artifact);
File sourceFile = resolve(createResourceArtifact(artifact, SOURCES_CLASSIFIER));
if (sourceFile == null) {
getLog().debug("No source file resolved for " + gavKey);
continue;
}
getLog().debug("Resolved " + sourceFile);
result.put(gavKey, sourceFile);
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,35 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.ProjectBuildingRequest;

/**
* Generates the attribution file for a single project.
*/
@Mojo(name = "generate", defaultPhase = LifecyclePhase.PACKAGE,
requiresDependencyCollection = ResolutionScope.RUNTIME, threadSafe = true)
@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_RESOURCES,
requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME, threadSafe = true)
public class GenerateAttributionMojo extends AbstractAttributionMojo {

@Override
protected Map<String, File> resolveSourceJars() {
Map<String, File> result = new HashMap<>();
List<Artifact> artifacts = project.getRuntimeArtifacts();
List<Artifact> artifacts = getRuntimeAndCompileScopedArtifacts(project);
ProjectBuildingRequest pbr = getProjectBuildingRequest(project);
Set<String> projectGaSet = getReactorProjectGaSet();
for (Artifact artifact : artifacts) {
String gaKey = gaKey(artifact);
if (projectGaSet.contains(gaKey)) {
getLog().debug("Skipping reactor project artifact " + gaKey);
continue;
}
String gavKey = gavKey(artifact);
File sourceFile = resolve(createResourceArtifact(artifact, SOURCES_CLASSIFIER));
File sourceFile = resourceResolver.resolveSourceFromArtifact(pbr, project, artifact);
if (sourceFile == null) {
getLog().debug("No source file resolved for " + gavKey);
continue;
Expand Down

This file was deleted.

Loading

0 comments on commit 30f79f1

Please sign in to comment.