Skip to content

Commit

Permalink
Merge pull request #296 from basil/RV_RETURN_VALUE_IGNORED_BAD_PRACTICE
Browse files Browse the repository at this point in the history
Fix `RV_RETURN_VALUE_IGNORED_BAD_PRACTICE` SpotBugs violations
  • Loading branch information
jglick committed Feb 22, 2022
2 parents fa4c6d8 + 00d6ea2 commit 433cb4d
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 30 deletions.
43 changes: 34 additions & 9 deletions src/main/java/org/jenkinsci/maven/plugins/hpi/AbstractHpiMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,25 @@ public void buildExplodedWebapp(File webappDirectory, File jarFile)
throws MojoExecutionException {
getLog().info("Exploding webapp...");

webappDirectory.mkdirs();
try {
Files.createDirectories(webappDirectory.toPath());
} catch (IOException e) {
throw new MojoExecutionException("Failed to create directories for '" + webappDirectory + "'", e);
}

File webinfDir = new File(webappDirectory, WEB_INF);
webinfDir.mkdirs();
try {
Files.createDirectories(webinfDir.toPath());
} catch (IOException e) {
throw new MojoExecutionException("Failed to create directories for '" + webinfDir + "'", e);
}

File metainfDir = new File(webappDirectory, META_INF);
metainfDir.mkdirs();
try {
Files.createDirectories(metainfDir.toPath());
} catch (IOException e) {
throw new MojoExecutionException("Failed to create directories for '" + metainfDir + "'", e);
}

try {
List<Resource> webResources = this.webResources != null ? Arrays.asList(this.webResources) : null;
Expand Down Expand Up @@ -621,8 +633,12 @@ private File unpackWarToTempDirectory(MavenArtifact artifact)
File tempLocation = new File(workDirectory, name.substring(0, name.length() - 4));

boolean process = false;
if (!tempLocation.exists()) {
tempLocation.mkdirs();
if (!Files.isDirectory(tempLocation.toPath())) {
try {
Files.createDirectories(tempLocation.toPath());
} catch (IOException e) {
throw new MojoExecutionException("Failed to create directories for '" + tempLocation + "'", e);
}
process = true;
} else if (artifact.getFile().lastModified() > tempLocation.lastModified()) {
process = true;
Expand Down Expand Up @@ -681,16 +697,25 @@ private void copyDependentWarContents(File srcDir, File targetDir)
scanner.scan();

for (String dir : scanner.getIncludedDirectories()) {
new File(targetDir, dir).mkdirs();
File includeDir = new File(targetDir, dir);
try {
Files.createDirectories(includeDir.toPath());
} catch (IOException e) {
throw new MojoExecutionException("Failed to create directories for '" + includeDir + "'", e);
}
}

for (String file : scanner.getIncludedFiles()) {
File targetFile = new File(targetDir, file);

// Do not overwrite existing files.
if (!targetFile.exists()) {
if (!Files.exists(targetFile.toPath())) {
try {
Files.createDirectories(targetFile.toPath().getParent());
} catch (IOException e) {
throw new MojoExecutionException("Failed to create parent directories for '" + targetFile + "'", e);
}
try {
targetFile.getParentFile().mkdirs();
FileUtils.copyFileIfModified(new File(srcDir, file), targetFile);
}
catch (IOException e) {
Expand Down Expand Up @@ -776,7 +801,7 @@ private static void copyFilteredFile(File from, File to, String encoding, Filter
Writer fileWriter = null;
try {
// fix for MWAR-36, ensures that the parent dir are created first
to.getParentFile().mkdirs();
Files.createDirectories(to.toPath().getParent());

if (encoding == null || encoding.length() < 1) {
fileReader = Files.newBufferedReader(from.toPath(), StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,13 @@ public abstract class AbstractJenkinsManifestMojo extends AbstractHpiMojo {
*/
protected void generateManifest(MavenArchiveConfiguration archive, File manifestFile) throws MojoExecutionException {
// create directory if it doesn't exist yet
if (!manifestFile.getParentFile().exists())
manifestFile.getParentFile().mkdirs();
if (!Files.isDirectory(manifestFile.toPath().getParent())) {
try {
Files.createDirectories(manifestFile.toPath().getParent());
} catch (IOException e) {
throw new MojoExecutionException("Failed to create parent directories for '" + manifestFile + "'", e);
}
}

getLog().info("Generating " + manifestFile);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
Expand Down Expand Up @@ -591,8 +592,9 @@ public void configureWebApplication () throws Exception
{
File target = new File(project.getBuild().getDirectory());
File tmp = new File(target, "jetty");
if (!tmp.exists())
tmp.mkdirs();
if (!Files.isDirectory(tmp.toPath())) {
Files.createDirectories(tmp.toPath());
}
webApp.setTempDirectory(tmp);
}

Expand Down
14 changes: 10 additions & 4 deletions src/main/java/org/jenkinsci/maven/plugins/hpi/RunMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -361,7 +363,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
// set JENKINS_HOME
setSystemPropertyIfEmpty("JENKINS_HOME",jenkinsHome.getAbsolutePath());
File pluginsDir = new File(jenkinsHome, "plugins");
pluginsDir.mkdirs();
try {
Files.createDirectories(pluginsDir.toPath());
} catch (IOException e) {
throw new MojoExecutionException("Failed to create directories for '" + pluginsDir + "'", e);
}

// enable view auto refreshing via stapler
setSystemPropertyIfEmpty("stapler.jelly.noCache","true");
Expand Down Expand Up @@ -450,9 +456,9 @@ private void setSystemPropertyIfEmpty(String name, String value) {
private void copyPlugin(File src, File pluginsDir, String shortName) throws IOException {
File dst = new File(pluginsDir, shortName + ".jpi");
File hpi = new File(pluginsDir, shortName + ".hpi");
if (hpi.isFile()) {
if (Files.isRegularFile(hpi.toPath())) {
getLog().warn("Moving historical " + hpi + " to *.jpi");
hpi.renameTo(dst);
Files.move(hpi.toPath(), dst.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
VersionNumber dstV = versionOfPlugin(dst);
if (versionOfPlugin(src).compareTo(dstV) < 0) {
Expand All @@ -465,7 +471,7 @@ private void copyPlugin(File src, File pluginsDir, String shortName) throws IOEx
// pin the dependency plugin, so that even if a different version of the same plugin is bundled to Jenkins,
// we still use the plugin as specified by the POM of the plugin.
FileUtils.writeStringToFile(new File(dst + ".pinned"), "pinned");
new File(pluginsDir, shortName + ".jpl").delete(); // in case we used to have a snapshot dependency
Files.deleteIfExists(new File(pluginsDir, shortName + ".jpl").toPath()); // in case we used to have a snapshot dependency
}
private VersionNumber versionOfPlugin(File p) throws IOException {
if (!p.isFile()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.io.PrintWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -77,7 +78,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
walk(new File(res.getDirectory()),codeModel.rootPackage(),"");
}

outputDirectory.mkdirs();
Files.createDirectories(outputDirectory.toPath());
CodeWriter w = new FilterCodeWriter(encoding != null ? new FileCodeWriter(outputDirectory, encoding) : new FileCodeWriter(outputDirectory)) {
// Cf. ProgressCodeWriter:
@Override public Writer openSource(JPackage pkg, String fileName) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

/**
* Places test-dependency plugins into somewhere the test harness can pick up.
Expand All @@ -25,12 +26,14 @@
public class TestDependencyMojo extends AbstractHpiMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
File testDir = new File(project.getBuild().getTestOutputDirectory(),"test-dependencies");
testDir.mkdirs();

File testDir = new File(project.getBuild().getTestOutputDirectory(), "test-dependencies");
try {
Writer w = new OutputStreamWriter(new FileOutputStream(new File(testDir,"index")), StandardCharsets.UTF_8);
Files.createDirectories(testDir.toPath());
} catch (IOException e) {
throw new MojoExecutionException("Failed to create directories for '" + testDir + "'", e);
}

try (FileOutputStream fos = new FileOutputStream(new File(testDir, "index")); Writer w = new OutputStreamWriter(fos, StandardCharsets.UTF_8)) {
for (MavenArtifact a : getProjectArtfacts()) {
if (!a.isPluginBestEffort(getLog()))
continue;
Expand All @@ -46,8 +49,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
FileUtils.copyFile(a.getHpi().getFile(),dst);
w.write(artifactId + "\n");
}

w.close();
} catch (IOException e) {
throw new MojoExecutionException("Failed to copy dependency plugins",e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

/**
* Generate .hpl file in the test class directory so that test harness can locate the plugin.
Expand All @@ -25,7 +26,11 @@ public class TestHplMojo extends HplMojo {
@Override
protected File computeHplFile() throws MojoExecutionException {
File testDir = new File(project.getBuild().getTestOutputDirectory());
testDir.mkdirs();
try {
Files.createDirectories(testDir.toPath());
} catch (IOException e) {
throw new MojoExecutionException("Failed to create directories for '" + testDir + "'", e);
}
File theHpl = new File(testDir,"the.hpl");
if (project.getArtifact().isSnapshot()) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import hudson.util.VersionNumber;
import java.io.File;
import java.io.FileNotFoundException;
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.nio.file.attribute.FileTime;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
Expand Down Expand Up @@ -107,7 +109,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {

try {
File f = new File(project.getBasedir(), "target/generated-test-sources/injected");
f.mkdirs();
Files.createDirectories(f.toPath());
File javaFile = new File(f, injectedTestName + ".java");
PrintWriter w = new PrintWriter(new OutputStreamWriter(new FileOutputStream(javaFile), StandardCharsets.UTF_8));
w.println("import java.util.*;");
Expand Down Expand Up @@ -135,9 +137,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {

// always set the same time stamp on this file, so that Maven will not re-compile this
// every time we run this mojo.
javaFile.setLastModified(0);
} catch (FileNotFoundException e) {
e.printStackTrace();
Files.setLastModifiedTime(javaFile.toPath(), FileTime.fromMillis(0L));
} catch (IOException e) {
throw new MojoExecutionException("Failed to create injected tests", e);
}
}
}

0 comments on commit 433cb4d

Please sign in to comment.