Skip to content

Improve performance in multimodule builds #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -33,10 +32,8 @@
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import java.util.zip.ZipFile;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.MojoExecutionException;

public class ClasspathDescriptor
Expand Down Expand Up @@ -192,16 +189,14 @@ private void addArchive(File element) throws IOException

List classes = new ArrayList();
List resources = new ArrayList();
InputStream input = null;
ZipInputStream zipInput = null;
ZipFile zipFile = null;

try {
input = element.toURI().toURL().openStream();
zipInput = new ZipInputStream(input);

ZipEntry entry;
zipFile = new ZipFile(element);
Enumeration entries = zipFile.entries();

while ((entry = zipInput.getNextEntry()) != null) {
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
if (!entry.isDirectory()) {
String name = entry.getName();

Expand All @@ -223,12 +218,13 @@ private void addArchive(File element) throws IOException
CACHED_BY_ELEMENT.put(element, new Cached(classes, resources));
}
finally {
if (zipInput != null) {
// this will also close the wrapped stream
IOUtils.closeQuietly(zipInput);
}
else if (input != null) {
IOUtils.closeQuietly(input);
// IOUtils has no closeQuietly for Closable in the 1.x series.
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException ex) {
// ignored
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public class DuplicateFinderMojo extends AbstractMojo
{
protected final Logger LOG = LoggerFactory.getLogger(this.getClass());

private static Map CACHED_SHA256 = new HashMap();

// the constants for conflicts
private final static int NO_CONFLICT = 0;
private final static int CONFLICT_CONTENT_EQUAL = 1;
Expand Down Expand Up @@ -423,6 +425,34 @@ else if(!newSHA256.equals(firstSHA256)) {
*/
private String getSHA256HexOfElement(final File file, final String resourcePath) throws IOException
{
class Sha256CacheKey {
final File file;
final String resourcePath;

Sha256CacheKey(File file, String resourcePath) {
this.file = file;
this.resourcePath = resourcePath;
}

public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Sha256CacheKey key = (Sha256CacheKey) o;

return file.equals(key.file) && resourcePath.equals(key.resourcePath);
}

public int hashCode() {
return 31 * file.hashCode() + resourcePath.hashCode();
}
}

final Sha256CacheKey key = new Sha256CacheKey(file, resourcePath);
if (CACHED_SHA256.containsKey(key)) {
return (String) CACHED_SHA256.get(key);
}

ZipFile zip = new ZipFile(file);
ZipEntry zipEntry = zip.getEntry(resourcePath);
if(zipEntry == null)
Expand All @@ -439,6 +469,8 @@ private String getSHA256HexOfElement(final File file, final String resourcePath)
} finally {
IOUtils.closeQuietly(in);
}

CACHED_SHA256.put(key, sha256);

return sha256;
}
Expand Down