Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
nic_botha2
Explorer

Overview

Maven is a very helpful tool when it comes to developing Java applications.  Maven can be run from within an IDE but I sometimes prefer the command line.  Unfortunately for some reason the Maven commands never stick in my long term memory and I find myself constantly looking up how to do things.  Therefor I have a cheat sheet that I use it for quick reference which saved me a lot of time in the past. 

The topics covered touch the most frequent commands I use in Maven and the general flow is something like

  1. Create the project structure
  2. Do development
  3. Deploy to a local server
  4. Perform a release


Minor edit - updated release flow.



TaskMaven cmd
List archtypes

mvn archetype:generate

Multi projectmvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=pom-root -DarchetypeVersion=RELEASE
Simple Java projectmvn archetype:generate -DgroupId=[groupId] -DartifactId=[artifactId] -DarchetypeArtifactId=maven-archetype-quickstart -e
Web projectmvn archetype:generate -DgroupId=[groupId] -DartifactId=[artifactId] -DarchetypeArtifactId=maven-archetype-webapp
Olingo projectmvn archetype:generate -DinteractiveMode=false -DgroupId=au.project -DartifactId=odata -DarchetypeGroupId=org.apache.olingo  -DarchetypeArtifactId=olingo-odata2-sample-cars-annotation-archetype -DarchetypeVersion=2.0.0
Required Eclipse filesmvn eclipse:eclipse
Remove Eclipse filesmvn eclipse:clean
Local build and installmvn clean install
Create local HCP servermvn neo-java-web:install-local
Deploy to local HCP servermvn neo-java-web:deploy-local
Start local HCP servermvn neo-java-web:start-local
Stop local HCP servermvn neo-java-web:stop-local
Maven releasemvn release:prepare
Maven release cleanupmvn release:clean

Project creation

Maven does have a few very useful archetypes that will help to quickly create the structure for your project.

To list them run


mvn archetype:generate

Multi project structure

One design choice could be to break up the project structure along the lines of the application layers.  For this you would need a root project.


mvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=pom-root -DarchetypeVersion=RELEASE

Running the above creates a project containing the root pom file.


C:.
└───project-root
        pom.xml

Simple java project

To create a basic Java project structure.  If you use this in a multi project structure go to the project root directory.


mvn archetype:generate -DgroupId=[groupId] -DartifactId=[artifactId] -DarchetypeArtifactId=maven-archetype-quickstart -e

Running above gives you the following project structure assuming you chose "model" as the artifactId.


C:\
└───project-root
    │   pom.xml
    │
    └───model
        │   pom.xml
        │
        └───src
            ├───main
            │   └───java
            │       └───au
            │           └───project
            │                   App.java
            │
            └───test
                └───java
                    └───au
                        └───project
                                AppTest.java

Web project

To create a simple web project.


mvn archetype:generate -DgroupId=[groupId] -DartifactId=[artifactId] -DarchetypeArtifactId=maven-archetype-webapp

Running above gives you the following project structure assuming you chose "web" as the artifactId.


C:\
└───project-root
    │   pom.xml
    │
    └───web
        │   pom.xml
        │
        └───src
            └───main
                ├───resources
                └───webapp
                    │   index.jsp
                    │
                    └───WEB-INF
                            web.xml

Olingo project

There are also a lot of archetypes to get you going for specific scenarios. Example to create an odata web project using Apache Olingo


mvn archetype:generate -DinteractiveMode=false -DgroupId=au.project -DartifactId=odata -DarchetypeGroupId=org.apache.olingo  -DarchetypeArtifactId=olingo-odata2-sample-cars-annotation-archetype -DarchetypeVersion=2.0.0

Running the above gives you the following web project structure.


C:\
└───project-root
    │   pom.xml
    │
    └───odata
        │   pom.xml
        │
        └───src
            └───main
                ├───java
                │   └───org
                │       └───apache
                │           └───olingo
                │               └───sample
                │                   └───annotation
                │                       ├───model
                │                       │       Address.java
                │                       │       Car.java
                │                       │       Driver.java
                │                       │       Manufacturer.java
                │                       │
                │                       ├───processor
                │                       │       AnnotationSampleServiceFactory.java
                │                       │
                │                       └───util
                │                               AnnotationSampleDataGenerator.java
                │
                ├───resources
                │       log4j.xml
                │
                └───webapp
                    │   index.jsp
                    │
                    └───WEB-INF
                            web.xml


IDE setup and build projects

Now that you have created your project structure you can start development.

Eclipse project

Usually you can just import the Maven project into Eclipse but let's say you want to generate the Eclipse project manually or you want to delete the Eclipse project files (.settings, .project, .classpath).

Create


mvn eclipse:eclipse

Clean


mvn eclipse:clean

Build

It might be worthwhile to read through the and understand the Maven build lifecycle.  On your development machine you will usually do the following to build and install locally.  I usually run the 'clean' phase to delete the target folder.


mvn clean install

Deploy to local server

After you have downloaded and extracted the SAP server you will have the Neo cmd line application available to perform various tasks related to your HCP environment.  You can make use of Maven for some of the tasks related to creating server, deploy to local server, start-stop local server. For more detail read Building Java Web Applications with Maven and Working with the "Neo" Maven Plugin.

Prepare the pom.xml

You need to configure the pom file by specifying and configuring the neo maven plugin.  The following is an example I have added to the root pom.  I have left my local settings as an example but you need to make the required changes. For more information about the Neo Maven plugin.


<build>
  <plugins>
  <plugin>
  <groupId>com.sap.cloud</groupId>
  <artifactId>neo-java-web-maven-plugin</artifactId>
  <version>3.12.4.3</version>
  <executions>
  </executions>
  <configuration>
  <!-- Location of the SAP HANA Cloud Platform SDK -->
  <sdkInstallPath>C:/apps/neo-java-web-sdk-3.12.4.3</sdkInstallPath>
  <!-- WAR of the application under test -->
  <source>${project.basedir}/odata/target/odata.war</source>
  <!-- Configuration for local server -->
  <location>C:/apps/neo-java-web-sdk-3.12.4.3/server</location>
  </configuration>
  </plugin>
  </plugins>
  </build>

Create a local server

You only need to do this once.


mvn neo-java-web:install-local

After running the above the local server is created.

Deploy to a local server

After a successful build you will want to deploy the application to the local server.


mvn neo-java-web:deploy-local

Start/Stop local server

To start or stop the local server make use of the following

Start


mvn neo-java-web:start-local

Stop


mvn neo-java-web:stop-local

Maven release

Up until now we have covered the basics.  Lets have a look at something more interesting, releasing your project with Maven.  The idea is to create a release tag with a release version (maybe for production), and then to increment the trunc version.  E.g. currently our trunc (development) version is 1.0-SNAPSHOT.  When we release we create the 1.0 tag that indicates an incremental release.  The trunc version is then incremented to 2.0-SNAPSHOT.  This allows an easy way to fix bugs on the production code line. Below diagram illustrates what a very basic release flow might look like.

Currently we are on trunc version 1.0-SNAPSHOT, below is a screen shot of a Git repo.  At this stage it is version 1.0-SNAPSHOT.

Prepare the pom.xml

First specify your git repository.  Add the following to the root pom.


<scm>
  <connection>scm:git:https://git.hanatrial.ondemand.com/sometrial/myproject</connection>
  <developerConnection>scm:git:https://git.hanatrial.ondemand.com/sometrial/myproject</developerConnection>
  <url>https://git.hanatrial.ondemand.com/plugins/gitiles/sometrial%2Fmyproject</url>
  <tag>HEAD</tag>
  </scm>


Now specify and configure the release plugin.  Add the following to the root pom under build>plugins.


<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-release-plugin</artifactId>
  <version>2.5.1</version>
  <configuration>
          <username>[username]</username>
          <password>[password]</password>
        </configuration>
  <dependencies>
  <dependency>
  <groupId>org.apache.maven.shared</groupId>
  <artifactId>maven-invoker</artifactId>
  <version>2.2</version>
  </dependency>
  </dependencies>
  </plugin>


Perform release

Now you will perform the maven release.  You might want to perform this on a test repository to ensure all is working and that you understand the configuration required.

Run the following from the project root directory.


Note: Make sure you do not have any local changes before starting the release.







mvn release:prepare

After this you will notice a new Tag in your Git repo and that the pom.xml version has incremented to 1.1-SNAPSHOT

Clean release

All that remains to do now is to clean up after the release.  You can run the following.


mvn release:clean

Conclusion

As you have seen Maven is a powerful tool that can be used for many development tasks.

Thank you for reading.