Use executeMaven step for artifact versioning (#114)

* Execute Maven inside Docker container
* Fix test
This commit is contained in:
Florian Wilhelm
2018-03-06 13:28:57 +01:00
committed by Oliver Nocon
parent ff46bb0f00
commit f6c71a8484
3 changed files with 51 additions and 16 deletions
@@ -13,6 +13,6 @@ class MavenArtifactVersioning extends ArtifactVersioning {
@Override
def setVersion(version) {
script.sh "mvn versions:set -DnewVersion=${version} --file ${configuration.filePath}"
script.mavenExecute script: script, goals: 'versions:set', defines: "-DnewVersion=${version}", pomPath: configuration.filePath
}
}
+32 -9
View File
@@ -6,11 +6,20 @@ import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import util.*
import util.JenkinsEnvironmentRule
import util.JenkinsLoggingRule
import util.JenkinsReadMavenPomRule
import util.JenkinsShellCallRule
import util.JenkinsStepRule
import util.JenkinsWriteFileRule
import util.Rules
import static org.junit.Assert.assertEquals
class ArtifactSetVersionTest extends BasePipelineTest {
Map dockerParameters
def mavenExecuteScript
def gitUtils
def sshAgentList = []
@@ -34,6 +43,16 @@ class ArtifactSetVersionTest extends BasePipelineTest {
@Before
void init() throws Throwable {
dockerParameters = [:]
helper.registerAllowedMethod("dockerExecute", [Map.class, Closure.class],
{ parameters, closure ->
dockerParameters = parameters
closure()
})
mavenExecuteScript = loadScript("mavenExecute.groovy").mavenExecute
helper.registerAllowedMethod("sshagent", [List.class, Closure.class], { list, closure ->
sshAgentList = list
return closure()
@@ -43,6 +62,9 @@ class ArtifactSetVersionTest extends BasePipelineTest {
jscr.setReturnValue("date --universal +'%Y%m%d%H%M%S'", '20180101010203')
jscr.setReturnValue('git diff --quiet HEAD', 0)
binding.setVariable('Jenkins', [instance: [pluginManager: [plugins: [new DockerExecuteTest.PluginMock()]]]])
gitUtils = new GitUtils()
prepareObjectInterceptors(gitUtils)
@@ -56,12 +78,12 @@ class ArtifactSetVersionTest extends BasePipelineTest {
assertEquals('1.2.3-20180101010203_testCommitId', jer.env.getArtifactVersion())
assertEquals('testCommitId', jer.env.getGitCommitId())
assertEquals('mvn versions:set -DnewVersion=1.2.3-20180101010203_testCommitId --file pom.xml', jscr.shell[3])
assertEquals('git add .', jscr.shell[4])
assertEquals ("git commit -m 'update version 1.2.3-20180101010203_testCommitId'", jscr.shell[5])
assertEquals ("git remote set-url origin myGitSshUrl", jscr.shell[6])
assertEquals ("git tag build_1.2.3-20180101010203_testCommitId", jscr.shell[7])
assertEquals ("git push origin build_1.2.3-20180101010203_testCommitId", jscr.shell[8])
assertEquals('mvn --file \'pom.xml\' versions:set -DnewVersion=1.2.3-20180101010203_testCommitId', jscr.shell[5])
assertEquals('git add .', jscr.shell[6])
assertEquals ("git commit -m 'update version 1.2.3-20180101010203_testCommitId'", jscr.shell[7])
assertEquals ("git remote set-url origin myGitSshUrl", jscr.shell[8])
assertEquals ("git tag build_1.2.3-20180101010203_testCommitId", jscr.shell[9])
assertEquals ("git push origin build_1.2.3-20180101010203_testCommitId", jscr.shell[10])
}
@Test
@@ -69,14 +91,14 @@ class ArtifactSetVersionTest extends BasePipelineTest {
jsr.step.call(script: [commonPipelineEnvironment: jer.env], juStabGitUtils: gitUtils, buildTool: 'maven', commitVersion: false)
assertEquals('1.2.3-20180101010203_testCommitId', jer.env.getArtifactVersion())
assertEquals('mvn versions:set -DnewVersion=1.2.3-20180101010203_testCommitId --file pom.xml', jscr.shell[3])
assertEquals('mvn --file \'pom.xml\' versions:set -DnewVersion=1.2.3-20180101010203_testCommitId', jscr.shell[5])
}
@Test
void testVersioningCustomGitUserAndEMail() {
jsr.step.call(script: [commonPipelineEnvironment: jer.env], juStabGitUtils: gitUtils, buildTool: 'maven', gitSshUrl: 'myGitSshUrl', gitUserEMail: 'test@test.com', gitUserName: 'test')
assertEquals ('git -c user.email="test@test.com" -c user.name "test" commit -m \'update version 1.2.3-20180101010203_testCommitId\'', jscr.shell[5])
assertEquals ('git -c user.email="test@test.com" -c user.name "test" commit -m \'update version 1.2.3-20180101010203_testCommitId\'', jscr.shell[7])
}
@Test
@@ -111,4 +133,5 @@ class ArtifactSetVersionTest extends BasePipelineTest {
object.metaClass.static.invokeMethod = helper.getMethodInterceptor()
object.metaClass.methodMissing = helper.getMethodMissingInterceptor()
}
}
@@ -11,10 +11,13 @@ import util.JenkinsShellCallRule
import util.Rules
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertTrue
class MavenArtifactVersioningTest extends BasePipelineTest{
Map dockerParameters
def mavenExecuteScript
def commonPipelineEnvironment
MavenArtifactVersioning av
JenkinsShellCallRule jscr = new JenkinsShellCallRule(this)
@@ -24,7 +27,18 @@ class MavenArtifactVersioningTest extends BasePipelineTest{
public RuleChain ruleChain = Rules.getCommonRules(this).around(jscr).around(thrown).around(new JenkinsReadMavenPomRule(this, 'test/resources/MavenArtifactVersioning'))
@Before
public void init() {
void init() {
dockerParameters = [:]
helper.registerAllowedMethod("dockerExecute", [Map.class, Closure.class],
{ parameters, closure ->
dockerParameters = parameters
closure()
})
mavenExecuteScript = loadScript("mavenExecute.groovy").mavenExecute
commonPipelineEnvironment = loadScript('commonPipelineEnvironment.groovy').commonPipelineEnvironment
prepareObjectInterceptors(this)
}
@@ -33,19 +47,17 @@ class MavenArtifactVersioningTest extends BasePipelineTest{
av = new MavenArtifactVersioning(this, [filePath: 'pom.xml'])
assertEquals('1.2.3', av.getVersion())
av.setVersion('1.2.3-20180101')
assertEquals('mvn versions:set -DnewVersion=1.2.3-20180101 --file pom.xml', jscr.shell[0])
assertEquals('mvn --file \'pom.xml\' versions:set -DnewVersion=1.2.3-20180101', jscr.shell[0])
}
@Test
void testVersioningCustomFilePathSnapshot() {
av = new MavenArtifactVersioning(this, [filePath: 'snapshot/pom.xml'])
assertEquals('1.2.3', av.getVersion())
av.setVersion('1.2.3-20180101')
assertEquals('mvn versions:set -DnewVersion=1.2.3-20180101 --file snapshot/pom.xml', jscr.shell[0])
assertEquals('mvn --file \'snapshot/pom.xml\' versions:set -DnewVersion=1.2.3-20180101', jscr.shell[0])
}
void prepareObjectInterceptors(object) {
object.metaClass.invokeMethod = helper.getMethodInterceptor()
object.metaClass.static.invokeMethod = helper.getMethodInterceptor()