Skip to content

Commit 0acc794

Browse files
committed
apply improvements proposed by IDE
1 parent ccc4206 commit 0acc794

File tree

6 files changed

+81
-85
lines changed

6 files changed

+81
-85
lines changed

src/main/java/org/apache/maven/plugins/artifact/buildinfo/AbstractBuildinfoMojo.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020

2121
import java.io.BufferedWriter;
2222
import java.io.File;
23-
import java.io.FileOutputStream;
2423
import java.io.IOException;
2524
import java.io.OutputStreamWriter;
2625
import java.io.PrintWriter;
2726
import java.nio.charset.StandardCharsets;
27+
import java.nio.file.Files;
2828
import java.text.SimpleDateFormat;
2929
import java.util.Date;
3030
import java.util.List;
@@ -224,7 +224,7 @@ protected Map<Artifact, String> generateBuildinfo(boolean mono) throws MojoExecu
224224
buildinfoFile.getParentFile().mkdirs();
225225

226226
try (PrintWriter p = new PrintWriter(new BufferedWriter(
227-
new OutputStreamWriter(new FileOutputStream(buildinfoFile), StandardCharsets.UTF_8)))) {
227+
new OutputStreamWriter(Files.newOutputStream(buildinfoFile.toPath()), StandardCharsets.UTF_8)))) {
228228
BuildInfoWriter bi = new BuildInfoWriter(getLog(), p, mono, artifactHandlerManager, rtInformation);
229229
bi.setIgnoreJavadoc(ignoreJavadoc);
230230
bi.setIgnore(ignore);

src/main/java/org/apache/maven/plugins/artifact/buildinfo/CheckBuildPlanMojo.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
package org.apache.maven.plugins.artifact.buildinfo;
2020

2121
import java.io.File;
22-
import java.io.FileInputStream;
2322
import java.io.IOException;
2423
import java.io.InputStream;
24+
import java.nio.file.Files;
2525
import java.text.SimpleDateFormat;
2626
import java.util.Date;
2727
import java.util.HashSet;
@@ -194,7 +194,7 @@ private boolean hasBadOutputTimestamp() {
194194
private Properties loadIssues() throws MojoExecutionException {
195195
try (InputStream in = (pluginIssues == null)
196196
? getClass().getResourceAsStream("not-reproducible-plugins.properties")
197-
: new FileInputStream(pluginIssues)) {
197+
: Files.newInputStream(pluginIssues.toPath())) {
198198
Properties prop = new Properties();
199199
prop.load(in);
200200

src/main/java/org/apache/maven/plugins/artifact/buildinfo/CompareMojo.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020

2121
import java.io.BufferedWriter;
2222
import java.io.File;
23-
import java.io.FileOutputStream;
2423
import java.io.IOException;
2524
import java.io.OutputStreamWriter;
2625
import java.io.PrintWriter;
2726
import java.nio.charset.StandardCharsets;
27+
import java.nio.file.Files;
2828
import java.util.ArrayList;
2929
import java.util.List;
3030
import java.util.Map;
@@ -128,7 +128,7 @@ protected void skip(MavenProject last) throws MojoExecutionException {
128128
*
129129
* @param artifacts a Map of artifacts added to the build info with their associated property key prefix
130130
* (<code>outputs.[#module.].#artifact</code>)
131-
* @throws MojoExecutionException
131+
* @throws MojoExecutionException if anything goes wrong
132132
*/
133133
private void checkAgainstReference(Map<Artifact, String> artifacts, boolean mono) throws MojoExecutionException {
134134
MavenProject root = mono ? project : getExecutionRoot();
@@ -216,7 +216,7 @@ private void compareWithReference(Map<Artifact, String> artifacts, File referenc
216216
File buildcompare = new File(
217217
buildinfoFile.getParentFile(), buildinfoFile.getName().replaceFirst(".buildinfo$", ".buildcompare"));
218218
try (PrintWriter p = new PrintWriter(new BufferedWriter(
219-
new OutputStreamWriter(new FileOutputStream(buildcompare), StandardCharsets.UTF_8)))) {
219+
new OutputStreamWriter(Files.newOutputStream(buildcompare.toPath()), StandardCharsets.UTF_8)))) {
220220
p.println("version=" + project.getVersion());
221221
p.println("ok=" + ok);
222222
p.println("ko=" + ko);
@@ -282,10 +282,8 @@ private String diffoscope(Artifact a, File referenceDir) {
282282
// notice: actual file name may have been defined in pom
283283
// reference file name is taken from repository format
284284
File reference = new File(new File(referenceDir, a.getGroupId()), getRepositoryFilename(a));
285-
if ((actual == null) || (reference == null)) {
286-
return "missing file for " + a.getId() + " reference = "
287-
+ (reference == null ? "null" : relative(reference)) + " actual = "
288-
+ (actual == null ? "null" : relative(actual));
285+
if (actual == null) {
286+
return "missing file for " + a.getId() + " reference = " + relative(reference) + " actual = null";
289287
}
290288
return "diffoscope " + relative(reference) + " " + relative(actual);
291289
}

src/main/java/org/apache/maven/plugins/artifact/buildinfo/JdkToolchainUtil.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static String getJavaVersion(Toolchain toolchain) {
4343
LineConsumer err = new LineConsumer();
4444
CommandLineUtils.executeCommandLine(cl, out, err);
4545
version = StringUtils.join(err.getLines().iterator(), ":");
46-
if (version == null) {
46+
if (StringUtils.isEmpty(version)) {
4747
version = "unable to detect...";
4848
}
4949
} catch (CommandLineException cle) {
@@ -54,7 +54,7 @@ static String getJavaVersion(Toolchain toolchain) {
5454
}
5555

5656
private static class LineConsumer implements StreamConsumer {
57-
private List<String> lines = new ArrayList<>();
57+
private final List<String> lines = new ArrayList<>();
5858

5959
@Override
6060
public void consumeLine(String line) throws IOException {

src/main/java/org/apache/maven/plugins/artifact/buildinfo/PluginUtil.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private static boolean isSkip(MavenProject project, String id) {
3838
if (skip == null) {
3939
skip = project.getProperties().getProperty("maven." + id + ".skip");
4040
}
41-
return Boolean.valueOf(skip);
41+
return Boolean.parseBoolean(skip);
4242
}
4343

4444
private static Plugin getPlugin(MavenProject project, String plugin) {

src/main/java/org/apache/maven/plugins/artifact/buildinfo/ReferenceBuildinfoUtil.java

+69-71
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020

2121
import java.io.BufferedWriter;
2222
import java.io.File;
23-
import java.io.FileOutputStream;
2423
import java.io.IOException;
2524
import java.io.InputStream;
2625
import java.io.OutputStreamWriter;
2726
import java.io.PrintWriter;
2827
import java.nio.charset.StandardCharsets;
28+
import java.nio.file.Files;
2929
import java.util.Collections;
3030
import java.util.HashMap;
3131
import java.util.HashSet;
@@ -122,89 +122,87 @@ File downloadOrCreateReferenceBuildinfo(
122122
referenceBuildinfo = null;
123123
}
124124

125-
if (referenceBuildinfo == null) {
126-
// download reference artifacts and guess Java version and OS
127-
String javaVersion = null;
128-
String osName = null;
129-
String currentJavaVersion = null;
130-
String currentOsName = null;
131-
Map<Artifact, File> referenceArtifacts = new HashMap<>();
132-
for (Artifact artifact : artifacts.keySet()) {
133-
try {
134-
// download
135-
File file = downloadReference(repo, artifact);
136-
referenceArtifacts.put(artifact, file);
137-
138-
// guess Java version and OS
139-
if ((javaVersion == null) && JAR_TYPES.contains(artifact.getType())) {
140-
ReproducibleEnv env = extractEnv(file, artifact);
141-
if ((env != null) && (env.javaVersion != null)) {
142-
javaVersion = env.javaVersion;
143-
osName = env.osName;
144-
145-
ReproducibleEnv currentEnv = extractEnv(artifact.getFile(), artifact);
146-
currentJavaVersion = currentEnv.javaVersion;
147-
currentOsName = currentEnv.osName;
148-
}
125+
// download reference artifacts and guess Java version and OS
126+
String javaVersion = null;
127+
String osName = null;
128+
String currentJavaVersion = null;
129+
String currentOsName = null;
130+
Map<Artifact, File> referenceArtifacts = new HashMap<>();
131+
for (Artifact artifact : artifacts.keySet()) {
132+
try {
133+
// download
134+
File file = downloadReference(repo, artifact);
135+
referenceArtifacts.put(artifact, file);
136+
137+
// guess Java version and OS
138+
if ((javaVersion == null) && JAR_TYPES.contains(artifact.getType())) {
139+
ReproducibleEnv env = extractEnv(file, artifact);
140+
if ((env != null) && (env.javaVersion != null)) {
141+
javaVersion = env.javaVersion;
142+
osName = env.osName;
143+
144+
ReproducibleEnv currentEnv = extractEnv(artifact.getFile(), artifact);
145+
currentJavaVersion = currentEnv.javaVersion;
146+
currentOsName = currentEnv.osName;
149147
}
150-
} catch (ArtifactNotFoundException e) {
151-
log.warn("Reference artifact not found " + artifact);
152148
}
149+
} catch (ArtifactNotFoundException e) {
150+
log.warn("Reference artifact not found " + artifact);
153151
}
152+
}
154153

155-
// generate buildinfo from reference artifacts
156-
referenceBuildinfo = getReference(null, buildinfoFile);
157-
try (PrintWriter p = new PrintWriter(new BufferedWriter(
158-
new OutputStreamWriter(new FileOutputStream(referenceBuildinfo), StandardCharsets.UTF_8)))) {
159-
BuildInfoWriter bi = new BuildInfoWriter(log, p, mono, artifactHandlerManager, rtInformation);
160-
161-
if (javaVersion != null || osName != null) {
162-
p.println("# effective build environment information");
163-
if (javaVersion != null) {
164-
p.println("java.version=" + javaVersion);
165-
log.info("Reference build java.version: " + javaVersion);
166-
if (!javaVersion.equals(currentJavaVersion)) {
167-
log.error("Current build java.version: " + currentJavaVersion);
168-
}
169-
}
170-
if (osName != null) {
171-
p.println("os.name=" + osName);
172-
log.info("Reference build os.name: " + osName);
173-
174-
// check against current line separator
175-
if (!osName.equals(currentOsName)) {
176-
log.error("Current build os.name: " + currentOsName);
177-
}
178-
String expectedLs = osName.startsWith("Windows") ? "\r\n" : "\n";
179-
if (!expectedLs.equals(System.lineSeparator())) {
180-
log.warn("Current System.lineSeparator() does not match reference build OS");
181-
182-
String ls = System.getProperty("line.separator");
183-
if (!ls.equals(System.lineSeparator())) {
184-
log.warn("System.lineSeparator() != System.getProperty( \"line.separator\" ): "
185-
+ "too late standard system property update...");
186-
}
187-
}
154+
// generate buildinfo from reference artifacts
155+
referenceBuildinfo = getReference(null, buildinfoFile);
156+
try (PrintWriter p = new PrintWriter(new BufferedWriter(
157+
new OutputStreamWriter(Files.newOutputStream(referenceBuildinfo.toPath()), StandardCharsets.UTF_8)))) {
158+
BuildInfoWriter bi = new BuildInfoWriter(log, p, mono, artifactHandlerManager, rtInformation);
159+
160+
if (javaVersion != null || osName != null) {
161+
p.println("# effective build environment information");
162+
if (javaVersion != null) {
163+
p.println("java.version=" + javaVersion);
164+
log.info("Reference build java.version: " + javaVersion);
165+
if (!javaVersion.equals(currentJavaVersion)) {
166+
log.error("Current build java.version: " + currentJavaVersion);
188167
}
189168
}
169+
if (osName != null) {
170+
p.println("os.name=" + osName);
171+
log.info("Reference build os.name: " + osName);
190172

191-
for (Map.Entry<Artifact, String> entry : artifacts.entrySet()) {
192-
Artifact artifact = entry.getKey();
193-
String prefix = entry.getValue();
194-
File referenceFile = referenceArtifacts.get(artifact);
195-
if (referenceFile != null) {
196-
bi.printFile(prefix, artifact.getGroupId(), referenceFile);
173+
// check against current line separator
174+
if (!osName.equals(currentOsName)) {
175+
log.error("Current build os.name: " + currentOsName);
176+
}
177+
String expectedLs = osName.startsWith("Windows") ? "\r\n" : "\n";
178+
if (!expectedLs.equals(System.lineSeparator())) {
179+
log.warn("Current System.lineSeparator() does not match reference build OS");
180+
181+
String ls = System.getProperty("line.separator");
182+
if (!ls.equals(System.lineSeparator())) {
183+
log.warn("System.lineSeparator() != System.getProperty( \"line.separator\" ): "
184+
+ "too late standard system property update...");
185+
}
197186
}
198187
}
188+
}
199189

200-
if (p.checkError()) {
201-
throw new MojoExecutionException("Write error to " + referenceBuildinfo);
190+
for (Map.Entry<Artifact, String> entry : artifacts.entrySet()) {
191+
Artifact artifact = entry.getKey();
192+
String prefix = entry.getValue();
193+
File referenceFile = referenceArtifacts.get(artifact);
194+
if (referenceFile != null) {
195+
bi.printFile(prefix, artifact.getGroupId(), referenceFile);
202196
}
197+
}
203198

204-
log.info("Minimal buildinfo generated from downloaded artifacts: " + referenceBuildinfo);
205-
} catch (IOException e) {
206-
throw new MojoExecutionException("Error creating file " + referenceBuildinfo, e);
199+
if (p.checkError()) {
200+
throw new MojoExecutionException("Write error to " + referenceBuildinfo);
207201
}
202+
203+
log.info("Minimal buildinfo generated from downloaded artifacts: " + referenceBuildinfo);
204+
} catch (IOException e) {
205+
throw new MojoExecutionException("Error creating file " + referenceBuildinfo, e);
208206
}
209207

210208
return referenceBuildinfo;

0 commit comments

Comments
 (0)