diff --git a/README.md b/README.md index b4f618c..b50579e 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,23 @@ For Gradle 2.1+ you can use the new plugin mechanism to download the plugin from ## Mirrors The plugin exposes Maven-like mirror capabilities. The plugin will properly register and enforce any mirrors defined in a `settings.xml` with `` values of `*`, `external:*` or `central`. Existing -`repositories {...}` definitions that match these identifiers will be removed. Credentials located in a matching -`` element are also used, and [decrypted](http://maven.apache.org/guides/mini/guide-encryption.html) if necessary. +`repositories {...}` definitions that match these identifiers will be removed. + +## Credentials +The plugin will attempt to apply credentials located in `` elements to appropriate Maven repository +definitions in your build script. This is done by matching the `` element in the `settings.xml` file to the `name` +property of the repository definition. + + repositories { + maven { + name = 'myRepo' // should match myRepo of appropriate in settings.xml + url = 'https://intranet.foo.org/repo' + } + } + +Server credentials are used for mirrors as well. When mirrors are added the plugin will look for a `` element +with the same `` and the configured credentials are used and [decrypted](http://maven.apache.org/guides/mini/guide-encryption.html) +if necessary. > **Note:** Currently only Basic Authentication using username and password is supported at this time. diff --git a/src/main/groovy/net/linguica/gradle/maven/settings/MavenSettingsPlugin.groovy b/src/main/groovy/net/linguica/gradle/maven/settings/MavenSettingsPlugin.groovy index a3f4525..fb169f7 100644 --- a/src/main/groovy/net/linguica/gradle/maven/settings/MavenSettingsPlugin.groovy +++ b/src/main/groovy/net/linguica/gradle/maven/settings/MavenSettingsPlugin.groovy @@ -55,6 +55,7 @@ public class MavenSettingsPlugin implements Plugin { loadSettings(project, extension) activateProfiles(project, extension) registerMirrors(project) + applyRepoCredentials(project) } } @@ -127,6 +128,16 @@ public class MavenSettingsPlugin implements Plugin { } } + private void applyRepoCredentials(Project project) { + project.repositories.all { repo -> + settings.servers.each { server -> + if (repo.name == server.id) { + addCredentials(server, repo) + } + } + } + } + private void createMirrorRepository(Project project, Mirror mirror) { createMirrorRepository(project, mirror) { true } } @@ -143,15 +154,19 @@ public class MavenSettingsPlugin implements Plugin { if (mirrorFound) { Server server = settings.getServer(mirror.id) - project.repositories.maven { + project.repositories.maven { repo -> name mirror.name ?: mirror.id url mirror.url - if (server?.username != null && server?.password != null) { - credentials { - username = server.username - password = server.password - } - } + addCredentials(server, repo) + } + } + } + + private addCredentials(Server server, MavenArtifactRepository repo) { + if (server?.username != null && server?.password != null) { + repo.credentials { + username = server.username + password = server.password } } } diff --git a/src/test/groovy/net/linguica/gradle/maven/settings/AbstractMavenSettingsTest.groovy b/src/test/groovy/net/linguica/gradle/maven/settings/AbstractMavenSettingsTest.groovy index ea19c19..33cee12 100644 --- a/src/test/groovy/net/linguica/gradle/maven/settings/AbstractMavenSettingsTest.groovy +++ b/src/test/groovy/net/linguica/gradle/maven/settings/AbstractMavenSettingsTest.groovy @@ -49,7 +49,7 @@ abstract class AbstractMavenSettingsTest { writer.write(settingsFile, null, settings) } - void addPluginWithSettings(Project project) { + void addPluginWithSettings() { project.with { apply plugin: MavenSettingsPlugin diff --git a/src/test/groovy/net/linguica/gradle/maven/settings/MavenSettingsPluginTest.groovy b/src/test/groovy/net/linguica/gradle/maven/settings/MavenSettingsPluginTest.groovy index 3cc683a..dc58699 100644 --- a/src/test/groovy/net/linguica/gradle/maven/settings/MavenSettingsPluginTest.groovy +++ b/src/test/groovy/net/linguica/gradle/maven/settings/MavenSettingsPluginTest.groovy @@ -18,6 +18,7 @@ package net.linguica.gradle.maven.settings import org.apache.maven.settings.Mirror import org.apache.maven.settings.Profile +import org.apache.maven.settings.Server import org.junit.Test import static org.junit.Assert.* @@ -40,7 +41,7 @@ class MavenSettingsPluginTest extends AbstractMavenSettingsTest { mirrors.add new Mirror(id: 'myrepo', mirrorOf: '*', url: 'http://maven.foo.bar') } - addPluginWithSettings(project) + addPluginWithSettings() project.with { repositories { @@ -63,7 +64,7 @@ class MavenSettingsPluginTest extends AbstractMavenSettingsTest { mirrors.add new Mirror(id: 'myrepo', mirrorOf: 'external:*', url: 'http://maven.foo.bar') } - addPluginWithSettings(project) + addPluginWithSettings() project.with { repositories { @@ -91,7 +92,7 @@ class MavenSettingsPluginTest extends AbstractMavenSettingsTest { mirrors.add new Mirror(id: 'myrepo', mirrorOf: 'external:*', url: 'http://maven.foo.bar') } - addPluginWithSettings(project) + addPluginWithSettings() project.with { repositories { @@ -119,7 +120,7 @@ class MavenSettingsPluginTest extends AbstractMavenSettingsTest { mirrors.add new Mirror(id: 'myrepo', mirrorOf: 'central', url: 'http://maven.foo.bar') } - addPluginWithSettings(project) + addPluginWithSettings() project.with { repositories { @@ -146,7 +147,7 @@ class MavenSettingsPluginTest extends AbstractMavenSettingsTest { mirrors.add new Mirror(id: 'myrepo', mirrorOf: 'central', url: 'http://maven.foo.bar') } - addPluginWithSettings(project) + addPluginWithSettings() project.with { repositories { @@ -175,7 +176,7 @@ class MavenSettingsPluginTest extends AbstractMavenSettingsTest { activeProfiles = ['myprofile'] } - addPluginWithSettings(project) + addPluginWithSettings() project.evaluate() @@ -191,7 +192,7 @@ class MavenSettingsPluginTest extends AbstractMavenSettingsTest { profiles.add new Profile(id: 'myprofile', properties: props) } - addPluginWithSettings(project) + addPluginWithSettings() project.with { mavenSettings { @@ -203,4 +204,27 @@ class MavenSettingsPluginTest extends AbstractMavenSettingsTest { assertThat(project.properties, hasEntry('myprop', 'true')) } + + @Test + void credentialsAddedToNamedRepository() { + withSettings { + servers.add new Server(id: 'central', username: 'first.last', password: 'secret') + } + + addPluginWithSettings() + + project.with { + repositories { + maven { + name 'central' + url 'https://repo1.maven.org/maven2/' + } + } + } + + project.evaluate() + + assertEquals('first.last', project.repositories.central.credentials.username) + assertEquals('secret', project.repositories.central.credentials.password) + } }