Skip to content

Commit

Permalink
Fix Maven.evaluateExpression() not using repo credentials.
Browse files Browse the repository at this point in the history
Failed e.g. when querying something from a POM using a parent or dependency from a private repo.
  • Loading branch information
schnatterer committed Jul 10, 2020
1 parent f16464e commit bfb4223
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/com/cloudogu/ces/cesbuildlib/Maven.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ abstract class Maven implements Serializable {
this.script = script
}

def call(String args) {
def call(String args, boolean printStdOut = true) {
if (repositories.isEmpty()) {
mvn(args)
mvn(args, printStdOut)
} else {
script.withCredentials(createRepositoryCredentials(repositories)) {
mvn(args)
mvn(args, printStdOut)
}
}
}
Expand Down Expand Up @@ -93,7 +93,7 @@ abstract class Maven implements Serializable {

String evaluateExpression(String expression) {
// See also: https://blog.soebes.de/blog/2018/06/09/help-plugin/
def evaluatedString = mvn("org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate -Dexpression=${expression} -q -DforceStdout", false)
def evaluatedString = call("org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate -Dexpression=${expression} -q -DforceStdout", false)
// we take only the last line of the evaluated expression,
// because in the case of maven wrapper the home and sometimes the download is printed before
return evaluatedString.trim().readLines().last()
Expand Down Expand Up @@ -173,14 +173,14 @@ abstract class Maven implements Serializable {
* @param newVersion new project version
*/
void setVersion(String newVersion) {
mvn "versions:set -DgenerateBackupPoms=false -DnewVersion=${newVersion}"
call "versions:set -DgenerateBackupPoms=false -DnewVersion=${newVersion}"
}

/**
* Set version to next minor snapshot e.g.: 2.0.1 becomes 2.1.0-SNAPSHOT
*/
void setVersionToNextMinorSnapshot() {
mvn "build-helper:parse-version versions:set -DgenerateBackupPoms=false -DnewVersion='\${parsedVersion.majorVersion}.\${parsedVersion.nextMinorVersion}.0-SNAPSHOT'"
call "build-helper:parse-version versions:set -DgenerateBackupPoms=false -DnewVersion='\${parsedVersion.majorVersion}.\${parsedVersion.nextMinorVersion}.0-SNAPSHOT'"
}

/**
Expand Down
11 changes: 11 additions & 0 deletions test/com/cloudogu/ces/cesbuildlib/MavenTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ class MavenTest {
assertEquals("Unexpected version returned",
"org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate -Dexpression=project.version -q -DforceStdout", mvn.getVersion())
}

@Test
void testGetVersionWithCredentials() {
mvn.useRepositoryCredentials([id: 'number0', credentialsId: 'creds0'])
assertEquals("Unexpected version returned",
"org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate -Dexpression=project.version -q -DforceStdout", mvn.getVersion())

assert 'creds0' == scriptMock.actualUsernamePasswordArgs[0]['credentialsId']
assert "NEXUS_REPO_CREDENTIALS_PASSWORD_0" == scriptMock.actualUsernamePasswordArgs[0]['passwordVariable']
assert "NEXUS_REPO_CREDENTIALS_USERNAME_0" == scriptMock.actualUsernamePasswordArgs[0]['usernameVariable']
}

@Test
void testGetArtifactId() {
Expand Down

0 comments on commit bfb4223

Please sign in to comment.