Support JRuby Maven GEM proxy (#401)

This commit is contained in:
Schalk Cronje 2020-01-17 18:55:48 +01:00
parent 814c36b2a5
commit 60829c7ce4
3 changed files with 106 additions and 0 deletions

View File

@ -13,3 +13,28 @@ This plugin offers the following functionality:
=== Compatibility
This plugin requires link:http://gradle.org[Gradle] 4.3 or better
=== Installing
.build.gradle
[source, groovy]
----
plugins {
id 'com.github.jruby-gradle.core' version 'VERSION-OF-PLUGIN'
}
----
=== Adding repositories
.build.gradle
[source,groovy]
----
repositories {
ruby.mavengems() // <1>
ruby.mavengems('https://foo.bar') // <2>
ruby.mavengems('https://foo.bar', 'acme-rubygems') // <3>
}
----
<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.
<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.
<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.

View File

@ -31,6 +31,7 @@ import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.artifacts.repositories.ArtifactRepository
import org.gradle.api.artifacts.repositories.IvyArtifactRepository
import org.gradle.api.artifacts.repositories.MavenArtifactRepository
import org.gradle.util.GradleVersion
import org.ysb33r.grolifant.api.ClosureUtils
@ -163,6 +164,56 @@ class RepositoryHandlerExtension {
bindRepositoryToProxyServer(project.uri(uri), group, cfg)
}
/** Adds the Maven-GEMs proxy that is supported by the JRuby group.
*
* For supporting Gradle versions, this repository will only be consulted for artifacts that are in the
* {@code rubygems} group.
*
* @return Maven repository
*/
MavenArtifactRepository mavengems() {
bindToMavenRepository(MAVENGEMS_URI, DEFAULT_GROUP_NAME)
}
/** Adds a remote Maven-GEMs proxy.
*
* For supporting Gradle versions, this repository will only be consulted for artifacts that are in the
* {@code rubygems} group.
*
* @param uri Remote Maven-GEMs proxy
* @return Maven repository
*/
MavenArtifactRepository mavengems(Object uri) {
bindToMavenRepository(project.uri(uri), DEFAULT_GROUP_NAME)
}
/** Adds a remote Maven-GEMs proxy anbd allocate a dedicated group for it.
*
* For supporting Gradle versions, this repository will only be consulted for artifacts that are in the
* specified group.
*
* @param group Maven group name
* @param uri Remote Maven-GEMs proxy
* @return Maven repository
*/
MavenArtifactRepository mavengems(String group, Object uri) {
bindToMavenRepository(project.uri(uri), group)
}
private MavenArtifactRepository bindToMavenRepository(
URI serverUri,
String group
) {
MavenArtifactRepository repo = project.repositories.maven(new Action<MavenArtifactRepository>() {
@Override
void execute(MavenArtifactRepository mvn) {
mvn.url = serverUri
}
})
restrictToGems(repo, group)
repo
}
private ArtifactRepository bindRepositoryToProxyServer(
URI serverUri,
String group,
@ -220,4 +271,5 @@ class RepositoryHandlerExtension {
private static final boolean HAS_CONTENT_FEATURE = GradleVersion.current() >= GradleVersion.version('5.1')
private static final boolean HAS_SECURE_PROTOCOL_FEATURE = GradleVersion.current() >= GradleVersion.version('6.0')
private static final URI RUBYGEMS_URI = 'https://rubygems.org'.toURI()
private static final URI MAVENGEMS_URI = 'https://mavengems.jruby.org'.toURI()
}

View File

@ -0,0 +1,29 @@
package com.github.jrubygradle.api.core
import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
import spock.lang.Specification
class RepositoryHandlerExtensionSpec extends Specification {
Project project = ProjectBuilder.builder().build()
void 'Add Maven repository'() {
when:
project.allprojects {
apply plugin : JRubyCorePlugin
repositories {
ruby {
mavengems()
mavengems('https://goo1')
mavengems('goo2','https://goo2')
}
}
}
then:
project.repositories.size() == 3
}
}