Skip to content

Commit

Permalink
Use settings.xml credentials for any configured repositories.
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-vieira committed Mar 12, 2015
1 parent 78c54d5 commit addb430
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 17 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<mirrorOf>` values of `*`, `external:*` or `central`. Existing
`repositories {...}` definitions that match these identifiers will be removed. Credentials located in a matching
`<server>` 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 `<server>` elements to appropriate Maven repository
definitions in your build script. This is done by matching the `<id>` element in the `settings.xml` file to the `name`
property of the repository definition.

repositories {
maven {
name = 'myRepo' // should match <id>myRepo</id> of appropriate <server> 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 `<server>` element
with the same `<id>` 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class MavenSettingsPlugin implements Plugin<Project> {
loadSettings(project, extension)
activateProfiles(project, extension)
registerMirrors(project)
applyRepoCredentials(project)
}
}

Expand Down Expand Up @@ -127,6 +128,16 @@ public class MavenSettingsPlugin implements Plugin<Project> {
}
}

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 }
}
Expand All @@ -143,15 +154,19 @@ public class MavenSettingsPlugin implements Plugin<Project> {

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
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ abstract class AbstractMavenSettingsTest {
writer.write(settingsFile, null, settings)
}

void addPluginWithSettings(Project project) {
void addPluginWithSettings() {
project.with {
apply plugin: MavenSettingsPlugin

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -175,7 +176,7 @@ class MavenSettingsPluginTest extends AbstractMavenSettingsTest {
activeProfiles = ['myprofile']
}

addPluginWithSettings(project)
addPluginWithSettings()

project.evaluate()

Expand All @@ -191,7 +192,7 @@ class MavenSettingsPluginTest extends AbstractMavenSettingsTest {
profiles.add new Profile(id: 'myprofile', properties: props)
}

addPluginWithSettings(project)
addPluginWithSettings()

project.with {
mavenSettings {
Expand All @@ -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)
}
}

0 comments on commit addb430

Please sign in to comment.