diff --git a/src/main/java/net/flintmc/gradle/maven/MavenArtifactDownloader.java b/src/main/java/net/flintmc/gradle/maven/MavenArtifactDownloader.java index 1c9331c..db47c46 100644 --- a/src/main/java/net/flintmc/gradle/maven/MavenArtifactDownloader.java +++ b/src/main/java/net/flintmc/gradle/maven/MavenArtifactDownloader.java @@ -34,6 +34,7 @@ import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -59,10 +60,8 @@ public MavenArtifactDownloader() { * * @param source The repository to add */ - public void addSource(ReadableMavenRepository source) { - synchronized (this.sources) { - this.sources.add(source); - } + public synchronized void addSource(ReadableMavenRepository source) { + this.sources.add(source); } /** @@ -71,10 +70,8 @@ public void addSource(ReadableMavenRepository source) { * @param source The repository to check if it exists as a source * @return {@code true} if this downloader has the give repository as a source, {@code false} otherwise */ - public boolean hasSource(ReadableMavenRepository source) { - synchronized (this.sources) { - return this.sources.contains(source); - } + public synchronized boolean hasSource(ReadableMavenRepository source) { + return this.sources.contains(source); } /** @@ -82,10 +79,8 @@ public boolean hasSource(ReadableMavenRepository source) { * * @param source The repository to remove */ - public void removeSource(ReadableMavenRepository source) { - synchronized (this.sources) { - this.sources.remove(source); - } + public synchronized void removeSource(ReadableMavenRepository source) { + this.sources.remove(source); } /** @@ -97,7 +92,7 @@ public void removeSource(ReadableMavenRepository source) { * @throws IOException If an I/O error occurs while installing the artifact or one if its dependencies * @throws MavenResolveException If the artifact or one of its dependencies can't be resolved */ - public void installAll(MavenArtifact artifact, SimpleMavenRepository target, boolean installIfNotExists) + public synchronized void installAll(MavenArtifact artifact, SimpleMavenRepository target, boolean installIfNotExists) throws IOException, MavenResolveException { // Get the local POM path Path localPomPath = target.getPomPath(artifact); @@ -218,19 +213,17 @@ private boolean shouldSkip(MavenDependency dependency) { * @return An input stream from which the artifact can be read, or {@code null} if not found in the sources * @throws IOException If an I/O error occurs while opening the stream */ - public InputStream findArtifactStream(MavenArtifact artifact) throws IOException { - synchronized (this.sources) { - for (ReadableMavenRepository source : sources) { - InputStream stream; - if ((stream = source.getArtifactStream(artifact)) != null) { - // Found the requested artifact - return stream; - } + public synchronized InputStream findArtifactStream(MavenArtifact artifact) throws IOException { + for (ReadableMavenRepository source : sources) { + InputStream stream; + if ((stream = source.getArtifactStream(artifact)) != null) { + // Found the requested artifact + return stream; } - - // Artifact has not been found in any source - return null; } + + // Artifact has not been found in any source + return null; } /** @@ -240,18 +233,16 @@ public InputStream findArtifactStream(MavenArtifact artifact) throws IOException * @return The found URI, or {@code null}, if not found in the sources * @throws IOException If an I/O error occurs while checking for the artifact */ - public Pair findArtifactURI(MavenArtifact artifact) throws IOException { - synchronized (this.sources) { - for (ReadableMavenRepository source : sources) { - URI uri = source.getArtifactURI(artifact); - if (uri != null) { - return new Pair<>(source, uri); - } + public synchronized Pair findArtifactURI(MavenArtifact artifact) throws IOException { + for (ReadableMavenRepository source : sources) { + URI uri = source.getArtifactURI(artifact); + if (uri != null) { + return new Pair<>(source, uri); } - - // Artifact has not been found in any source - return null; } + + // Artifact has not been found in any source + return null; } /** @@ -262,13 +253,11 @@ public Pair findArtifactURI(MavenArtifact artifact * @throws IOException If an I/O error occurs while trying to read a POM */ private MavenPom findPom(MavenArtifact artifact) throws IOException { - synchronized (this.sources) { - for (ReadableMavenRepository source : sources) { - MavenPom pom; - if ((pom = source.getArtifactPom(artifact)) != null) { - // Found the requested POM - return pom; - } + for (ReadableMavenRepository source : sources) { + MavenPom pom; + if ((pom = source.getArtifactPom(artifact)) != null) { + // Found the requested POM + return pom; } } @@ -285,7 +274,7 @@ private MavenPom findPom(MavenArtifact artifact) throws IOException { * @throws IOException If an I/O error occurs while installing the artifact */ @SuppressWarnings("BooleanMethodIsAlwaysInverted") - public boolean installArtifact(MavenArtifact artifact, SimpleMavenRepository target) throws IOException { + public synchronized boolean installArtifact(MavenArtifact artifact, SimpleMavenRepository target) throws IOException { try (InputStream stream = findArtifactStream(artifact)) { // Try to find the given artifact if (stream != null) { @@ -299,7 +288,7 @@ public boolean installArtifact(MavenArtifact artifact, SimpleMavenRepository tar LOGGER.lifecycle("Installing artifact {}", formatArtifact(artifact)); // Copy the artifact to the local path - Files.copy(stream, targetPath); + Files.copy(stream, targetPath, StandardCopyOption.REPLACE_EXISTING); return true; } else { return false;