Maven

Using Maven Without Editing the pom File (Remote Projects)


Single Project with Multiple Modules


  1. Ensure Maven is installed properly and environment variables and paths are correct.
  2. Create the repository containing the project (System > Project Support > Repositories).

    If creating and configuring a git repository, use the git plugin. You can download the plugin here: http://plugins.urbancode.com/anthill3/plugin/Git. Once downloaded, upload it to Anthill (System > Server > Plugins). Use the git plugin rather than the git integration when creating and configuring repositories and workflows.

  3. Configure Anthill to look for artifacts not found locally (System > Integration > Maven)
  4. Create the repository and set the url to the maven central repository: (http://repo1.maven.org/maven2). # Repeat the process for any repositories that must be used for external dependencies and/or plugins

Edit ~/.m2/settings.xml to be something like:

To use mirrors:

<settings>
  <servers>
    <server>
      <id>anthill</id>
      <username>admin</username>
      <password>admin</password>
    </server>
  </servers>
  <mirrors>
    <mirror>
      <id>anthill</id>
      <name>Maven Repository Manager running on Anthill Pro Server at ${env.AH_WEB_URL}</name>
      <url>${env.AH_WEB_URL}/rest/maven2/${env.AH_BUILD_LIFE_ID}</url>
      <mirrorOf>*</mirrorOf>
    </mirror>
  </mirrors>
</settings>

Make sure IDs match and Maven repositories are configured in AnthillPro as in step 2. All repositories will be mirrored to the AnthillPro server, but you can choose which repositories to mirror in the pom file by using [id], or ![id].

To use profiles:

<servers>
  <server>
    <id>ahp-repo</id>
    <username>admin</username>
    <password>admin</password>
  </server>
  <server>
    <id>ahp-dist</id>
    <username>PasswordIsAuthToken</username>
    <password>${env.AH_AUTH_TOKEN}</password>
  </server>
</servers>
<profiles>
  <profile>
    <id>default</id>
    <repositories>
      <repository>
        <id>central</id>
        <name>Maven Repository Switchboard</name>
        <url>http://repo1.maven.org/maven2</url>
      </repository>
    </repositories>
  </profile>
  <profile>
    <id>ahp-dev</id>
    <repositories>
      <repository>
        <id>ahp-repo</id>
        <name>AHP Maven Repository Mirror</name>
        <url>${env.AH_WEB_URL}/rest/maven2</url>
      </repository>
    </repositories>
  </profile>
  <profile>
    <id>ahp-dist</id>
    <repositories>
      <repository>
        <id>ahp-dist</id>
        <name>AHP Maven Repository Mirror</name>
        <url>${env.AH_WEB_URL}/rest/maven2dist/${env.AH_BUILD_LIFE_ID}</url>
      </repository>
    </repositories>
  </profile>
</profiles>
<activeProfiles>
  <activeProfile>ahp-dist</activeProfile>
</activeProfiles>

Ensure IDs match and any Maven repositories are configured in as in step 2

# Create a single project in AnthillPro.

  1. Use a maven life-cycle model.
  2. Configure a workflow using the repository you set up earlier containing the project source.
  3. Configure a job.

    When configuring the Maven builder, use deploy for the goal and -U, and -P<profileId> (if you are using profiles and not mirrors; only necessary if you do not specify ahp-dist as the active profile in settings.xml), and -DaltDeploymentRepository=<serverId>::default::${env.AH_WEB_URL}/rest/maven2dist/${env.AH_BUILD_LIFE_ID} for the “Maven Properties”

    Note: Arguments/flags are called Maven Properties in AnthillPro.

  4. Make sure your maven repository (~/.m2/repository/) and anthill cache are cleared so you can pull dependencies and artifacts remotely on first run.
  5. Run your workflow.
  6. You should see a newly populated “Maven Projects” folder in your dashboard/administration folder that contains the dependencies/plugins specified in the pom file and the artifact sets for each, and the dependencies and artifacts for the build life of your project

Project for Each Module


  1. Complete steps 1-4 above
  2. Complete step 5 above for every module of the project you wish to build and once for the project as a whole
  3. 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)
  4. 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

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);
  • The same script, but combined with the SVN\Perforce changeset script:
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);