4
4
import org .gradle .api .artifacts .Configuration ;
5
5
import org .gradle .api .artifacts .ExternalModuleDependency ;
6
6
import org .gradle .api .artifacts .ModuleVersionIdentifier ;
7
+ import com .google .common .cache .Cache ;
8
+ import com .google .common .cache .CacheBuilder ;
7
9
8
10
import java .io .File ;
9
11
import java .util .HashMap ;
10
12
import java .util .Map ;
11
- import java .util .Set ;
12
- import java .util .WeakHashMap ;
13
+ import java .util .concurrent .ExecutionException ;
13
14
import java .util .concurrent .TimeUnit ;
14
15
15
16
public class MavenArtifactDownloader {
17
+ private static final Cache <String , File > CACHE = CacheBuilder .newBuilder ()
18
+ .expireAfterWrite (5 , TimeUnit .MINUTES )
19
+ .build ();
16
20
17
- private static final Map <Project , Integer > COUNTERS = new WeakHashMap <>();
18
- private static final Map <String , Set <File >> CACHE = new HashMap <>();
19
21
private static final Map <String , String > VERSIONS = new HashMap <>();
20
22
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 ));
31
30
}
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
+ }
41
36
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 (":" , "/" );
50
39
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 );
54
63
return ret ;
55
64
}
56
65
@@ -59,12 +68,11 @@ public static File single(Project project, String artifact) {
59
68
}
60
69
61
70
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 );
64
72
}
65
73
66
74
public static String getVersion (Project project , String artifact ) {
67
- download (project , artifact );
75
+ single (project , artifact );
68
76
return VERSIONS .get (artifact );
69
77
}
70
78
}
0 commit comments