Skip to content

Commit

Permalink
Merge pull request #250 from basil/improvements
Browse files Browse the repository at this point in the history
Miscellaneous code cleanup
  • Loading branch information
basil authored Nov 26, 2021
2 parents d06d7af + 8253d3f commit bcc4f2c
Show file tree
Hide file tree
Showing 22 changed files with 91 additions and 266 deletions.
4 changes: 1 addition & 3 deletions src/main/java/hudson/Extension.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@

import jenkins.YesNoMaybe;

import static jenkins.YesNoMaybe.MAYBE;

/**
* Minimal clone to convince Sezpoz.
*
* @author Kohsuke Kawaguchi
*/
public @interface Extension {
YesNoMaybe dynamicLoadable() default MAYBE;
YesNoMaybe dynamicLoadable() default YesNoMaybe.MAYBE;
}
37 changes: 12 additions & 25 deletions src/main/java/org/jenkinsci/maven/plugins/hpi/AbstractHpiMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
Expand Down Expand Up @@ -279,7 +279,7 @@ public void setContainerConfigXML(File containerConfigXML) {
* @return an array of tokens to exclude
*/
protected String[] getExcludes() {
List<String> excludeList = new ArrayList<String>();
List<String> excludeList = new ArrayList<>();
if (StringUtils.isNotEmpty(warSourceExcludes)) {
excludeList.addAll(Arrays.asList(StringUtils.split(warSourceExcludes, ",")));
}
Expand All @@ -299,7 +299,7 @@ protected String[] getExcludes() {
* @return an array of tokens to include
*/
protected String[] getIncludes() {
return StringUtils.split(StringUtils.defaultString(warSourceIncludes), ",");
return StringUtils.split(Objects.toString(warSourceIncludes), ",");
}

/**
Expand All @@ -325,7 +325,7 @@ protected String[] getDependentWarExcludes() {
* @return an array of tokens to include
*/
protected String[] getDependentWarIncludes() {
return StringUtils.split(StringUtils.defaultString(dependentWarIncludes), ",");
return StringUtils.split(Objects.toString(dependentWarIncludes), ",");
}

public void buildExplodedWebapp(File webappDirectory, File jarFile)
Expand Down Expand Up @@ -397,7 +397,6 @@ private Properties getBuildFilterProperties()
*
* @param resource the resource to copy
* @param webappDirectory the target directory
* @param filterProperties
* @throws java.io.IOException if an error occurred while copying webResources
*/
public void copyResources(Resource resource, File webappDirectory, Properties filterProperties)
Expand Down Expand Up @@ -479,7 +478,6 @@ protected Set<MavenArtifact> wrap(Iterable<Artifact> artifacts) {
* the {@code webappDirectory} during this phase.
*
* @param project the maven project
* @param webappDirectory
* @throws java.io.IOException if an error occurred while building the webapp
*/
public void buildWebapp(MavenProject project, File webappDirectory)
Expand All @@ -497,7 +495,7 @@ public void buildWebapp(MavenProject project, File webappDirectory)

List<String> duplicates = findDuplicates(artifacts);

List<File> dependentWarDirectories = new ArrayList<File>();
List<File> dependentWarDirectories = new ArrayList<>();

// List up IDs of Jenkins plugin dependencies
Set<String> jenkinsPlugins = new HashSet<>();
Expand Down Expand Up @@ -582,8 +580,8 @@ public void buildWebapp(MavenProject project, File webappDirectory)
getLog().info("Overlaying " + dependentWarDirectories.size() + " war(s).");

// overlay dependent wars
for (Iterator iter = dependentWarDirectories.iterator(); iter.hasNext();) {
copyDependentWarContents((File) iter.next(), webappDirectory);
for (File dependentWarDirectory : dependentWarDirectories) {
copyDependentWarContents(dependentWarDirectory, webappDirectory);
}
}
}
Expand All @@ -595,8 +593,8 @@ public void buildWebapp(MavenProject project, File webappDirectory)
* @return List of duplicated artifacts
*/
private List<String> findDuplicates(Set<MavenArtifact> artifacts) {
List<String> duplicates = new ArrayList<String>();
List<String> identifiers = new ArrayList<String>();
List<String> duplicates = new ArrayList<>();
List<String> identifiers = new ArrayList<>();
for (MavenArtifact artifact : artifacts) {
String candidate = artifact.getDefaultFinalName();
if (identifiers.contains(candidate)) {
Expand All @@ -614,7 +612,6 @@ private List<String> findDuplicates(Set<MavenArtifact> artifacts) {
*
* @param artifact War artifact to unpack.
* @return Directory containing the unpacked war.
* @throws MojoExecutionException
*/
private File unpackWarToTempDirectory(MavenArtifact artifact)
throws MojoExecutionException {
Expand Down Expand Up @@ -733,12 +730,12 @@ private String[] getWarFiles(Resource resource) {
DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir(resource.getDirectory());
if (resource.getIncludes() != null && !resource.getIncludes().isEmpty()) {
scanner.setIncludes((String[]) resource.getIncludes().toArray(EMPTY_STRING_ARRAY));
scanner.setIncludes(resource.getIncludes().toArray(EMPTY_STRING_ARRAY));
} else {
scanner.setIncludes(DEFAULT_INCLUDES);
}
if (resource.getExcludes() != null && !resource.getExcludes().isEmpty()) {
scanner.setExcludes((String[]) resource.getExcludes().toArray(EMPTY_STRING_ARRAY));
scanner.setExcludes(resource.getExcludes().toArray(EMPTY_STRING_ARRAY));
}

scanner.addDefaultExcludes();
Expand Down Expand Up @@ -791,11 +788,6 @@ public Reader getReader(Reader fileReader, Properties filterProperties) {
}

/**
* @param from
* @param to
* @param encoding
* @param wrappers
* @param filterProperties
* @throws IOException TO DO: Remove this method when Maven moves to plexus-utils version 1.4
*/
private static void copyFilteredFile(File from, File to, String encoding, FilterWrapper[] wrappers,
Expand Down Expand Up @@ -866,8 +858,6 @@ private static void copyFileIfModified(File source, File destination)
* <li>The <code>sourceDirectory</code> must exists.
* </ul>
*
* @param sourceDirectory
* @param destinationDirectory
* @throws IOException TO DO: Remove this method when Maven moves to plexus-utils version 1.4
*/
private static void copyDirectoryStructureIfModified(File sourceDirectory, File destinationDirectory)
Expand Down Expand Up @@ -925,10 +915,7 @@ public String getGitHeadSha1() {
return null; // git rev-parse failed to run

return v.trim().substring(0,8);
} catch (IOException e) {
LOGGER.log(Level.FINE, "Failed to run git rev-parse HEAD",e);
return null;
} catch (InterruptedException e) {
} catch (IOException | InterruptedException e) {
LOGGER.log(Level.FINE, "Failed to run git rev-parse HEAD",e);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.plexus.archiver.jar.Manifest;
import org.codehaus.plexus.archiver.jar.ManifestException;
import org.codehaus.plexus.util.IOUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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.Collection;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
Expand Down Expand Up @@ -88,29 +89,21 @@ protected void generateManifest(MavenArchiveConfiguration archive, File manifest
MavenArchiver ma = new MavenArchiver();
ma.setOutputFile(manifestFile);

PrintWriter printWriter = null;
try {
try (PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(manifestFile), StandardCharsets.UTF_8))) {
Manifest mf = ma.getManifest(project, archive.getManifest());
Manifest.ExistingSection mainSection = mf.getMainSection();
setAttributes(mainSection);

printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(manifestFile), "UTF-8"));
mf.write(printWriter);
} catch (ManifestException e) {
throw new MojoExecutionException("Error preparing the manifest: " + e.getMessage(), e);
} catch (DependencyResolutionRequiredException e) {
} catch (ManifestException | IOException | DependencyResolutionRequiredException e) {
throw new MojoExecutionException("Error preparing the manifest: " + e.getMessage(), e);
} catch (IOException e) {
throw new MojoExecutionException("Error preparing the manifest: " + e.getMessage(), e);
} finally {
IOUtil.close(printWriter);
}
}

protected void setAttributes(Manifest.ExistingSection mainSection) throws MojoExecutionException, ManifestException, IOException {
File pluginImpl = new File(project.getBuild().getOutputDirectory(), "META-INF/services/hudson.Plugin");
if(pluginImpl.exists()) {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(pluginImpl),"UTF-8"));
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(pluginImpl), StandardCharsets.UTF_8));
String pluginClassName = in.readLine();
in.close();

Expand Down Expand Up @@ -238,7 +231,7 @@ private String findDependencyPlugins() throws IOException, MojoExecutionExceptio

// check any "provided" scope plugin dependencies that are probably not what the user intended.
// see http://jenkins-ci.361315.n4.nabble.com/Classloading-problem-when-referencing-classes-from-another-plugin-during-the-initialization-phase-of-td394967.html
for (Artifact a : (Collection<Artifact>)project.getDependencyArtifacts())
for (Artifact a : project.getDependencyArtifacts())
if ("provided".equals(a.getScope()) && wrap(a).isPlugin())
throw new MojoExecutionException(a.getId()+" is marked as 'provided' scope dependency, but it should be the 'compile' scope.");

Expand All @@ -252,8 +245,7 @@ private String findDependencyPlugins() throws IOException, MojoExecutionExceptio
private String getDevelopersForManifest() throws IOException {
StringBuilder buf = new StringBuilder();

for (Object o : project.getDevelopers()) {
Developer d = (Developer) o;
for (Developer d : project.getDevelopers()) {
if (buf.length() > 0) {
buf.append(',');
}
Expand All @@ -268,11 +260,8 @@ private String getDevelopersForManifest() throws IOException {
}

protected Manifest loadManifest(File f) throws IOException, ManifestException {
InputStreamReader r = new InputStreamReader(new FileInputStream(f), "UTF-8");
try {
return new Manifest(r);
} finally {
IOUtil.close(r);
try (InputStream is = Files.newInputStream(f.toPath())) {
return new Manifest(is);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public abstract class AbstractJenkinsMojo extends AbstractMojo {


protected String findJenkinsVersion() throws MojoExecutionException {
for(Dependency a : (List<Dependency>)project.getDependencies()) {
for(Dependency a : project.getDependencies()) {
boolean match;
if (jenkinsCoreId!=null)
match = (a.getGroupId()+':'+a.getArtifactId()).equals(jenkinsCoreId);
Expand Down
Loading

0 comments on commit bcc4f2c

Please sign in to comment.