Tiven Wang
chevron_rightHCP chevron_rightMaven chevron_rightJava

Setup Java Maven Project for HANA Cloud Platform

Wang Tiven February 23, 2017
425 favorite favorites
bookmark bookmark
share share

The project codes for this article can be downloaded from Github.

Development Environment

  • JDK 1.7
  • Maven 3.+
  • Eclipse with Maven plugins

http://download.eclipse.org/technology/m2e/releases/

Maven Project Initialization

You will need somewhere for your project to reside, create a directory somewhere and start a shell in that directory. On your command line, execute the following Maven goal:

mvn -B archetype:generate \
  -DarchetypeGroupId=org.apache.maven.archetypes \
  -DgroupId=com.mycompany.app \
  -DartifactId=my-app

For our project using

mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.sap.c4c -DartifactId=wechat

cd wechat

Just execute the compile command to check the project

mvn compile

Add Servlet

In order to run in web application server, the project need web servlet to process http request and add the Java Web API dependencies.

Add Web API Dependencies

Add the Java Web API by the SAP Cloud Java Web API dependence.

<dependencies>
  <!-- The SAP HANA Cloud Platform Java Web Tomcat 7 API -->
  <dependency>
      <groupId>com.sap.cloud</groupId>
      <artifactId>neo-java-web-api</artifactId>
      <version>2.65.5</version>
      <scope>provided</scope>
  </dependency>
  ...
</dependencies>

Create Servlet

Create a Java servlet class to print a simple words “Hello World!” for http response.

package com.sap.c4c.wechat.web;

import java.io.IOException;

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

/**
 * Servlet implementing simplest possible hello world application for SAP HANA Cloud Platform.
 */
public class MessageServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /** {@inheritDoc} */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().println("<p>Hello World!</p>");
    }
}

Add Web Content

Add the Web Content files in folder WebContent/ and the servlet configurations in file WEB-INF/web.xml

<!-- Main sample servlet mapped to / so that the integration test harness can detect readiness (generic for all samples) -->
<servlet>
    <servlet-name>MessageServlet</servlet-name>
    <servlet-class>com.sap.c4c.wechat.web.MessageServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>MessageServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Maven Build Plugins

In order to package Java war artifact, you need add the maven war and compiler plugins to rewrite the default configurations.

<build>
  <plugins>
      <!-- Map the Eclipse file system layout to the Maven plug-ins -->
      <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>2.1</version>
          <configuration>
              <warName>${project.artifactId}</warName>
              <warSourceDirectory>WebContent</warSourceDirectory>
              <archive>
                  <manifestFile>WebContent/META-INF/MANIFEST.MF</manifestFile>
              </archive>
          </configuration>
      </plugin>

      <!-- Set compiler to officially supported JDK version for the given SAP HANA Cloud Platform SDK -->
      <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.5.1</version>
          <configuration>
              <source>1.7</source>
              <target>1.7</target>
              <showDeprecation>true</showDeprecation>
              <showWarnings>true</showWarnings>
          </configuration>
      </plugin>
    </plugins>
</build>

Test Locally

Tomcat Maven Plugin

The Tomcat7 Maven Plugin provides goals to manipulate WAR projects within the Tomcat servlet container version 7.x.

<plugins>
  ...
  <plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.0</version>
    <configuration>
      ...
    </configuration>
  </plugin>
  ...
</plugins>

Run tomcat7 container using mvn tomcat7:run then access the project by http://localhost:8080/wechat/

Build and Package

In maven command or the eclipse maven tools to execute the maven goal mvn package to build and package the project to Java war artifact. Once completed the maven goal, you can find the war artifact in /target/wechat.war

Deploy

Deploy Manually

Upload or update the Java war artifact on HANA Cloud with the cockpit.

Deploy from Eclipse

Deploying on the Cloud from Eclipse IDE

Deploy with Console Client

Deploying on the Cloud with the Console Client

Similar Posts

Comments

Back to Top