diff --git a/build.gradle.kts b/build.gradle.kts index 918f789..6e0cabd 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -71,7 +71,7 @@ repositories { mavenCentral() } -version = System.getenv().getOrDefault("VERSION", "2.10.1") +version = System.getenv().getOrDefault("VERSION", "2.11.0") dependencies { implementation(group = "com.fasterxml.jackson.core", name = "jackson-core", version = "2.11.1") diff --git a/src/main/java/net/flintmc/gradle/maven/MavenArtifactDownloader.java b/src/main/java/net/flintmc/gradle/maven/MavenArtifactDownloader.java index 0cdb4fb..1c9331c 100644 --- a/src/main/java/net/flintmc/gradle/maven/MavenArtifactDownloader.java +++ b/src/main/java/net/flintmc/gradle/maven/MavenArtifactDownloader.java @@ -60,7 +60,9 @@ public MavenArtifactDownloader() { * @param source The repository to add */ public void addSource(ReadableMavenRepository source) { - this.sources.add(source); + synchronized (this.sources) { + this.sources.add(source); + } } /** @@ -70,7 +72,9 @@ public void addSource(ReadableMavenRepository source) { * @return {@code true} if this downloader has the give repository as a source, {@code false} otherwise */ public boolean hasSource(ReadableMavenRepository source) { - return this.sources.contains(source); + synchronized (this.sources) { + return this.sources.contains(source); + } } /** @@ -79,7 +83,9 @@ public boolean hasSource(ReadableMavenRepository source) { * @param source The repository to remove */ public void removeSource(ReadableMavenRepository source) { - this.sources.remove(source); + synchronized (this.sources) { + this.sources.remove(source); + } } /** @@ -97,7 +103,7 @@ public void installAll(MavenArtifact artifact, SimpleMavenRepository target, boo Path localPomPath = target.getPomPath(artifact); MavenPom artifactPom; - if(artifact instanceof MavenPom) { + if (artifact instanceof MavenPom) { // If the artifact is a POM already, don't try to read it from anywhere artifactPom = (MavenPom) artifact; } else { @@ -105,21 +111,21 @@ public void installAll(MavenArtifact artifact, SimpleMavenRepository target, boo artifactPom = Files.exists(localPomPath) ? PomReader.read(localPomPath) : findPom(artifact); } - if(artifactPom != null) { + if (artifactPom != null) { // POM found, install it with all its dependencies installAll(artifactPom, target); - if(installIfNotExists) { - if(!Files.exists(localPomPath)) { + if (installIfNotExists) { + if (!Files.exists(localPomPath)) { // The POM file does not exist locally, write it down PomWriter.write(artifactPom, localPomPath); } } } - if(installIfNotExists) { + if (installIfNotExists) { // If the artifact is not installed locally, try to install it - if(!target.isInstalled(artifact) && !installArtifact(artifact, target) && artifactPom == null) { + if (!target.isInstalled(artifact) && !installArtifact(artifact, target) && artifactPom == null) { // The artifact failed to install and there was also no POM for it throw new MavenResolveException("Could not resolve " + artifact); } @@ -141,19 +147,19 @@ private void installAll(MavenPom pom, SimpleMavenRepository target) throws IOExc Set toProcess = new HashSet<>(pom.getDependencies()); // Process until the list to process is empty - while(!toProcess.isEmpty()) { + while (!toProcess.isEmpty()) { // Copy over the dependencies to process into the current processing round Set roundDependencies = new HashSet<>(toProcess); toProcess.clear(); - for(MavenDependency dependency : roundDependencies) { + for (MavenDependency dependency : roundDependencies) { // Mark the dependency as found dependencies.add(dependency); // Sort out unwanted dependencies - if(shouldSkip(dependency)) { + if (shouldSkip(dependency)) { continue; - } else if(dependency.isBroken()) { + } else if (dependency.isBroken()) { continue; } @@ -162,30 +168,30 @@ private void installAll(MavenPom pom, SimpleMavenRepository target) throws IOExc // Try to read the POM locally, if it does not exist fall back to reading it online MavenPom dependencyPom = Files.exists(localPomPath) ? PomReader.read(localPomPath) : findPom(dependency); - if(dependencyPom != null) { + if (dependencyPom != null) { // A POM file was found, iterate its dependencies - for(MavenDependency innerDependency : dependencyPom.getDependencies()) { + for (MavenDependency innerDependency : dependencyPom.getDependencies()) { // Sort out unwanted dependencies - if(shouldSkip(innerDependency)) { + if (shouldSkip(innerDependency)) { continue; - } else if(innerDependency.isBroken()) { + } else if (innerDependency.isBroken()) { continue; } - if(!dependencies.contains(innerDependency)) { + if (!dependencies.contains(innerDependency)) { // The dependency has not been found already, process it toProcess.add(innerDependency); } } - if(!Files.exists(localPomPath)) { + if (!Files.exists(localPomPath)) { // If the POM does not exist locally, install it PomWriter.write(dependencyPom, localPomPath); } } // Try to install the dependency locally - if(!target.isInstalled(dependency) && !installArtifact(dependency, target) && dependencyPom == null) { + if (!target.isInstalled(dependency) && !installArtifact(dependency, target) && dependencyPom == null) { // The dependency had no artifact and also no POM throw new MavenResolveException("Could not resolve " + dependency); } @@ -213,16 +219,18 @@ private boolean shouldSkip(MavenDependency dependency) { * @throws IOException If an I/O error occurs while opening the stream */ public InputStream findArtifactStream(MavenArtifact artifact) throws IOException { - for(ReadableMavenRepository source : sources) { - InputStream stream; - if((stream = source.getArtifactStream(artifact)) != null) { - // Found the requested artifact - return stream; + synchronized (this.sources) { + 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; + } } /** @@ -233,15 +241,17 @@ public InputStream findArtifactStream(MavenArtifact artifact) throws IOException * @throws IOException If an I/O error occurs while checking for the artifact */ public Pair findArtifactURI(MavenArtifact artifact) throws IOException { - for(ReadableMavenRepository source : sources) { - URI uri = source.getArtifactURI(artifact); - if(uri != null) { - return new Pair<>(source, uri); + synchronized (this.sources) { + 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; + } } /** @@ -252,11 +262,13 @@ 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 { - for(ReadableMavenRepository source : sources) { - MavenPom pom; - if((pom = source.getArtifactPom(artifact)) != null) { - // Found the requested POM - return pom; + synchronized (this.sources) { + for (ReadableMavenRepository source : sources) { + MavenPom pom; + if ((pom = source.getArtifactPom(artifact)) != null) { + // Found the requested POM + return pom; + } } } @@ -274,12 +286,12 @@ private MavenPom findPom(MavenArtifact artifact) throws IOException { */ @SuppressWarnings("BooleanMethodIsAlwaysInverted") public boolean installArtifact(MavenArtifact artifact, SimpleMavenRepository target) throws IOException { - try(InputStream stream = findArtifactStream(artifact)) { + try (InputStream stream = findArtifactStream(artifact)) { // Try to find the given artifact - if(stream != null) { + if (stream != null) { // The artifact has been found, install it Path targetPath = target.getArtifactPath(artifact); - if(!Files.isDirectory(targetPath.getParent())) { + if (!Files.isDirectory(targetPath.getParent())) { // Make sure the parent directories exist Files.createDirectories(targetPath.getParent()); }