Skip to content

Commit

Permalink
[#4] Reuse password configured in uploadArchives task
Browse files Browse the repository at this point in the history
  • Loading branch information
szpak committed Mar 5, 2015
1 parent c2cca6f commit 08472c5
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/main/groovy/io/codearte/gradle/nexus/NexusStagingPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package io.codearte.gradle.nexus
import io.codearte.gradle.nexus.logic.OperationRetrier
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.maven.MavenDeployer
import org.gradle.api.tasks.Upload

class NexusStagingPlugin implements Plugin<Project> {

Expand All @@ -18,6 +20,7 @@ class NexusStagingPlugin implements Plugin<Project> {
def closeRepositoryTask = createAndConfigureCloseRepositoryTask(project)
def promoteRepositoryTask = createAndConfigurePromoteRepositoryTask(project)
promoteRepositoryTask.mustRunAfter(closeRepositoryTask)
tryToGetCredentialsFromUploadArchivesTask(project, extension)
}

void emitWarningIfAppliedNotToRootProject(Project project) {
Expand Down Expand Up @@ -76,4 +79,24 @@ class NexusStagingPlugin implements Plugin<Project> {
delayBetweenRetriesInMillis = { extension.delayBetweenRetriesInMillis }
}
}

private void tryToGetCredentialsFromUploadArchivesTask(Project project, NexusStagingExtension extension) {
project.afterEvaluate {
if (extension.username != null) {
return //username already set manually
}

Upload uploadTask = project.tasks.findByPath("uploadArchives")
uploadTask?.repositories.withType(MavenDeployer).each { MavenDeployer deployer ->
project.logger.debug("Trying to read credentials from repository '${deployer.name}'")
def authentication = deployer.repository.authentication //Not to use class names as maven-ant-task is not on classpath when plugin is executed
if (authentication.userName != null) {
extension.username = authentication.userName
extension.password = authentication.password
project.logger.info("Using username '${extension.username}' and password from repository '${deployer.name}'")
return //from each
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.codearte.gradle.nexus.functional

class PasswordFunctionalSpec extends BaseNexusStagingFunctionalSpec {

def "should read password from repository configured in uploadArchives"() {
given:
copyResources("sampleProjects//uploadArchives", "")
when:
def result = runTasksSuccessfully('tasks')
then:
result.standardOutput.contains("Using username 'testUsername' and password from repository 'Test repo'")
}
}
33 changes: 33 additions & 0 deletions src/test/resources/sampleProjects/uploadArchives/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apply plugin: 'groovy'
apply plugin: 'maven'

apply plugin: 'io.codearte.nexus-staging'

repositories {
mavenCentral()
}

dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.7'

testCompile 'org.spockframework:spock-core:0.7-groovy-2.0'
testCompile 'junit:junit:4.11'
}

artifacts {
archives jar
}

uploadArchives {
repositories {
mavenDeployer {
repository(id: 'test-repo-staging', url: "http://localhost:8089/nexus/content/repositories/internal/") {
authentication(userName: sonatypeUsername, password: sonatypePassword)
}
snapshotRepository(id: 'test-repo-snapshots', url: 'http://localhost:8089/nexus/content/repositories/internal-snapshots/') {
authentication(userName: 'testUser', password: 'noPassword')
}
name = 'Test repo'
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
group=com.example.upload1
version=0.0.1-SNAPSHOT
sonatypeUsername=testUsername
sonatypePassword=testPassword
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'uploadArchives'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Library {
boolean someLibraryMethod() {
true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import spock.lang.Specification

class LibraryTest extends Specification{
def "someLibraryMethod returns true"() {
setup:
Library lib = new Library()
when:
def result = lib.someLibraryMethod()
then:
result == true
}
}

0 comments on commit 08472c5

Please sign in to comment.