(→Single Project with Multiple Modules)
|
(→Project for Each Module)
|
Line 102: | |||
# Create a maven.groupId property and a maven.artifactId property in each projectâs properties configuration and set the value appropriately (look in the .pom file) | # Create a maven.groupId property and a maven.artifactId property in each projectâs properties configuration and set the value appropriately (look in the .pom file) | ||
# Complete steps 6 and 7 above and you should see artifacts and dependencies in their respective tabs for each build life you run | # Complete steps 6 and 7 above and you should see artifacts and dependencies in their respective tabs for each build life you run | ||
+ | = Read Version From Maven POM = | ||
+ | *In this example, I have configured an early build step to print the maven pom to standard out. For Windows, I use "type pom.xml" and for unix, I use "cat pom.xml". This stamp context script parses the output and puts the contents of the version element in the context. | ||
+ | ==== AHPSCRIPTS-25 ==== | ||
+ | <pre>import com.urbancode.anthill3.domain.singleton.serversettings.*; | ||
+ | import com.urbancode.anthill3.domain.buildlife.*; | ||
+ | import com.urbancode.anthill3.domain.jobtrace.*; | ||
+ | import com.urbancode.anthill3.domain.jobtrace.buildlife.*; | ||
+ | import com.urbancode.anthill3.runtime.scripting.helpers.*; | ||
+ | import com.urbancode.commons.xml.DOMUtils; | ||
+ | private org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger("ContextScript"); | ||
+ | // BUILD_STEP_NAME: Name of the step within the job that should be inspected | ||
+ | String BUILD_STEP_NAME = "Echo POM"; | ||
+ | BuildLifeJobTrace jobTrace = JobTraceLookup.getCurrent(); | ||
+ | StepTrace[] stepTraceArray = jobTrace.getStepTraceArray(); | ||
+ | StepTrace stepTrace = null; | ||
+ | for (int j = 0; j < stepTraceArray.length && stepTrace == null; j++) { | ||
+ | if (stepTraceArray[j].getName().equalsIgnoreCase(BUILD_STEP_NAME)) { | ||
+ | stepTrace = stepTraceArray[j]; | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | String outputLog = LogHelper.getOutput(stepTrace.getCommandTraceArray()[0]); | ||
+ | // The output log contains junk other than the pom. Trim that out. | ||
+ | String docStartFlag = "<project"; | ||
+ | String docEndFlag = "command exit"; | ||
+ | startIndex = outputLog.indexOf(docStartFlag); | ||
+ | endIndex = outputLog.indexOf(docEndFlag); | ||
+ | outputLog = outputLog.substring(startIndex, endIndex); | ||
+ | // Load the POM as an XML doc and get the version value | ||
+ | doc = DOMUtils.loadDocument(outputLog); | ||
+ | topElement = doc.getDocumentElement(); | ||
+ | version = DOMUtils.getFirstChildText(topElement, "version"); | ||
+ | stampContext.put("version", ""+version);</pre> | ||
+ | *The same script, but combined with the SVN\Perforce changeset script: | ||
+ | <pre>import com.urbancode.anthill3.domain.singleton.serversettings.*; | ||
+ | import com.urbancode.anthill3.domain.buildlife.*; | ||
+ | import com.urbancode.anthill3.domain.jobtrace.*; | ||
+ | import com.urbancode.anthill3.domain.jobtrace.buildlife.*; | ||
+ | import com.urbancode.anthill3.runtime.scripting.helpers.*; | ||
+ | import com.urbancode.commons.xml.DOMUtils; | ||
+ | import com.urbancode.vcsdriver3.*; | ||
+ | private org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger("ContextScript"); | ||
+ | // BUILD_STEP_NAME: Name of the step within the job that should be inspected | ||
+ | String BUILD_STEP_NAME = "Echo POM"; | ||
+ | // String COMMAND_NAME = "shell-command"; | ||
+ | BuildLifeJobTrace jobTrace = JobTraceLookup.getCurrent(); | ||
+ | StepTrace[] stepTraceArray = jobTrace.getStepTraceArray(); | ||
+ | StepTrace stepTrace = null; | ||
+ | for (int j = 0; j < stepTraceArray.length && stepTrace == null; j++) { | ||
+ | if (stepTraceArray[j].getName().equalsIgnoreCase(BUILD_STEP_NAME)) { | ||
+ | stepTrace = stepTraceArray[j]; | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | String outputLog = LogHelper.getOutput(stepTrace.getCommandTraceArray()[0]); | ||
+ | // The output log contains junk other than the pom. Trim that out. | ||
+ | String docStartFlag = "<project"; | ||
+ | String docEndFlag = "command exit"; | ||
+ | startIndex = outputLog.indexOf(docStartFlag); | ||
+ | endIndex = outputLog.indexOf(docEndFlag); | ||
+ | outputLog = outputLog.substring(startIndex, endIndex); | ||
+ | // Load the POM as an XML doc and get the version value | ||
+ | doc = DOMUtils.loadDocument(outputLog); | ||
+ | topElement = doc.getDocumentElement(); | ||
+ | version = DOMUtils.getFirstChildText(topElement, "version"); | ||
+ | // Great, now let's dig out the SVN or Perforce changeset | ||
+ | int getMaxChangeSet(BuildLife life) { | ||
+ | int result = 0; | ||
+ | ChangeLog[] changelogArray = ChangeLogHelper.getChangeLogArray(life); | ||
+ | for (int i = 0; i < changelogArray.length; i++) { | ||
+ | ChangeSet[] changesetArray = changelogArray[i].getChangeSetArray(); | ||
+ | for (int j = 0; j < changesetArray.length; j++) { | ||
+ | ChangeSet changeset = changesetArray[j]; | ||
+ | id = changeset.getId(); | ||
+ | // edit out the "r" character for svn | ||
+ | if (id.startsWith("r")) { | ||
+ | id = id.substring(1); | ||
+ | } | ||
+ | int num = (new Integer(id.trim())).intValue(); | ||
+ | if (num > result) { | ||
+ | result = num; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | return result; | ||
+ | } | ||
+ | // If there is no changelog, look up the previous build | ||
+ | // and take the highest number from that (if present, else keep searching). | ||
+ | int highestChangeset = 0; | ||
+ | BuildLife life = BuildLifeLookup.getCurrent(); | ||
+ | while(highestChangeset == 0 && life != null) { | ||
+ | highestChangeset = getMaxChangeSet(life); | ||
+ | life = life.getPrevBuildLife(); | ||
+ | } | ||
+ | stampContext.put("version", ""+version); | ||
+ | stampContext.put("changeset", ""+highestChangeset);</pre> |