|
45 | 45 |
|
46 | 46 | (def ^:private separator java.io.File/separator)
|
47 | 47 |
|
48 |
| -(def ^:private local-maven-repo-d |
49 |
| - (delay |
50 |
| - (try |
51 |
| - ; The command: |
52 |
| - ; mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout |
53 |
| - ; determines where the local repository is located. |
54 |
| - (let [sh-result (sh/sh "mvn" "help:evaluate" "-Dexpression=settings.localRepository" "-q" "-DforceStdout")] |
55 |
| - (if (zero? (:exit sh-result)) |
56 |
| - (s/trim (:out sh-result)) |
57 |
| - (str (System/getProperty "user.home") (str separator ".m2" separator "repository")))) |
58 |
| - (catch java.io.IOException _ |
59 |
| - (str (System/getProperty "user.home") (str separator ".m2" separator "repository")))))) |
60 |
| - |
61 |
| -; TODO: make this configurable |
62 |
| -(def ^:private remote-maven-repos {"central" "https://repo1.maven.org/maven2" |
63 |
| - "clojars" "https://repo.clojars.org"}) |
| 48 | +(def default-local-maven-repo |
| 49 | + "A String containing a file path for the default local Maven artifact |
| 50 | + cache that the library uses. Attempts to use this Maven client command to |
| 51 | + determine this value: |
| 52 | +
|
| 53 | + mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout |
| 54 | +
|
| 55 | + but falls back on a \"best guess\" if the Maven client isn't installed or |
| 56 | + cannot be executed." |
| 57 | + (try |
| 58 | + ; The command: |
| 59 | + ; mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout |
| 60 | + ; determines where the local repository is located. |
| 61 | + (let [sh-result (sh/sh "mvn" "help:evaluate" "-Dexpression=settings.localRepository" "-q" "-DforceStdout")] |
| 62 | + (if (zero? (:exit sh-result)) |
| 63 | + (s/trim (:out sh-result)) |
| 64 | + (str (System/getProperty "user.home") (str separator ".m2" separator "repository")))) |
| 65 | + (catch java.io.IOException _ |
| 66 | + (str (System/getProperty "user.home") (str separator ".m2" separator "repository"))))) |
| 67 | + |
| 68 | +(def ^:private local-maven-repo-a (atom default-local-maven-repo)) |
| 69 | + |
| 70 | +(defn local-maven-repo |
| 71 | + "The current local Maven repo in use, as a String containing a file path." |
| 72 | + [] |
| 73 | + @local-maven-repo-a) |
| 74 | + |
| 75 | +(defn set-local-maven-repo! |
| 76 | + "Sets the local Maven repo to use from this point onward. The argument is a |
| 77 | + String containing a file path that must be a readable directory that exists |
| 78 | + (throws ex-info if these conditions are not met)." |
| 79 | + [dir] |
| 80 | + (let [d (io/file dir)] |
| 81 | + (if (and (.exists d) |
| 82 | + (.isDirectory d) |
| 83 | + (.canRead d)) |
| 84 | + (swap! local-maven-repo-a (constantly dir)) |
| 85 | + (throw (ex-info (str dir " either does not exist, is not a directory, or is not readable.") {})))) |
| 86 | + nil) |
| 87 | + |
| 88 | +(def default-remote-maven-repos |
| 89 | + "A map containing the default remote Maven artifact repositories that the |
| 90 | + library uses. Each key is a string that's the short identifier of the repo |
| 91 | + (e.g. \"clojars\"), and each value is the base URL of that artifact repository |
| 92 | + (e.g. \"https://repo.clojars.org\")." |
| 93 | + {"central" "https://repo1.maven.org/maven2" |
| 94 | + "clojars" "https://repo.clojars.org"}) |
| 95 | + |
| 96 | +(def ^:private remote-maven-repos-a (atom default-remote-maven-repos)) |
| 97 | + |
| 98 | +(defn remote-maven-repos |
| 99 | + "The current remote Maven repos in use, as a map in the format described in |
| 100 | + `default-remote-maven-repos`." |
| 101 | + [] |
| 102 | + @remote-maven-repos-a) |
| 103 | + |
| 104 | +(defn set-remote-maven-repos! |
| 105 | + "Sets the remote Maven repos to use from this point onward. The argument is a |
| 106 | + map in the format described in `default-remote-maven-repos`. |
| 107 | +
|
| 108 | + For most use cases you should merge `default-remote-maven-repos` with whatever |
| 109 | + additional repos you wish to provide (the rare exceptions being situations |
| 110 | + such as a dev environment that contains a custom Maven artifact repository |
| 111 | + that proxies/caches Maven Central and/or Clojars)." |
| 112 | + [repos] |
| 113 | + (swap! remote-maven-repos-a (constantly repos)) |
| 114 | + nil) |
64 | 115 |
|
65 | 116 | (xml/alias-uri 'pom "http://maven.apache.org/POM/4.0.0")
|
66 | 117 |
|
|
103 | 154 | "Returns a java.net.URI pointing to the maven-metadata.xml for the given GA,
|
104 | 155 | or nil if one cannot be found. The returned URI is guaranteed to be
|
105 | 156 | resolvable - either to a file that exists in the local Maven cache, or to an
|
106 |
| - HTTP-accessible resource on a remote Maven repository (i.e. Maven Central or |
| 157 | + HTTP-accessible resource on a remote Maven repository (e.g. Maven Central, |
107 | 158 | Clojars) that resolves."
|
108 | 159 | ([{:keys [group-id artifact-id]}] (ga->metadata-uri group-id artifact-id))
|
109 | 160 | ([group-id artifact-id]
|
110 | 161 | (when (and (not (s/blank? group-id))
|
111 | 162 | (not (s/blank? artifact-id)))
|
112 | 163 | (let [ga-path (str (s/replace group-id "." "/") "/" artifact-id)
|
113 |
| - local-metadata-paths (map #(str ga-path "/maven-metadata-" % ".xml") (keys remote-maven-repos))] |
| 164 | + local-metadata-paths (map #(str ga-path "/maven-metadata-" % ".xml") (keys @remote-maven-repos-a))] |
114 | 165 | (if-let [local-metadata-file (first (filter #(and (.exists ^java.io.File %) (.isFile ^java.io.File %))
|
115 |
| - (map #(io/file (str @local-maven-repo-d "/" %)) (map #(s/replace % "/" separator) local-metadata-paths))))] |
| 166 | + (map #(io/file (str @local-maven-repo-a "/" %)) (map #(s/replace % "/" separator) local-metadata-paths))))] |
116 | 167 | (.toURI ^java.io.File local-metadata-file)
|
117 |
| - (when-let [remote-uri (first (filter lcihttp/uri-resolves? (map #(str % "/" ga-path "/maven-metadata.xml") (vals remote-maven-repos))))] |
| 168 | + (when-let [remote-uri (first (filter lcihttp/uri-resolves? (map #(str % "/" ga-path "/maven-metadata.xml") (vals @remote-maven-repos-a))))] |
118 | 169 | (java.net.URI. remote-uri)))))))
|
119 | 170 |
|
120 | 171 | (defn gav->metadata-uri
|
121 | 172 | "Returns a java.net.URI pointing to the maven-metadata.xml for the given GAV,
|
122 | 173 | or nil if one cannot be found. The returned URI is guaranteed to be
|
123 | 174 | resolvable - either to a file that exists in the local Maven cache, or to an
|
124 |
| - HTTP-accessible resource on a remote Maven repository (i.e. Maven Central or |
| 175 | + HTTP-accessible resource on a remote Maven repository (e.g. Maven Central, |
125 | 176 | Clojars) that resolves."
|
126 | 177 | ([{:keys [group-id artifact-id version]}] (gav->metadata-uri group-id artifact-id version))
|
127 | 178 | ([group-id artifact-id version]
|
128 | 179 | (when (and (not (s/blank? group-id))
|
129 | 180 | (not (s/blank? artifact-id))
|
130 | 181 | (not (s/blank? version)))
|
131 | 182 | (let [gav-path (str (s/replace group-id "." "/") "/" artifact-id "/" version)
|
132 |
| - local-metadata-paths (map #(str gav-path "/maven-metadata-" % ".xml") (keys remote-maven-repos))] |
| 183 | + local-metadata-paths (map #(str gav-path "/maven-metadata-" % ".xml") (keys @remote-maven-repos-a))] |
133 | 184 | (if-let [local-metadata-file (first (filter #(and (.exists ^java.io.File %) (.isFile ^java.io.File %))
|
134 |
| - (map #(io/file (str @local-maven-repo-d "/" %)) (map #(s/replace % "/" separator) local-metadata-paths))))] |
| 185 | + (map #(io/file (str @local-maven-repo-a "/" %)) (map #(s/replace % "/" separator) local-metadata-paths))))] |
135 | 186 | (.toURI ^java.io.File local-metadata-file)
|
136 |
| - (when-let [remote-uri (first (filter lcihttp/uri-resolves? (map #(str % "/" gav-path "/maven-metadata.xml") (vals remote-maven-repos))))] |
| 187 | + (when-let [remote-uri (first (filter lcihttp/uri-resolves? (map #(str % "/" gav-path "/maven-metadata.xml") (vals @remote-maven-repos-a))))] |
137 | 188 | (java.net.URI. remote-uri)))))))
|
138 | 189 |
|
139 | 190 | (defn ga-latest-version
|
|
187 | 238 | "Returns a java.net.URI pointing to the POM for the given GAV, or nil if one
|
188 | 239 | cannot be found. The returned URI is guaranteed to be resolvable - either to
|
189 | 240 | a file that exists in the local Maven cache, or to an HTTP-accessible resource
|
190 |
| - on a remote Maven repository (i.e. Maven Central or Clojars) that resolves. |
| 241 | + on a remote Maven repository (e.g. Maven Central, Clojars) that resolves. |
191 | 242 |
|
192 | 243 | If version is not provided, determines the latest version (which may be a
|
193 | 244 | SNAPSHOT) and uses that."
|
|
202 | 253 | version (if (release-version? version) (ga-release-version group-id artifact-id) version)
|
203 | 254 | file-version (resolve-snapshot-version group-id artifact-id version)
|
204 | 255 | gav-path (str (s/replace group-id "." "/") "/" artifact-id "/" version "/" artifact-id "-" file-version ".pom")
|
205 |
| - local-pom (io/file (str @local-maven-repo-d separator (s/replace gav-path "/" separator)))] |
| 256 | + local-pom (io/file (str @local-maven-repo-a separator (s/replace gav-path "/" separator)))] |
206 | 257 | (if (and (.exists local-pom)
|
207 | 258 | (.isFile local-pom))
|
208 | 259 | (.toURI local-pom)
|
209 |
| - (when-let [remote-uri (first (filter lcihttp/uri-resolves? (map #(str % "/" gav-path) (vals remote-maven-repos))))] |
| 260 | + (when-let [remote-uri (first (filter lcihttp/uri-resolves? (map #(str % "/" gav-path) (vals @remote-maven-repos-a))))] |
210 | 261 | (java.net.URI. remote-uri)))))))
|
211 | 262 |
|
212 | 263 | (defmulti pom->expressions-info
|
|
309 | 360 | allow explicit control of the cost of initialisation to callers who need it."
|
310 | 361 | []
|
311 | 362 | (lcmtch/init!)
|
312 |
| - @local-maven-repo-d |
| 363 | + @local-maven-repo-a |
313 | 364 | nil)
|
0 commit comments