Skip to content

Commit

Permalink
[MARTIFACT-48] ignore by glob on $groupId/$filename
Browse files Browse the repository at this point in the history
  • Loading branch information
hboutemy committed Sep 24, 2023
1 parent 8335310 commit 3f83ae1
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 27 deletions.
17 changes: 17 additions & 0 deletions src/it/buildinfo-multi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@

<build>
<plugins>
<plugin>
<groupId>org.spdx</groupId>
<artifactId>spdx-maven-plugin</artifactId>
<version>0.6.3</version>
<executions>
<execution>
<goals>
<goal>createSPDX</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
Expand All @@ -65,6 +77,11 @@
<goals>
<goal>buildinfo</goal>
</goals>
<configuration>
<ignore>
<ignore>*/*.spdx.json</ignore>
</ignore>
</configuration>
</execution>
</executions>
</plugin>
Expand Down
4 changes: 4 additions & 0 deletions src/it/buildinfo-multi/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ assert buildinfo.contains( "version=1.0-SNAPSHOT" )
assert buildinfo.contains( "outputs.1.coordinates=org.apache.maven.plugins.it:multi-modA" )
assert buildinfo.contains( "outputs.1.0.filename=multi-modA-1.0-SNAPSHOT.pom" )
assert buildinfo.contains( "outputs.1.1.filename=multi-modA-1.0-SNAPSHOT.jar" )
assert !buildinfo.contains( "outputs.1.2.filename=" )
assert buildinfo.contains( "# ignored multi-modA-1.0-SNAPSHOT.spdx.json" )
assert buildinfo.contains( "outputs.2.coordinates=org.apache.maven.plugins.it:multi-modB" )
assert buildinfo.contains( "outputs.2.0.filename=multi-modB-1.0-SNAPSHOT.pom" )
assert buildinfo.contains( "outputs.2.1.filename=multi-modB-1.0-SNAPSHOT.jar" )
assert !buildinfo.contains( "outputs.2.2.filename=" )
assert buildinfo.contains( "# ignored multi-modB-1.0-SNAPSHOT.spdx.json" )
assert !buildinfo.contains( ".buildinfo" )
assert buildinfo.contains( "mvn.aggregate.artifact-id=multi-modB" )

Expand Down
4 changes: 2 additions & 2 deletions src/it/compare-mono/invoker.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@

# initial reference build: install
invoker.goals.1=clean install
# second build: package (could be verify, but not install to avoid overriding reference)
invoker.goals.2=clean package artifact:compare
# second build: verify (could be package, but not install to avoid overriding reference)
invoker.goals.2=clean verify artifact:compare -Dbuildinfo.ignore=*/*.spdx.json
invoker.buildResult.2=failure
14 changes: 14 additions & 0 deletions src/it/compare-mono/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,19 @@
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.spdx</groupId>
<artifactId>spdx-maven-plugin</artifactId>
<version>0.6.3</version>
<executions>
<execution>
<goals>
<goal>createSPDX</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2 changes: 2 additions & 0 deletions src/it/compare-mono/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ String compare = compareFile.text
assert compare.contains( "version=1.0-SNAPSHOT" )
assert compare.contains( "ok=1" )
assert compare.contains( "ko=1" )
assert compare.contains( "ignored=1" )
assert compare.contains( 'okFiles="mono-1.0-SNAPSHOT.pom"' )
assert compare.contains( 'koFiles="mono-1.0-SNAPSHOT.jar"' )
assert compare.contains( 'ignoredFiles="mono-1.0-SNAPSHOT.spdx.json"' )
if( File.separator == '/' ) {
assert compare.contains( '# diffoscope target/reference/org.apache.maven.plugins.it.compare/mono-1.0-SNAPSHOT.jar target/mono-1.0-SNAPSHOT.jar' )
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.maven.archiver.MavenArchiver;
import org.apache.maven.artifact.Artifact;
Expand Down Expand Up @@ -79,10 +78,11 @@ public abstract class AbstractBuildinfoMojo extends AbstractMojo {
private boolean ignoreJavadoc;

/**
* Artifacts to ignore, specified as <code>extension</code> or <code>classifier.extension</code>.
* Artifacts to ignore, specified as a glob matching against <code>${groupId}/${filename}</code>, for example
* <code>*</>/*.xml</code>.
*/
@Parameter(property = "buildinfo.ignore", defaultValue = "")
private Set<String> ignore;
private List<String> ignore;

/**
* Detect projects/modules with install or deploy skipped: avoid taking fingerprints.
Expand Down Expand Up @@ -216,7 +216,7 @@ protected void copyAggregateToRoot(File aggregate) throws MojoExecutionException
* @param mono is it a mono-module build?
* @return a Map of artifacts added to the build info with their associated property key prefix
* (<code>outputs.[#module.].#artifact</code>)
* @throws MojoExecutionException
* @throws MojoExecutionException if anything goes wrong
*/
protected Map<Artifact, String> generateBuildinfo(boolean mono) throws MojoExecutionException {
MavenProject root = mono ? project : getExecutionRoot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.nio.file.*;
import java.util.*;
import java.util.stream.Collectors;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.maven.artifact.Artifact;
Expand All @@ -41,6 +36,7 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.rtinfo.RuntimeInformation;
import org.apache.maven.shared.utils.PropertyUtils;
import org.apache.maven.shared.utils.StringUtils;
import org.apache.maven.toolchain.Toolchain;

/**
Expand All @@ -55,7 +51,7 @@ class BuildInfoWriter {
private final Map<Artifact, String> artifacts = new LinkedHashMap<>();
private int projectCount = -1;
private boolean ignoreJavadoc = true;
private Set<String> ignore;
private List<PathMatcher> ignore;
private Toolchain toolchain;

BuildInfoWriter(
Expand Down Expand Up @@ -223,6 +219,8 @@ void printArtifacts(MavenProject project) throws MojoExecutionException {
continue;
}
if (isIgnore(attached)) {
p.println("# ignored " + getArtifactFilename(attached));
artifacts.put(attached, null);
continue;
}
printArtifact(prefix, n++, attached);
Expand Down Expand Up @@ -251,7 +249,7 @@ private void printArtifact(String prefix, int i, Artifact artifact) throws MojoE
artifacts.put(artifact, prefix);
}

private String getArtifactFilename(Artifact artifact) {
static String getArtifactFilename(Artifact artifact) {
StringBuilder path = new StringBuilder(128);

path.append(artifact.getArtifactId()).append('-').append(artifact.getBaseVersion());
Expand All @@ -262,8 +260,7 @@ private String getArtifactFilename(Artifact artifact) {

ArtifactHandler artifactHandler = artifact.getArtifactHandler();

if (artifactHandler.getExtension() != null
&& artifactHandler.getExtension().length() > 0) {
if (StringUtils.isNotEmpty(artifactHandler.getExtension())) {
path.append('.').append(artifactHandler.getExtension());
}

Expand Down Expand Up @@ -317,15 +314,14 @@ void setIgnoreJavadoc(boolean ignoreJavadoc) {
this.ignoreJavadoc = ignoreJavadoc;
}

void setIgnore(Set<String> ignore) {
this.ignore = ignore;
void setIgnore(List<String> ignore) {
FileSystem fs = FileSystems.getDefault();
this.ignore = ignore.stream().map(i -> fs.getPathMatcher("glob:" + i)).collect(Collectors.toList());
}

private boolean isIgnore(Artifact attached) {
String classifier = attached.getClassifier();
String extension = attached.getType();
String search = (classifier == null) ? "" : (classifier + '.') + extension;
return ignore.contains(search);
Path path = Paths.get(attached.getGroupId() + '/' + getArtifactFilename(attached));
return ignore.stream().anyMatch(m -> m.matches(path));
}

public void setToolchain(Toolchain toolchain) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.RemoteRepository;

import static org.apache.maven.plugins.artifact.buildinfo.BuildInfoWriter.getArtifactFilename;

/**
* Compare current build output (from {@code package}) against reference either previously {@code install}-ed or downloaded from a remote
* repository: comparison results go to {@code .buildcompare} file.
Expand Down Expand Up @@ -166,10 +168,16 @@ private void compareWithReference(Map<Artifact, String> artifacts, File referenc
List<String> okFilenames = new ArrayList<>();
List<String> koFilenames = new ArrayList<>();
List<String> diffoscopes = new ArrayList<>();
List<String> ignored = new ArrayList<>();
File referenceDir = referenceBuildinfo.getParentFile();
for (Map.Entry<Artifact, String> entry : artifacts.entrySet()) {
Artifact artifact = entry.getKey();
String prefix = entry.getValue();
if (prefix == null) {
// ignored file
ignored.add(getArtifactFilename(artifact));
continue;
}

String[] checkResult = checkArtifact(artifact, prefix, reference, actual, referenceDir);
String filename = checkResult[0];
Expand All @@ -184,22 +192,24 @@ private void compareWithReference(Map<Artifact, String> artifacts, File referenc
}
}

int ko = artifacts.size() - ok;
int ko = artifacts.size() - ok - ignored.size();
int missing = reference.size() / 3 /* 3 property keys par file: filename, length and checksums.sha512 */;

if (ko + missing > 0) {
getLog().error("Reproducible Build output summary: "
+ MessageUtils.buffer().success(ok + " files ok")
+ ", " + MessageUtils.buffer().failure(ko + " different")
+ ((missing == 0) ? "" : (", " + MessageUtils.buffer().failure(missing + " missing"))));
+ ((missing == 0) ? "" : (", " + MessageUtils.buffer().failure(missing + " missing")))
+ ((ignored.isEmpty()) ? "" : (", " + MessageUtils.buffer().warning(ignored.size() + " ignored"))));
getLog().error("see "
+ MessageUtils.buffer()
.project("diff " + relative(referenceBuildinfo) + " " + relative(buildinfoFile))
.toString());
getLog().error("see also https://maven.apache.org/guides/mini/guide-reproducible-builds.html");
} else {
getLog().info("Reproducible Build output summary: "
+ MessageUtils.buffer().success(ok + " files ok"));
+ MessageUtils.buffer().success(ok + " files ok")
+ ((ignored.isEmpty()) ? "" : (", " + MessageUtils.buffer().warning(ignored.size() + " ignored"))));
}

// save .compare file
Expand All @@ -210,8 +220,10 @@ private void compareWithReference(Map<Artifact, String> artifacts, File referenc
p.println("version=" + project.getVersion());
p.println("ok=" + ok);
p.println("ko=" + ko);
p.println("ignored=" + ignored.size());
p.println("okFiles=\"" + StringUtils.join(okFilenames.iterator(), " ") + '"');
p.println("koFiles=\"" + StringUtils.join(koFilenames.iterator(), " ") + '"');
p.println("ignoredFiles=\"" + StringUtils.join(ignored.iterator(), " ") + '"');
Properties ref = PropertyUtils.loadOptionalProperties(referenceBuildinfo);
String v = ref.getProperty("java.version");
if (v != null) {
Expand Down

0 comments on commit 3f83ae1

Please sign in to comment.