Setting Up Jenkins on Ubuntu Server From Scratch

  Java, Jenkins, Maven, Nexus

Jenkins test run: Creating a demo web application

Before developers commit code into SCM, they check locally whether it works or not. So, if you are developing a web application, typically you test it with a local installation of your web container, which in our case is Tomcat8. Therefore, make sure you have a local installation of Tomcat to which your IDE can deploy your web application. If everything works with your code locally on your development workstation, commit your code and see our Jenkins service pick up your project and build and deploy it on the Ubuntu server’s Tomcat instance. We will see this working in the next chapter.

With a web container in place, we can now write our first web application and commit it to the SCM (e.g. Subversion). Of course, this web app will have to be a maven project, because otherwise Jenkins wouldn’t know what to do with it. I will leave it up to you to create a web application with your IDE. I suggest using Eclipse WTP with a local Tomcat installation, so you can test you app before committing your code into SCM. The most simple web application would be a “Hello World” servlet using annotations:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class HelloWorldServlet
*/
@WebServlet("/HelloWorldServlet")
public class HelloWorldServlet extends HttpServlet {
   private static final long serialVersionUID = 1L;
   private static final String CONTENT_TYPE = "text/html;charset=UTF-8";
   private static final String MESSAGE = "<!DOCTYPE html><html>" +
      "<head><title>Hello!</title></head>" +
      "<body>Hello World, Tomcat</body>" +
      "</html>";
   @Override
   protected void doGet(HttpServletRequest request, HttpServletResponse response)
                     throws ServletException, IOException {
      response.setContentType(CONTENT_TYPE);
      try (PrintWriter out = response.getWriter()) {
         out.println(MESSAGE);
      }
   }
   /**
   * @see HttpServlet#HttpServlet()
   */
   public HelloWorldServlet() {
      super();
   }

   /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      doGet(request, response);
   }
}

Let’s assume you named your web application ‘MyDynamicWebProject’and created it as a Maven project in your IDE. Then you should have at least a pom.xml file which should look similar to this:

<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/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>MyDynamicWebProject</groupId>
   <artifactId>MyDynamicWebProject</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>war</packaging>
   <dependencies>
      <dependency>
         <groupId>org.apache.tomcat</groupId>
         <artifactId>tomcat-catalina</artifactId>
         <version>8.0.26</version>
      </dependency>
      <dependency>
         <groupId>org.apache.tomcat</groupId>
         <artifactId>tomcat-servlet-api</artifactId>
         <version>8.0.26</version>
      </dependency>
      <dependency>
         <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-api</artifactId>
         <version>8.0.26</version>
      </dependency>
   </dependencies>
   <build>
      <sourceDirectory>src</sourceDirectory>
      <plugins>
         <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
         </plugin>
         <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
               <warSourceDirectory>WebContent</warSourceDirectory>
               <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
         </plugin>
         <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
               <url>http://localhost:8080/manager/text</url>
               <server>tomcat7server</server>
               <path>/${project.artifactId}</path>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>

Please note that we didn’t specify any distibutionManagement settings in the above pom.xml, so we won’t be able to deploy our web app artifact to a remote (Nexus) repository, but that’s not required for this walkthrough anyway.

In order to actually build the app and have it deployed to Tomcat, we must create a run configuration which specifies the maven build goals clean tomcat7:deploy. It is assumed here that you know how to create a run configuration in your IDE. Don’t forget to add the ‘user’ to your maven settings.xml file. See a sample global settings.xml which also matches the tomcat7-maven-plugin server setting of the POM above with the id setting in the server element below:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
          http://maven.apache.org/xsd/settings-1.0.0.xsd">
   <servers>
      <server>
         <id>tomcat7server</id>
         <username>user</username>
         <password>userpsw</password>
      </server>
      <server>
         <id>my-nexus</id>
         <username>deployment</username>
         <password>deployment123</password>
      </server>
   </servers>
</settings>

If everything works as expected, you should see the output “Hello World, Tomcat” in your browser window after entering the URL http://localhost:8080/MyDynamicWebProject/HelloWorldServlet. Don’t forget that in this chapter we were using a local installation of Tomcat. Don’t mistake it with the  Tomcat8 installation on our Ubuntu server, which I described in the previous chapter.

In the pom.xml file, we hard-coded the URL to our local Tomcat instance. If we commit our code, Jenkins will read this POM and try to deploy our app to the same URL.  This doesn’t seem to be a problem because Jenkins will also find a Tomcat instance on its local Ubuntu server. In many cases, though, a local Tomcat instance on a development workstation will listen to another port than a Tomcat instance on a Ubuntu server. For that reason, developers usually use profiles in their maven settings.

LEAVE A COMMENT