blob: 17bf92f76312c043d89eb22641d211af3f3fbd95 [file] [log] [blame]
Stuart McCullochf3173222012-06-07 21:57:32 +00001package aQute.bnd.maven.support;
2
3import java.io.*;
4import java.net.*;
5import java.util.*;
6
7import aQute.bnd.service.*;
8import aQute.lib.io.*;
9import aQute.lib.osgi.*;
10import aQute.libg.reporter.*;
11import aQute.libg.version.*;
12
13public class MavenRemoteRepository implements RepositoryPlugin, RegistryPlugin, Plugin {
14 Reporter reporter;
15 URI[] repositories;
16 Registry registry;
17 Maven maven;
18
Stuart McCulloch4482c702012-06-15 13:27:53 +000019 public File get(String bsn, String version, Strategy strategy, Map<String,String> properties) throws Exception {
Stuart McCullochf3173222012-06-07 21:57:32 +000020 String groupId = null;
Stuart McCulloch4482c702012-06-15 13:27:53 +000021
Stuart McCullochf3173222012-06-07 21:57:32 +000022 if (properties != null)
23 groupId = properties.get("groupId");
Stuart McCulloch4482c702012-06-15 13:27:53 +000024
Stuart McCullochf3173222012-06-07 21:57:32 +000025 if (groupId == null) {
26 int n = bsn.indexOf('+');
Stuart McCulloch4482c702012-06-15 13:27:53 +000027 if (n < 0)
Stuart McCullochf3173222012-06-07 21:57:32 +000028 return null;
Stuart McCulloch4482c702012-06-15 13:27:53 +000029
30 groupId = bsn.substring(0, n);
31 bsn = bsn.substring(n + 1);
Stuart McCullochf3173222012-06-07 21:57:32 +000032 }
33
34 String artifactId = bsn;
35
36 if (version == null) {
37 if (reporter != null)
38 reporter.error("Maven dependency version not set for %s - %s", groupId, artifactId);
39 return null;
40 }
41
42 CachedPom pom = getMaven().getPom(groupId, artifactId, version, repositories);
43
44 String value = properties == null ? null : properties.get("scope");
45 if (value == null)
46 return pom.getArtifact();
47
48 Pom.Scope action = null;
49
50 try {
51 action = Pom.Scope.valueOf(value);
52 return pom.getLibrary(action, repositories);
Stuart McCulloch4482c702012-06-15 13:27:53 +000053 }
54 catch (Exception e) {
Stuart McCullochf3173222012-06-07 21:57:32 +000055 return pom.getArtifact();
56 }
57 }
58
59 public Maven getMaven() {
Stuart McCulloch4482c702012-06-15 13:27:53 +000060 if (maven != null)
Stuart McCullochf3173222012-06-07 21:57:32 +000061 return maven;
Stuart McCulloch4482c702012-06-15 13:27:53 +000062
Stuart McCullochf3173222012-06-07 21:57:32 +000063 maven = registry.getPlugin(Maven.class);
64 return maven;
65 }
66
67 public boolean canWrite() {
68 return false;
69 }
70
71 public File put(Jar jar) throws Exception {
72 throw new UnsupportedOperationException("cannot do put");
73 }
74
75 public List<String> list(String regex) throws Exception {
76 throw new UnsupportedOperationException("cannot do list");
77 }
78
79 public List<Version> versions(String bsn) throws Exception {
80 throw new UnsupportedOperationException("cannot do versions");
81 }
82
83 public String getName() {
84 return "maven";
85 }
86
87 public void setRepositories(URI... urls) {
88 repositories = urls;
89 }
90
Stuart McCulloch4482c702012-06-15 13:27:53 +000091 public void setProperties(Map<String,String> map) {
Stuart McCullochf3173222012-06-07 21:57:32 +000092 String repoString = map.get("repositories");
93 if (repoString != null) {
94 String[] repos = repoString.split("\\s*,\\s*");
95 repositories = new URI[repos.length];
96 int n = 0;
97 for (String repo : repos) {
98 try {
99 URI uri = new URI(repo);
Stuart McCulloch4482c702012-06-15 13:27:53 +0000100 if (!uri.isAbsolute())
101 uri = IO.getFile(new File(""), repo).toURI();
Stuart McCullochf3173222012-06-07 21:57:32 +0000102 repositories[n++] = uri;
Stuart McCulloch4482c702012-06-15 13:27:53 +0000103 }
104 catch (Exception e) {
Stuart McCullochf3173222012-06-07 21:57:32 +0000105 if (reporter != null)
106 reporter.error("Invalid repository %s for maven plugin, %s", repo, e);
107 }
108 }
109 }
110 }
111
112 public void setReporter(Reporter reporter) {
113 this.reporter = reporter;
114 }
115
116 public void setRegistry(Registry registry) {
117 this.registry = registry;
118 }
119
120 public void setMaven(Maven maven) {
121 this.maven = maven;
122 }
123
124 public String getLocation() {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000125 if (repositories == null || repositories.length == 0)
Stuart McCullochf3173222012-06-07 21:57:32 +0000126 return "maven central";
Stuart McCulloch4482c702012-06-15 13:27:53 +0000127
128 return Arrays.toString(repositories);
Stuart McCullochf3173222012-06-07 21:57:32 +0000129 }
130}