Skip to content

Commit 60829c7

Browse files
committed
Support JRuby Maven GEM proxy (#401)
1 parent 814c36b commit 60829c7

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

core-plugin/README.adoc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,28 @@ This plugin offers the following functionality:
1313
=== Compatibility
1414

1515
This plugin requires link:http://gradle.org[Gradle] 4.3 or better
16+
17+
=== Installing
18+
19+
.build.gradle
20+
[source, groovy]
21+
----
22+
plugins {
23+
id 'com.github.jruby-gradle.core' version 'VERSION-OF-PLUGIN'
24+
}
25+
----
26+
27+
=== Adding repositories
28+
29+
.build.gradle
30+
[source,groovy]
31+
----
32+
repositories {
33+
ruby.mavengems() // <1>
34+
ruby.mavengems('https://foo.bar') // <2>
35+
ruby.mavengems('https://foo.bar', 'acme-rubygems') // <3>
36+
}
37+
----
38+
<1> Adds a Maven repository that uses the one official supported by the JRuby group. In order to use this, GEM dependencies should all be placed in the `rubygems` group.
39+
<2> Adds a custom Maven repository that will proxy a GEMs repository. In order to use this, GEM dependencies should all be placed in the `rubygems` group.
40+
<3> Adds a custom Maven repository that will proxy a GEMs repository, but allocate a custom dedicated Maven group. This is useful where you would want to use both the public repository and your own, but you want to save network query traffic, by only query repositories where you know the dependencies should exist.

core-plugin/src/main/groovy/com/github/jrubygradle/api/core/RepositoryHandlerExtension.groovy

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import org.gradle.api.Action
3131
import org.gradle.api.Project
3232
import org.gradle.api.artifacts.repositories.ArtifactRepository
3333
import org.gradle.api.artifacts.repositories.IvyArtifactRepository
34+
import org.gradle.api.artifacts.repositories.MavenArtifactRepository
3435
import org.gradle.util.GradleVersion
3536
import org.ysb33r.grolifant.api.ClosureUtils
3637

@@ -163,6 +164,56 @@ class RepositoryHandlerExtension {
163164
bindRepositoryToProxyServer(project.uri(uri), group, cfg)
164165
}
165166

167+
/** Adds the Maven-GEMs proxy that is supported by the JRuby group.
168+
*
169+
* For supporting Gradle versions, this repository will only be consulted for artifacts that are in the
170+
* {@code rubygems} group.
171+
*
172+
* @return Maven repository
173+
*/
174+
MavenArtifactRepository mavengems() {
175+
bindToMavenRepository(MAVENGEMS_URI, DEFAULT_GROUP_NAME)
176+
}
177+
178+
/** Adds a remote Maven-GEMs proxy.
179+
*
180+
* For supporting Gradle versions, this repository will only be consulted for artifacts that are in the
181+
* {@code rubygems} group.
182+
*
183+
* @param uri Remote Maven-GEMs proxy
184+
* @return Maven repository
185+
*/
186+
MavenArtifactRepository mavengems(Object uri) {
187+
bindToMavenRepository(project.uri(uri), DEFAULT_GROUP_NAME)
188+
}
189+
190+
/** Adds a remote Maven-GEMs proxy anbd allocate a dedicated group for it.
191+
*
192+
* For supporting Gradle versions, this repository will only be consulted for artifacts that are in the
193+
* specified group.
194+
*
195+
* @param group Maven group name
196+
* @param uri Remote Maven-GEMs proxy
197+
* @return Maven repository
198+
*/
199+
MavenArtifactRepository mavengems(String group, Object uri) {
200+
bindToMavenRepository(project.uri(uri), group)
201+
}
202+
203+
private MavenArtifactRepository bindToMavenRepository(
204+
URI serverUri,
205+
String group
206+
) {
207+
MavenArtifactRepository repo = project.repositories.maven(new Action<MavenArtifactRepository>() {
208+
@Override
209+
void execute(MavenArtifactRepository mvn) {
210+
mvn.url = serverUri
211+
}
212+
})
213+
restrictToGems(repo, group)
214+
repo
215+
}
216+
166217
private ArtifactRepository bindRepositoryToProxyServer(
167218
URI serverUri,
168219
String group,
@@ -220,4 +271,5 @@ class RepositoryHandlerExtension {
220271
private static final boolean HAS_CONTENT_FEATURE = GradleVersion.current() >= GradleVersion.version('5.1')
221272
private static final boolean HAS_SECURE_PROTOCOL_FEATURE = GradleVersion.current() >= GradleVersion.version('6.0')
222273
private static final URI RUBYGEMS_URI = 'https://rubygems.org'.toURI()
274+
private static final URI MAVENGEMS_URI = 'https://mavengems.jruby.org'.toURI()
223275
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.github.jrubygradle.api.core
2+
3+
import org.gradle.api.Project
4+
import org.gradle.testfixtures.ProjectBuilder
5+
import spock.lang.Specification
6+
7+
8+
class RepositoryHandlerExtensionSpec extends Specification {
9+
10+
Project project = ProjectBuilder.builder().build()
11+
12+
void 'Add Maven repository'() {
13+
when:
14+
project.allprojects {
15+
apply plugin : JRubyCorePlugin
16+
17+
repositories {
18+
ruby {
19+
mavengems()
20+
mavengems('https://goo1')
21+
mavengems('goo2','https://goo2')
22+
}
23+
}
24+
}
25+
26+
then:
27+
project.repositories.size() == 3
28+
}
29+
}

0 commit comments

Comments
 (0)