Skip to content

Commit

Permalink
apply improvements proposed by IDE
Browse files Browse the repository at this point in the history
  • Loading branch information
hboutemy committed Sep 24, 2023
1 parent ccc4206 commit 0acc794
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -224,7 +224,7 @@ protected Map<Artifact, String> generateBuildinfo(boolean mono) throws MojoExecu
buildinfoFile.getParentFile().mkdirs();

try (PrintWriter p = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(buildinfoFile), StandardCharsets.UTF_8)))) {
new OutputStreamWriter(Files.newOutputStream(buildinfoFile.toPath()), StandardCharsets.UTF_8)))) {
BuildInfoWriter bi = new BuildInfoWriter(getLog(), p, mono, artifactHandlerManager, rtInformation);
bi.setIgnoreJavadoc(ignoreJavadoc);
bi.setIgnore(ignore);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
package org.apache.maven.plugins.artifact.buildinfo;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
Expand Down Expand Up @@ -194,7 +194,7 @@ private boolean hasBadOutputTimestamp() {
private Properties loadIssues() throws MojoExecutionException {
try (InputStream in = (pluginIssues == null)
? getClass().getResourceAsStream("not-reproducible-plugins.properties")
: new FileInputStream(pluginIssues)) {
: Files.newInputStream(pluginIssues.toPath())) {
Properties prop = new Properties();
prop.load(in);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -128,7 +128,7 @@ protected void skip(MavenProject last) throws MojoExecutionException {
*
* @param artifacts 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
*/
private void checkAgainstReference(Map<Artifact, String> artifacts, boolean mono) throws MojoExecutionException {
MavenProject root = mono ? project : getExecutionRoot();
Expand Down Expand Up @@ -216,7 +216,7 @@ private void compareWithReference(Map<Artifact, String> artifacts, File referenc
File buildcompare = new File(
buildinfoFile.getParentFile(), buildinfoFile.getName().replaceFirst(".buildinfo$", ".buildcompare"));
try (PrintWriter p = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(buildcompare), StandardCharsets.UTF_8)))) {
new OutputStreamWriter(Files.newOutputStream(buildcompare.toPath()), StandardCharsets.UTF_8)))) {
p.println("version=" + project.getVersion());
p.println("ok=" + ok);
p.println("ko=" + ko);
Expand Down Expand Up @@ -282,10 +282,8 @@ private String diffoscope(Artifact a, File referenceDir) {
// notice: actual file name may have been defined in pom
// reference file name is taken from repository format
File reference = new File(new File(referenceDir, a.getGroupId()), getRepositoryFilename(a));
if ((actual == null) || (reference == null)) {
return "missing file for " + a.getId() + " reference = "
+ (reference == null ? "null" : relative(reference)) + " actual = "
+ (actual == null ? "null" : relative(actual));
if (actual == null) {
return "missing file for " + a.getId() + " reference = " + relative(reference) + " actual = null";
}
return "diffoscope " + relative(reference) + " " + relative(actual);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static String getJavaVersion(Toolchain toolchain) {
LineConsumer err = new LineConsumer();
CommandLineUtils.executeCommandLine(cl, out, err);
version = StringUtils.join(err.getLines().iterator(), ":");
if (version == null) {
if (StringUtils.isEmpty(version)) {
version = "unable to detect...";
}
} catch (CommandLineException cle) {
Expand All @@ -54,7 +54,7 @@ static String getJavaVersion(Toolchain toolchain) {
}

private static class LineConsumer implements StreamConsumer {
private List<String> lines = new ArrayList<>();
private final List<String> lines = new ArrayList<>();

@Override
public void consumeLine(String line) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private static boolean isSkip(MavenProject project, String id) {
if (skip == null) {
skip = project.getProperties().getProperty("maven." + id + ".skip");
}
return Boolean.valueOf(skip);
return Boolean.parseBoolean(skip);
}

private static Plugin getPlugin(MavenProject project, String plugin) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -122,89 +122,87 @@ File downloadOrCreateReferenceBuildinfo(
referenceBuildinfo = null;
}

if (referenceBuildinfo == null) {
// download reference artifacts and guess Java version and OS
String javaVersion = null;
String osName = null;
String currentJavaVersion = null;
String currentOsName = null;
Map<Artifact, File> referenceArtifacts = new HashMap<>();
for (Artifact artifact : artifacts.keySet()) {
try {
// download
File file = downloadReference(repo, artifact);
referenceArtifacts.put(artifact, file);

// guess Java version and OS
if ((javaVersion == null) && JAR_TYPES.contains(artifact.getType())) {
ReproducibleEnv env = extractEnv(file, artifact);
if ((env != null) && (env.javaVersion != null)) {
javaVersion = env.javaVersion;
osName = env.osName;

ReproducibleEnv currentEnv = extractEnv(artifact.getFile(), artifact);
currentJavaVersion = currentEnv.javaVersion;
currentOsName = currentEnv.osName;
}
// download reference artifacts and guess Java version and OS
String javaVersion = null;
String osName = null;
String currentJavaVersion = null;
String currentOsName = null;
Map<Artifact, File> referenceArtifacts = new HashMap<>();
for (Artifact artifact : artifacts.keySet()) {
try {
// download
File file = downloadReference(repo, artifact);
referenceArtifacts.put(artifact, file);

// guess Java version and OS
if ((javaVersion == null) && JAR_TYPES.contains(artifact.getType())) {
ReproducibleEnv env = extractEnv(file, artifact);
if ((env != null) && (env.javaVersion != null)) {
javaVersion = env.javaVersion;
osName = env.osName;

ReproducibleEnv currentEnv = extractEnv(artifact.getFile(), artifact);
currentJavaVersion = currentEnv.javaVersion;
currentOsName = currentEnv.osName;
}
} catch (ArtifactNotFoundException e) {
log.warn("Reference artifact not found " + artifact);
}
} catch (ArtifactNotFoundException e) {
log.warn("Reference artifact not found " + artifact);
}
}

// generate buildinfo from reference artifacts
referenceBuildinfo = getReference(null, buildinfoFile);
try (PrintWriter p = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(referenceBuildinfo), StandardCharsets.UTF_8)))) {
BuildInfoWriter bi = new BuildInfoWriter(log, p, mono, artifactHandlerManager, rtInformation);

if (javaVersion != null || osName != null) {
p.println("# effective build environment information");
if (javaVersion != null) {
p.println("java.version=" + javaVersion);
log.info("Reference build java.version: " + javaVersion);
if (!javaVersion.equals(currentJavaVersion)) {
log.error("Current build java.version: " + currentJavaVersion);
}
}
if (osName != null) {
p.println("os.name=" + osName);
log.info("Reference build os.name: " + osName);

// check against current line separator
if (!osName.equals(currentOsName)) {
log.error("Current build os.name: " + currentOsName);
}
String expectedLs = osName.startsWith("Windows") ? "\r\n" : "\n";
if (!expectedLs.equals(System.lineSeparator())) {
log.warn("Current System.lineSeparator() does not match reference build OS");

String ls = System.getProperty("line.separator");
if (!ls.equals(System.lineSeparator())) {
log.warn("System.lineSeparator() != System.getProperty( \"line.separator\" ): "
+ "too late standard system property update...");
}
}
// generate buildinfo from reference artifacts
referenceBuildinfo = getReference(null, buildinfoFile);
try (PrintWriter p = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(Files.newOutputStream(referenceBuildinfo.toPath()), StandardCharsets.UTF_8)))) {
BuildInfoWriter bi = new BuildInfoWriter(log, p, mono, artifactHandlerManager, rtInformation);

if (javaVersion != null || osName != null) {
p.println("# effective build environment information");
if (javaVersion != null) {
p.println("java.version=" + javaVersion);
log.info("Reference build java.version: " + javaVersion);
if (!javaVersion.equals(currentJavaVersion)) {
log.error("Current build java.version: " + currentJavaVersion);
}
}
if (osName != null) {
p.println("os.name=" + osName);
log.info("Reference build os.name: " + osName);

for (Map.Entry<Artifact, String> entry : artifacts.entrySet()) {
Artifact artifact = entry.getKey();
String prefix = entry.getValue();
File referenceFile = referenceArtifacts.get(artifact);
if (referenceFile != null) {
bi.printFile(prefix, artifact.getGroupId(), referenceFile);
// check against current line separator
if (!osName.equals(currentOsName)) {
log.error("Current build os.name: " + currentOsName);
}
String expectedLs = osName.startsWith("Windows") ? "\r\n" : "\n";
if (!expectedLs.equals(System.lineSeparator())) {
log.warn("Current System.lineSeparator() does not match reference build OS");

String ls = System.getProperty("line.separator");
if (!ls.equals(System.lineSeparator())) {
log.warn("System.lineSeparator() != System.getProperty( \"line.separator\" ): "
+ "too late standard system property update...");
}
}
}
}

if (p.checkError()) {
throw new MojoExecutionException("Write error to " + referenceBuildinfo);
for (Map.Entry<Artifact, String> entry : artifacts.entrySet()) {
Artifact artifact = entry.getKey();
String prefix = entry.getValue();
File referenceFile = referenceArtifacts.get(artifact);
if (referenceFile != null) {
bi.printFile(prefix, artifact.getGroupId(), referenceFile);
}
}

log.info("Minimal buildinfo generated from downloaded artifacts: " + referenceBuildinfo);
} catch (IOException e) {
throw new MojoExecutionException("Error creating file " + referenceBuildinfo, e);
if (p.checkError()) {
throw new MojoExecutionException("Write error to " + referenceBuildinfo);
}

log.info("Minimal buildinfo generated from downloaded artifacts: " + referenceBuildinfo);
} catch (IOException e) {
throw new MojoExecutionException("Error creating file " + referenceBuildinfo, e);
}

return referenceBuildinfo;
Expand Down

0 comments on commit 0acc794

Please sign in to comment.