Skip to content

Commit 0541ac2

Browse files
committed
Make MavenArtifactDownloader use an expiring cache to prevent redundant/overloading lookups.
Seems to solve it randomly failing. Small hack to attach gradle API source to eclipse workspace.
1 parent 0d90cd4 commit 0541ac2

File tree

4 files changed

+68
-41
lines changed

4 files changed

+68
-41
lines changed

build.gradle

+21
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ plugins {
66

77
apply plugin: 'java'
88
apply plugin: 'maven-publish'
9+
apply plugin: 'eclipse'
910

1011
group = 'net.minecraftforge.gradle'
1112
version = gitVersion()
@@ -47,6 +48,11 @@ repositories {
4748
maven { url = 'http://files.minecraftforge.net/maven' }
4849
}
4950

51+
wrapper {
52+
gradleVersion = '4.9'
53+
distributionType = Wrapper.DistributionType.ALL
54+
}
55+
5056
dependencies {
5157
commonImplementation gradleApi()
5258
commonImplementation 'com.cloudbees:diff4j:1.2'
@@ -69,6 +75,21 @@ dependencies {
6975
userdevImplementation sourceSets.common.output
7076
}
7177

78+
//Gradle doesn't add it's own source when doing the API. So lets hack it in!
79+
import org.gradle.plugins.ide.eclipse.model.*
80+
import org.gradle.plugins.ide.eclipse.model.internal.*
81+
project.extensions.eclipse.classpath.file.whenMerged { Classpath cp ->
82+
def gradleSrc = gradle.gradleHomeDir.absolutePath.replace(File.separator, '/') + '/src/'
83+
cp.entries.each { entry ->
84+
if ((entry in AbstractLibrary) && entry.library.file.name.startsWith('gradle-')) {
85+
def type = (entry.library.file.name =~ "^gradle(-(.*))?-(${gradle.gradleVersion})")[0][2]
86+
if (type == 'api') type = 'core-api' //Gradle name is different for cores
87+
if (type == '') type = 'core'
88+
entry.sourcePath = new FileReferenceFactory().fromPath(gradleSrc + type)
89+
}
90+
}
91+
}
92+
7293
publishing {
7394
publications {
7495
mavenJava(MavenPublication) {
+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#Wed Aug 01 17:10:49 CEST 2018
21
distributionBase=GRADLE_USER_HOME
32
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-all.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-all.zip

src/common/java/net/minecraftforge/gradle/common/util/MavenArtifactDownloader.java

+45-37
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,62 @@
44
import org.gradle.api.artifacts.Configuration;
55
import org.gradle.api.artifacts.ExternalModuleDependency;
66
import org.gradle.api.artifacts.ModuleVersionIdentifier;
7+
import com.google.common.cache.Cache;
8+
import com.google.common.cache.CacheBuilder;
79

810
import java.io.File;
911
import java.util.HashMap;
1012
import java.util.Map;
11-
import java.util.Set;
12-
import java.util.WeakHashMap;
13+
import java.util.concurrent.ExecutionException;
1314
import java.util.concurrent.TimeUnit;
1415

1516
public class MavenArtifactDownloader {
17+
private static final Cache<String, File> CACHE = CacheBuilder.newBuilder()
18+
.expireAfterWrite(5, TimeUnit.MINUTES)
19+
.build();
1620

17-
private static final Map<Project, Integer> COUNTERS = new WeakHashMap<>();
18-
private static final Map<String, Set<File>> CACHE = new HashMap<>();
1921
private static final Map<String, String> VERSIONS = new HashMap<>();
2022

21-
public static Set<File> download(Project project, String artifact) {
22-
return download(project, artifact, false);
23-
}
24-
public static Set<File> download(Project project, String artifact, boolean changing) {
25-
Set<File> ret = CACHE.get(artifact);
26-
if (ret == null) {
27-
String name = "mavenDownloader_" + artifact.replace(":", "/");
28-
synchronized(project) {
29-
name += COUNTERS.getOrDefault(project, 0);
30-
COUNTERS.compute(project, (proj, prev) -> (prev != null ? prev : 0) + 1);
23+
private static File _download(Project project, String artifact, boolean changing) {
24+
File ret = null;
25+
try {
26+
ret = CACHE.get(artifact, () -> gradleDownload(project, artifact, changing));
27+
if (ret != null && !ret.exists()) {
28+
CACHE.invalidate(artifact);
29+
ret = CACHE.get(artifact, () -> gradleDownload(project, artifact, changing));
3130
}
32-
Configuration cfg = project.getConfigurations().create(name);
33-
ExternalModuleDependency dependency = (ExternalModuleDependency)project.getDependencies().create(artifact);
34-
dependency.setChanging(changing);
35-
cfg.getDependencies().add(dependency);
36-
cfg.resolutionStrategy(strat -> {
37-
strat.cacheChangingModulesFor(5, TimeUnit.MINUTES);
38-
strat.cacheDynamicVersionsFor(5, TimeUnit.MINUTES);
39-
});
40-
ret = cfg.resolve();
31+
} catch (ExecutionException e) {
32+
e.printStackTrace();
33+
}
34+
return ret;
35+
}
4136

42-
Artifact mine = Artifact.from(artifact);
43-
cfg.getResolvedConfiguration().getResolvedArtifacts().forEach(art -> {
44-
ModuleVersionIdentifier resolved = art.getModuleVersion().getId();
45-
if (resolved.getGroup().equals(mine.getGroup()) && resolved.getName().equals(mine.getName())) {
46-
if ((mine.getClassifier() == null && art.getClassifier() == null) || mine.getClassifier().equals(art.getClassifier()))
47-
VERSIONS.put(artifact, resolved.getVersion());
48-
}
49-
});
37+
private static File gradleDownload(Project project, String artifact, boolean changing) {
38+
String name = "mavenDownloader_" + artifact.replace(":", "/");
5039

51-
project.getConfigurations().remove(cfg);
52-
//CACHE.put(artifact, ret); //Daemons break this
53-
}
40+
//TODO: Bypass gradle's crap?
41+
//List<ArtifactRepository> repos = project.getRepositories();
42+
43+
Configuration cfg = project.getConfigurations().create(name);
44+
ExternalModuleDependency dependency = (ExternalModuleDependency)project.getDependencies().create(artifact);
45+
dependency.setChanging(changing);
46+
cfg.getDependencies().add(dependency);
47+
cfg.resolutionStrategy(strat -> {
48+
strat.cacheChangingModulesFor(5, TimeUnit.MINUTES);
49+
strat.cacheDynamicVersionsFor(5, TimeUnit.MINUTES);
50+
});
51+
File ret = cfg.resolve().iterator().next(); //We only want the first, not transitive
52+
53+
Artifact mine = Artifact.from(artifact);
54+
cfg.getResolvedConfiguration().getResolvedArtifacts().forEach(art -> {
55+
ModuleVersionIdentifier resolved = art.getModuleVersion().getId();
56+
if (resolved.getGroup().equals(mine.getGroup()) && resolved.getName().equals(mine.getName())) {
57+
if ((mine.getClassifier() == null && art.getClassifier() == null) || mine.getClassifier().equals(art.getClassifier()))
58+
VERSIONS.put(artifact, resolved.getVersion());
59+
}
60+
});
61+
62+
project.getConfigurations().remove(cfg);
5463
return ret;
5564
}
5665

@@ -59,12 +68,11 @@ public static File single(Project project, String artifact) {
5968
}
6069

6170
public static File single(Project project, String artifact, boolean changing) {
62-
Set<File> ret = download(project, artifact, changing);
63-
return ret == null ? null : ret.iterator().next();
71+
return _download(project, artifact, changing);
6472
}
6573

6674
public static String getVersion(Project project, String artifact) {
67-
download(project, artifact);
75+
single(project, artifact);
6876
return VERSIONS.get(artifact);
6977
}
7078
}

src/mcp/java/net/minecraftforge/gradle/mcp/function/ListLibrariesFunction.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ public File execute(MCPEnvironment environment) {
3030
Set<File> files = new HashSet<>();
3131
for (JsonElement libElement : json.getAsJsonArray("libraries")) {
3232
JsonObject library = libElement.getAsJsonObject();
33-
Set<File> libFiles = MavenArtifactDownloader.download(environment.project, library.get("name").getAsString());
34-
files.addAll(libFiles);
33+
files.add(MavenArtifactDownloader.single(environment.project, library.get("name").getAsString()));
3534
}
3635

3736
// Write the list

0 commit comments

Comments
 (0)