1- import groovy.transform.Field
2- import java.nio.file.Paths
1+ import org.example.JenkinsPlugins
2+
33
44plugins {
55 id ' groovy'
@@ -10,6 +10,8 @@ plugins {
1010group = ' org.example'
1111version = ' 1.0-SNAPSHOT'
1212
13+ JenkinsPlugins . create(project)
14+
1315java {
1416 toolchain {
1517 setLanguageVersion JavaLanguageVersion . of(11 )
@@ -41,20 +43,10 @@ configurations {
4143}
4244
4345dependencies {
44- implementationDependencies. each { String dependency ->
45- implementation dependency
46- }
47- testImplementationDependencies. each { String dependency ->
48- testImplementation dependency
49- }
50- jenkinsPlugins. values(). each { String pluginGAV ->
51- testPlugins pluginGAV
52- }
53-
54- /* JUnit5 dependencies */
55- testImplementation platform(' org.junit:junit-bom:5.9.2' )
56- testImplementation ' org.junit.jupiter:junit-jupiter'
57- testRuntimeOnly ' org.junit.vintage:junit-vintage-engine'
46+ implementationDependencies delegate
47+ testImplementationDependencies delegate
48+ jenkinsPluginDependencies delegate
49+ junit5Dependencies delegate
5850}
5951
6052idea {
@@ -99,36 +91,22 @@ tasks.register('resolveTestPlugins', Copy) {
9991 final String destinationDir = " ${ sourceSets.test.output.resourcesDir} /test-dependencies"
10092
10193 doFirst {
102- configurations. testPlugins
94+ Set< ResolvedArtifact > resolvedArtifacts = configurations. testPlugins
10395 .resolvedConfiguration
10496 .resolvedArtifacts
105- .each { ResolvedArtifact artifact ->
106- if (artifact. extension ==~ ' [hj]pi' ) {
107- addPluginMapping artifact
108- }
109- }
97+ JenkinsPlugins . get(). registerResolvedPlugins(resolvedArtifacts)
11098 }
11199
112100 from configurations. testPlugins {
113101 rename { String filePath ->
114- getPluginsMapping(
115- Paths . get(filePath). getFileName(). toString()
116- )
117- .fileName
102+ JenkinsPlugins . get(). pluginFileNameForJenkinsInstance(filePath)
118103 }
119104 }
120105 include ' *.hpi' , ' *.jpi'
121106 into destinationDir
122107
123108 doLast {
124- new File (destinationDir, ' index' ). setText(
125- pluginsMapping
126- .values()
127- .collect { JenkinsPlugin plugin -> plugin. name }
128- .toSorted()
129- .join(' \n ' ),
130- ' UTF-8'
131- )
109+ JenkinsPlugins . get(). generatePluginsIndex(" ${ destinationDir} /index" )
132110 }
133111}
134112
@@ -140,89 +118,64 @@ tasks.named('wrapper') {
140118
141119/* Helpers */
142120
143- Map<String , String > getJenkinsPlugins () {
144- property(" jenkinsPluginVersions.$jenkinsVersion " )
145- .tokenize()
146- .collate(2 )
147- .findAll { def (String module, String version) = it
148- ! module. startsWith(' #' )
149- }
150- .collectEntries { def (String module, String version) = it
151- [(module. replaceAll(/ :$/ , ' ' )): version]
152- }
153- .collectEntries { String module , version ->
154- String pluginName = module. split(' :' ). last()
155- [(pluginName): " $module :$version " . toString()]
156- }
157- }
158-
159- @Field
160- final Map<String , JenkinsPlugin > pluginsMapping = [:]
161-
162- void addPluginMapping (ResolvedArtifact artifact ) {
163- pluginsMapping[artifact. file. name] = new JenkinsPlugin (artifact. name, artifact. extension)
164- }
165-
166- Map<String , JenkinsPlugin > getPluginsMapping () {
167- pluginsMapping
168- }
169-
170- JenkinsPlugin getPluginsMapping (String fileName ) {
171- getPluginsMapping()[fileName]
172- }
173-
174- String getTestHarnessVersion () {
175- property(" testHarnessVersion.$jenkinsVersion " )
176- }
177-
178- List<String > getImplementationDependencies () {
179- List<String > result = [
121+ void implementationDependencies (DependencyHandler context ) {
122+ List<String > dependencies = [
180123 " org.codehaus.groovy:groovy-all:2.4.21" ,
181- " org.jenkins-ci.main:jenkins-core:$jenkinsVersion " ,
182- jenkinsPlugins [' job-dsl-core' ],
124+ " org.jenkins-ci.main:jenkins-core:${ JenkinsPlugins.get(). jenkinsVersion} " ,
125+ JenkinsPlugins . get() [' job-dsl-core' ],
183126 ]
184- switch (jenkinsVersion) {
127+ switch (JenkinsPlugins . get() . jenkinsVersion) {
185128 case ' 2.277.4' :
186- result << ' com.cloudbees:groovy-cps:1.22'
129+ dependencies << ' com.cloudbees:groovy-cps:1.22'
187130 break
188131 case ' 2.346.3' :
189- result << jenkinsPlugins [' workflow-cps' ]
132+ dependencies << JenkinsPlugins . get() [' workflow-cps' ]
190133 break
191134 default :
192- throw new IllegalArgumentException (" Unrecognized Jenkins version: $jenkinsVersion " )
135+ throw new IllegalArgumentException (" Unrecognized Jenkins version: ${ JenkinsPlugins.get().jenkinsVersion} " )
136+ }
137+
138+ dependencies. each { String dependency ->
139+ context. implementation dependency
193140 }
194- result
195141}
196142
197- List<String > getTestImplementationDependencies () {
198- List<String > result = [
143+ static void jenkinsPluginDependencies (DependencyHandler context ) {
144+ JenkinsPlugins . get(). each { String dependency ->
145+ context. testPlugins dependency
146+ }
147+ }
148+
149+ static void junit5Dependencies (DependencyHandler context ) {
150+ context. with {
151+ testImplementation platform(' org.junit:junit-bom:5.9.2' )
152+ testImplementation ' org.junit.jupiter:junit-jupiter'
153+ testRuntimeOnly ' org.junit.vintage:junit-vintage-engine'
154+ }
155+ }
156+
157+ static void testImplementationDependencies (DependencyHandler context ) {
158+ List<String > dependencies = [
199159 " com.lesfurets:jenkins-pipeline-unit:1.19" ,
200160 " org.spockframework:spock-core:1.3-groovy-2.4" ,
201- " org.jenkins-ci.main:jenkins-test-harness:${ testHarnessVersion} " ,
202- " org.jenkins-ci.main:jenkins-war:$jenkinsVersion " ,
203- jenkinsPlugins [' job-dsl' ],
204- " ${ jenkinsPlugins ['job-dsl']} @jar" ,
205- " ${ jenkinsPlugins ['structs']} @jar" ,
161+ " org.jenkins-ci.main:jenkins-test-harness:${ JenkinsPlugins.get(). testHarnessVersion} " ,
162+ " org.jenkins-ci.main:jenkins-war:${ JenkinsPlugins.get(). jenkinsVersion} " ,
163+ JenkinsPlugins . get() [' job-dsl' ],
164+ " ${ JenkinsPlugins.get() ['job-dsl']} @jar" ,
165+ " ${ JenkinsPlugins.get() ['structs']} @jar" ,
206166 ]
207- switch (jenkinsVersion) {
167+ switch (JenkinsPlugins . get() . jenkinsVersion) {
208168 case ' 2.277.4' :
209- result << ' cglib:cglib-nodep:2.2.2'
210- result << " ${ jenkinsPlugins ['script-security']} @jar"
169+ dependencies << ' cglib:cglib-nodep:2.2.2'
170+ dependencies << " ${ JenkinsPlugins.get() ['script-security']} @jar"
211171 break
212172 case ' 2.346.3' :
213173 break
214174 default :
215- throw new IllegalArgumentException (" Unrecognized Jenkins version: $jenkinsVersion " )
175+ throw new IllegalArgumentException (" Unrecognized Jenkins version: ${ JenkinsPlugins.get(). jenkinsVersion} " )
216176 }
217- result
218- }
219-
220- class JenkinsPlugin {
221- final String name
222- final String fileName
223177
224- JenkinsPlugin (String name , String extension ) {
225- this . name = name
226- this . fileName = " ${ name} .${ extension} "
178+ dependencies. each { String dependency ->
179+ context. testImplementation dependency
227180 }
228181}
0 commit comments