Skip to content

Commit cc9b454

Browse files
committed
Merge pull request #400 from daspilker/maven-context
Refactoring of MavenContext and LocalRepositoryLocation
2 parents 8bfeba6 + 822cf6a commit cc9b454

File tree

11 files changed

+165
-62
lines changed

11 files changed

+165
-62
lines changed

docs/Home.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Have a look at the [Jenkins Job DSL Gradle example](https://github.com/sheehan/j
1717

1818
## Release Notes
1919
* 1.31 (unreleased)
20+
* The enum argument of `localRepository` for the Maven job and context has changed, see [[Migration]]
2021
* 1.30 (March 08 2015)
2122
* Added support for [Custom Tools Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Custom+Tools+Plugin)
2223
* Added support for [Flaky Test Handler Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Flaky+Test+Handler+Plugin)

docs/Job-reference.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -757,10 +757,12 @@ Refers to the pull down box in the UI to select which installation of Maven to u
757757
localRepository(LocalRepositoryLocation location)
758758
```
759759

760-
LocalRepositoryLocation is available as two enums, injected into the script. Their names are LocalToExecutor and LocalToWorkspace, they can be used like this:
760+
Possible values for `localRepository` are `LocalRepositoryLocation.LOCAL_TO_WORKSPACE` and
761+
`LocalRepositoryLocation.LOCAL_TO_EXECUTOR`. The `LocalToWorkspace` and `LocalToExecutor` values are deprecated since
762+
1.31.
761763

762764
```groovy
763-
localRepository(LocalToWorkspace)
765+
localRepository(LocalRepositoryLocation.LOCAL_TO_WORKSPACE)
764766
```
765767

766768
(Since 1.17)
@@ -2290,7 +2292,7 @@ maven { // since 1.20; all methods
22902292
goals(String goals) // the goals to run, multiple calls will be accumulated
22912293
rootPOM(String fileName) // path to the POM
22922294
mavenOpts(String options) // JVM options, multiple calls will be accumulated
2293-
localRepository(LocalRepositoryLocation location) // can be either LocalToWorkspace or LocalToExecutor (default)
2295+
localRepository(LocalRepositoryLocation location) // defaults to LocalRepositoryLocation.LOCAL_TO_EXECUTOR
22942296
mavenInstallation(String name) // name of the Maven installation to use
22952297
properties(Map properties) // since 1.21; add (system)-properties
22962298
property(String key, String value) // since 1.21; add a (system)-property
@@ -2303,6 +2305,10 @@ Runs Apache Maven. Configure block is handed `hudson.tasks.Maven`. The
23032305
[Config File Provider Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Config+File+Provider+Plugin) is required to
23042306
use `providedSettings`.
23052307

2308+
Possible values for `localRepository` are `LocalRepositoryLocation.LOCAL_TO_WORKSPACE` and
2309+
`LocalRepositoryLocation.LOCAL_TO_EXECUTOR`. The `LocalToWorkspace` and `LocalToExecutor` values are deprecated since
2310+
1.31.
2311+
23062312
Examples:
23072313

23082314
```groovy

docs/Migration.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
1+
## Migrating to 1.31
2+
3+
### Local Maven Repository Location
4+
5+
The `localRepository` method with a `javaposse.jobdsl.dsl.helpers.common.MavenContext.LocalRepositoryLocation` argument
6+
has been [[deprecated|Deprecation-Policy]] and replaced by a method with a
7+
`javaposse.jobdsl.dsl.helpers.LocalRepositoryLocation` argument. The values of the enum have been renamed from camel
8+
case to upper case to follow the naming convention for enum values. The new enum is implicitly imported, but not with
9+
star import as the new deprecated variant.
10+
11+
DSL prior to 1.31
12+
```groovy
13+
mavenJob {
14+
localRepository(LocalToWorkspace)
15+
}
16+
job {
17+
steps {
18+
maven {
19+
localRepository(LocalToWorkspace)
20+
}
21+
}
22+
}
23+
```
24+
25+
DSL since 1.31
26+
```groovy
27+
mavenJob {
28+
localRepository(LocalRepositoryLocation.LOCAL_TO_WORKSPACE)
29+
}
30+
job {
31+
steps {
32+
maven {
33+
localRepository(LocalRepositoryLocation.LOCAL_TO_WORKSPACE)
34+
}
35+
}
36+
}
37+
```
38+
139
## Migrating to 1.30
240

341
### Factory and Name Methods

job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/DslScriptLoader.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ private static CompilerConfiguration createCompilerConfiguration(JobManagement j
194194
icz.addImports("javaposse.jobdsl.dsl.views.jobfilter.RegexMatchValue");
195195
icz.addImports("javaposse.jobdsl.dsl.helpers.scm.SvnCheckoutStrategy");
196196
icz.addImports("javaposse.jobdsl.dsl.helpers.scm.SvnDepth");
197+
icz.addImports("javaposse.jobdsl.dsl.helpers.LocalRepositoryLocation");
197198
config.addCompilationCustomizers(icz);
198199

199200
GrabDeprecationTransformation grabDeprecationTransformation = new GrabDeprecationTransformation(jobManagement);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package javaposse.jobdsl.dsl.helpers
2+
3+
enum LocalRepositoryLocation {
4+
LOCAL_TO_EXECUTOR('hudson.maven.local_repo.PerExecutorLocalRepositoryLocator'),
5+
LOCAL_TO_WORKSPACE('hudson.maven.local_repo.PerJobLocalRepositoryLocator')
6+
7+
final String type
8+
9+
LocalRepositoryLocation(String type) {
10+
this.type = type
11+
}
12+
}

job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/common/MavenContext.groovy

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,17 @@ package javaposse.jobdsl.dsl.helpers.common
22

33
import javaposse.jobdsl.dsl.Context
44

5+
@Deprecated
56
interface MavenContext extends Context {
6-
/**
7-
* Specifies the path to the root POM.
8-
* @param rootPOM path to the root POM
9-
*/
10-
void rootPOM(String rootPOM)
11-
12-
/**
13-
* Specifies the goals to execute.
14-
* @param goals the goals to execute
15-
*/
16-
void goals(String goals)
17-
18-
/**
19-
* Specifies the JVM options needed when launching Maven as an external process.
20-
* @param mavenOpts JVM options needed when launching Maven
21-
*/
22-
void mavenOpts(String mavenOpts)
23-
24-
/**
25-
* <localRepository class="hudson.maven.local_repo.PerJobLocalRepositoryLocator"/>
26-
*
27-
* Set to use isolated local Maven repositories.
28-
* @param location the local repository to use for isolation
29-
*/
30-
void localRepository(LocalRepositoryLocation location)
31-
32-
/**
33-
* Specifies the Maven installation for executing this step or job
34-
* @param name name of the Maven installation to use
35-
*/
36-
void mavenInstallation(String name)
37-
38-
/**
39-
* Specifies the managed Maven settings to be used.
40-
* @param settings name of the managed Maven settings
41-
*/
42-
void providedSettings(String settings)
43-
7+
@Deprecated
448
enum LocalRepositoryLocation {
45-
LocalToExecutor('hudson.maven.local_repo.PerExecutorLocalRepositoryLocator'),
46-
LocalToWorkspace('hudson.maven.local_repo.PerJobLocalRepositoryLocator')
9+
LocalToExecutor(javaposse.jobdsl.dsl.helpers.LocalRepositoryLocation.LOCAL_TO_EXECUTOR),
10+
LocalToWorkspace(javaposse.jobdsl.dsl.helpers.LocalRepositoryLocation.LOCAL_TO_WORKSPACE)
4711

48-
String type
12+
final javaposse.jobdsl.dsl.helpers.LocalRepositoryLocation location
4913

50-
LocalRepositoryLocation(String type) {
51-
this.type = type
14+
LocalRepositoryLocation(javaposse.jobdsl.dsl.helpers.LocalRepositoryLocation location) {
15+
this.location = location
5216
}
5317
}
5418
}

job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/step/MavenContext.groovy

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package javaposse.jobdsl.dsl.helpers.step
22

33
import com.google.common.base.Preconditions
44
import javaposse.jobdsl.dsl.ConfigFileType
5+
import javaposse.jobdsl.dsl.Context
56
import javaposse.jobdsl.dsl.JobManagement
6-
import javaposse.jobdsl.dsl.helpers.common.MavenContext.LocalRepositoryLocation
7+
import javaposse.jobdsl.dsl.helpers.LocalRepositoryLocation
78

8-
class MavenContext implements javaposse.jobdsl.dsl.helpers.common.MavenContext {
9+
class MavenContext implements Context {
910
private final JobManagement jobManagement
1011

1112
String rootPOM
@@ -21,32 +22,56 @@ class MavenContext implements javaposse.jobdsl.dsl.helpers.common.MavenContext {
2122
this.jobManagement = jobManagement
2223
}
2324

24-
@Override
25+
/**
26+
* Specifies the path to the root POM.
27+
* @param rootPOM path to the root POM
28+
*/
2529
void rootPOM(String rootPOM) {
2630
this.rootPOM = rootPOM
2731
}
2832

29-
@Override
33+
/**
34+
* Specifies the goals to execute.
35+
* @param goals the goals to execute
36+
*/
3037
void goals(String goals) {
3138
this.goals << goals
3239
}
3340

34-
@Override
41+
/**
42+
* Specifies the JVM options needed when launching Maven as an external process.
43+
* @param mavenOpts JVM options needed when launching Maven
44+
*/
3545
void mavenOpts(String mavenOpts) {
3646
this.mavenOpts << mavenOpts
3747
}
3848

39-
@Override
49+
@Deprecated
50+
void localRepository(javaposse.jobdsl.dsl.helpers.common.MavenContext.LocalRepositoryLocation location) {
51+
jobManagement.logDeprecationWarning()
52+
this.localRepositoryLocation = location.location
53+
}
54+
55+
/**
56+
* Set to use isolated local Maven repositories.
57+
* @param location the local repository to use for isolation
58+
*/
4059
void localRepository(LocalRepositoryLocation location) {
4160
this.localRepositoryLocation = location
4261
}
4362

44-
@Override
63+
/**
64+
* Specifies the Maven installation for executing this step or job
65+
* @param name name of the Maven installation to use
66+
*/
4567
void mavenInstallation(String name) {
4668
this.mavenInstallation = name
4769
}
4870

49-
@Override
71+
/**
72+
* Specifies the managed Maven settings to be used.
73+
* @param settings name of the managed Maven settings
74+
*/
5075
void providedSettings(String settingsName) {
5176
String settingsId = jobManagement.getConfigFileId(ConfigFileType.MavenSettings, settingsName)
5277
Preconditions.checkNotNull settingsId, "Managed Maven settings with name '${settingsName}' not found"

job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/step/StepContext.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import javaposse.jobdsl.dsl.WithXmlAction
1010
import javaposse.jobdsl.dsl.helpers.common.DownstreamContext
1111

1212
import static com.google.common.base.Strings.isNullOrEmpty
13-
import static javaposse.jobdsl.dsl.helpers.common.MavenContext.LocalRepositoryLocation.LocalToWorkspace
13+
import static javaposse.jobdsl.dsl.helpers.LocalRepositoryLocation.LOCAL_TO_WORKSPACE
1414

1515
class StepContext implements Context {
1616
private static final List<String> VALID_CONTINUATION_CONDITIONS = ['SUCCESSFUL', 'UNSTABLE', 'COMPLETED']
@@ -369,7 +369,7 @@ class StepContext implements Context {
369369
if (mavenContext.rootPOM) {
370370
pom mavenContext.rootPOM
371371
}
372-
usePrivateRepository mavenContext.localRepositoryLocation == LocalToWorkspace ? 'true' : 'false'
372+
usePrivateRepository mavenContext.localRepositoryLocation == LOCAL_TO_WORKSPACE
373373
if (mavenContext.providedSettingsId) {
374374
settings(class: 'org.jenkinsci.plugins.configfiles.maven.job.MvnSettingsProvider') {
375375
settingsConfigId(mavenContext.providedSettingsId)

job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/jobs/MavenJob.groovy

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import javaposse.jobdsl.dsl.DslContext
66
import javaposse.jobdsl.dsl.Job
77
import javaposse.jobdsl.dsl.JobManagement
88
import javaposse.jobdsl.dsl.WithXmlAction
9+
import javaposse.jobdsl.dsl.helpers.LocalRepositoryLocation
910
import javaposse.jobdsl.dsl.helpers.common.MavenContext
1011
import javaposse.jobdsl.dsl.helpers.step.StepContext
1112
import javaposse.jobdsl.dsl.helpers.triggers.MavenTriggerContext
@@ -132,7 +133,22 @@ class MavenJob extends Job {
132133
* Set to use isolated local Maven repositories.
133134
* @param location the local repository to use for isolation
134135
*/
136+
@Deprecated
135137
void localRepository(MavenContext.LocalRepositoryLocation location) {
138+
jobManagement.logDeprecationWarning()
139+
140+
Preconditions.checkNotNull(location, 'localRepository can not be null')
141+
142+
localRepository(location.location)
143+
}
144+
145+
/**
146+
* <localRepository class="hudson.maven.local_repo.PerJobLocalRepositoryLocator"/>
147+
*
148+
* Set to use isolated local Maven repositories.
149+
* @param location the local repository to use for isolation
150+
*/
151+
void localRepository(LocalRepositoryLocation location) {
136152
Preconditions.checkNotNull(location, 'localRepository can not be null')
137153

138154
withXmlActions << WithXmlAction.create { Node project ->

job-dsl-core/src/test/groovy/javaposse/jobdsl/dsl/helpers/step/StepContextSpec.groovy

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package javaposse.jobdsl.dsl.helpers.step
33
import hudson.util.VersionNumber
44
import javaposse.jobdsl.dsl.ConfigFileType
55
import javaposse.jobdsl.dsl.JobManagement
6+
import javaposse.jobdsl.dsl.helpers.LocalRepositoryLocation
67
import spock.lang.Specification
78
import spock.lang.Unroll
89

@@ -299,7 +300,7 @@ class StepContextSpec extends Specification {
299300
goals('install')
300301
mavenOpts('-Xms256m')
301302
mavenOpts('-Xmx512m')
302-
localRepository(LocalToWorkspace)
303+
localRepository(LocalRepositoryLocation.LOCAL_TO_WORKSPACE)
303304
mavenInstallation('Maven 3.0.5')
304305
properties skipTests: true, other: 'some'
305306
property 'evenAnother', 'One'
@@ -317,7 +318,7 @@ class StepContextSpec extends Specification {
317318
mavenStep.targets[0].value() == 'clean install'
318319
mavenStep.pom[0].value() == 'module-a/pom.xml'
319320
mavenStep.jvmOptions[0].value() == '-Xms256m -Xmx512m'
320-
mavenStep.usePrivateRepository[0].value() == 'true'
321+
mavenStep.usePrivateRepository[0].value() == true
321322
mavenStep.mavenName[0].value() == 'Maven 3.0.5'
322323
mavenStep.settingsConfigId[0].value() == 'foo-bar'
323324
mavenStep.properties[0].value() == 'skipTests=true\nother=some\nevenAnother=One'
@@ -336,10 +337,24 @@ class StepContextSpec extends Specification {
336337
mavenStep.children().size() == 4
337338
mavenStep.targets[0].value() == ''
338339
mavenStep.jvmOptions[0].value() == ''
339-
mavenStep.usePrivateRepository[0].value() == 'false'
340+
mavenStep.usePrivateRepository[0].value() == false
340341
mavenStep.mavenName[0].value() == '(Default)'
341342
}
342343

344+
def 'call maven method with deprecated options'() {
345+
when:
346+
context.maven {
347+
localRepository(LocalToWorkspace)
348+
}
349+
350+
then:
351+
with(context.stepNodes[0]) {
352+
name() == 'hudson.tasks.Maven'
353+
children().size() == 4
354+
usePrivateRepository[0].value() == true
355+
}
356+
}
357+
343358
def 'call maven method with unknown provided settings'() {
344359
setup:
345360
String settingsName = 'lalala'
@@ -373,7 +388,7 @@ class StepContextSpec extends Specification {
373388
children().size() == 5
374389
targets[0].value() == ''
375390
jvmOptions[0].value() == ''
376-
usePrivateRepository[0].value() == 'false'
391+
usePrivateRepository[0].value() == false
377392
mavenName[0].value() == '(Default)'
378393
settings[0].attribute('class') == 'org.jenkinsci.plugins.configfiles.maven.job.MvnSettingsProvider'
379394
settings[0].children().size() == 1

0 commit comments

Comments
 (0)