|
| 1 | +/** |
| 2 | + * Provides a simple custom maven settings.xml file to current working directory with Maven Central mirror in the |
| 3 | + * current CES instance. |
| 4 | + * |
| 5 | + * Example call: |
| 6 | + * |
| 7 | + * <pre> |
| 8 | + * nexusCreds = usernamePassword(credentialsId: 'jenkinsNexusServiceUser', passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME') |
| 9 | + * withMavenSettings(nexusCreds, 'cesinstance.stage.host.tld', '/usr/share/maven') { settingsXml -> |
| 10 | + * // do your maven call here |
| 11 | + * mvn "-s ${settingsXml} test" |
| 12 | + * } // settings.xml will be removed automatically |
| 13 | + * </pre> |
| 14 | + * |
| 15 | + * @param nexusCredentials Jenkins credentials which provide USERNAME and PASSWORD to an account which enables Nexus interaction |
| 16 | + * @param cesFQDN the full qualified domain name of the current CES instance, f. i. <code>cesinstance.stage.host.tld</code> |
| 17 | + * @param pathToLocalMavenRepository without the .m2 directory part, f. i. <code>/usr/share/maven</code>. The suffix <code>/.m2/repository</code> will be added automatically. |
| 18 | + */ |
| 19 | +def settings(def nexusCredentials, String cesFQDN, String pathToLocalMavenRepository, Closure closure) { |
| 20 | + echo "write settings.xml to ${pathToLocalMavenRepository}" |
| 21 | + String settingsXml = "settings.xml" |
| 22 | + withCredentials([nexusCredentials]) { |
| 23 | + writeFile file: settingsXml, text: """ |
| 24 | + <settings> |
| 25 | + <localRepository>${pathToLocalMavenRepository}/.m2/repository</localRepository> |
| 26 | + <servers> |
| 27 | + <server> |
| 28 | + <id>${cesFQDN}</id> |
| 29 | + <username>${USERNAME}</username> |
| 30 | + <password>${PASSWORD}</password> |
| 31 | + </server> |
| 32 | + </servers> |
| 33 | + <mirrors> |
| 34 | + <mirror> |
| 35 | + <id>${cesFQDN}</id> |
| 36 | + <name>${cesFQDN} Central Mirror</name> |
| 37 | + <url>https://${cesFQDN}/nexus/repository/itzbundshared/</url> |
| 38 | + <mirrorOf>central</mirrorOf> |
| 39 | + </mirror> |
| 40 | + </mirrors> |
| 41 | + </settings>""" |
| 42 | + } |
| 43 | + |
| 44 | + try { |
| 45 | + closure.call(settingsXml) |
| 46 | + } finally { |
| 47 | + sh "rm -f ${settingsXml}" |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +def mvnWithSettings(def nexusCredentials, String cesFQDN, String mvnCallArgs) { |
| 52 | + def currentHome = env.HOME |
| 53 | + settings(nexusCredentials, cesFQDN, currentHome) { settingsXml -> |
| 54 | + mvn settingsXml, mvnCallArgs |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * This method extracts the Maven 3 installation from Jenkins and calls Maven with the given settings.xml and Maven arguments |
| 60 | + */ |
| 61 | +def mvn(String settingsXml, String mvnCallArgs) { |
| 62 | + def mvnHome = tool 'M3' |
| 63 | + |
| 64 | + mvnWithHome(mvnHome, settingsXml, mvnCallArgs) |
| 65 | +} |
| 66 | + |
| 67 | +def customMvnWithSettings(def nexusCredentials, String cesFQDN, String mvnHome, String pathToLocalMavenRepository, String mvnCallArgs) { |
| 68 | + settings(nexusCredentials, cesFQDN, pathToLocalMavenRepository) { settingsXml -> |
| 69 | + mvnWithHome(mvnHome, settingsXml, mvnCallArgs) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +def mvnWithHome(String mvnHome, String settingsXml, String mvnCallArgs) { |
| 74 | + sh "${mvnHome}/bin/mvn -s ${settingsXml} --batch-mode -V -U -e -Dsurefire.useFile=false ${mvnCallArgs}" |
| 75 | +} |
0 commit comments