4.4 Script a build in Maven

Learning Objectives

By the end of this lesson you will be able to:

EXERCISE: Run a Maven build

  1. From the AWSCLI home directory, download and unzip Maven
    $ cd /home/ec2-user
    				  
    $ sudo wget http://apache.etoak.com/maven/binaries/apache-maven-3.0.4-bin.zip
    				  
    $ unzip apache-maven-3.0.4-bin.zip 
    				  
  2. Go to the build directory, make a directory called maven and change to this directory.
    $ cd /home/ec2-user/continuous_integration_example/software/build
    				  
    $ mkdir maven
    				  
    $ cd maven
    				  
  3. Type mvn from the command line
    /home/ec2-user/apache-maven-3.0.4/bin/mvn
    You should receive a message like this:
    [ERROR] No goals have been specified for this build.
  4. From /home/ec2-user/continuous_integration_example/software/build/maven, do a git clone.
    $ git clone git@github.com:student29/devcenter-embedded-tomcat.git
    		          
  5. Change directory to devcenter-embedded-tomcat
    $ cd /home/ec2-user/continuous_integration_example/software/build/maven/devcenter-embedded-tomcat
    				  
  6. Create a Maven build file called mypom.xml
    $ sudo vim mypom.xml
    				  
  7. Enter the Maven contents below in the mypom.xml (Originally from https://github.com/heroku/devcenter-embedded-tomcat/blob/master/pom.xml)
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.heroku.sample</groupId>
    <artifactId>embeddedTomcatSample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>embeddedTomcatSample Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
      <dependency>
          <groupId>org.apache.tomcat.embed</groupId>
          <artifactId>tomcat-embed-core</artifactId>
          <version>7.0.22</version>
      </dependency>
      <dependency>
          <groupId>org.apache.tomcat.embed</groupId>
          <artifactId>tomcat-embed-logging-juli</artifactId>
          <version>7.0.22</version>
      </dependency>
      <dependency>
          <groupId>org.apache.tomcat.embed</groupId>
          <artifactId>tomcat-embed-jasper</artifactId>
          <version>7.0.22</version>
      </dependency>
      <dependency>
          <groupId>org.apache.tomcat</groupId>
          <artifactId>tomcat-jasper</artifactId>
          <version>7.0.22</version>
      </dependency>
      <dependency>
          <groupId>org.apache.tomcat</groupId>
          <artifactId>tomcat-jasper-el</artifactId>
          <version>7.0.22</version>
      </dependency>
      <dependency>
          <groupId>org.apache.tomcat</groupId>
          <artifactId>tomcat-jsp-api</artifactId>
          <version>7.0.22</version>
      </dependency>
    </dependencies>
    <build>
      <finalName>embeddedTomcatSample</finalName>
      <plugins>
          <plugin>
              <groupId>org.codehaus.mojo</groupId>
              <artifactId>appassembler-maven-plugin</artifactId>
              <version>1.1.1</version>
              <configuration>
                  <assembleDirectory>target</assembleDirectory>
                  <programs>
                      <program>
                          <mainClass>launch.Main</mainClass>
                          <name>webapp</name>
                      </program>
                  </programs>
              </configuration>
              <executions>
                  <execution>
                      <phase>package</phase>
                      <goals>
                          <goal>assemble</goal>
                      </goals>
                  </execution>
              </executions>
          </plugin>
      </plugins>
    </build>
    </project>
    
  8. cd /home/ec2-user/continuous_integration_example/software/build/maven/devcenter-embedded-tomcat/src/main/java/launch
  9. Open port 8180 in EC2 Security Group (e.g. AWSCLI00-Ec2SecurityGroup...)
  10. Type mvn from the command line
    /home/ec2-user/apache-maven-3.0.4/bin/mvn package
  11. Start the web application
    sh target/bin/webapp
  12. Run the web application. Open your web browser to the following URL (your DNS will be different): http://ec2-107-21-81-107.compute-1.amazonaws.com:8180/