Jump-Start your next Cumulocity microservice project in java

Introduction

Setting up a microservice for Cumulocity using Microservice SDK project is easy.

However, a few manual steps must be taken before you have a successfully running microservice locally configured to your Cumulocity instance. To speed this process up, use a maven archetype instead: cumulocity-microservice-archetype

This open-source maven archetype helps you to setup your maven project in less than 5 minutes!!!

It is not just faster; it is also a good template which are based on best practices.

In this blog post I show you, step by step how to use this cumulocity-microservice-archetype to setup your first project “hello”.

Pre-requisite

  • Java installed >= 11
  • Maven installed >= 3.6
  • Cumulocity IoT Tenant >= 1010.0.0
  • Cumulocity IoT User Credentials (Base64 encoded)

Steps to follow

Step 1: Go to GitHub - SoftwareAG/cumulocity-microservice-archetype: Maven archetype for cumulocity microservice. Developed by Global Competency Center IoT and clone the repository

Step 2: Build and install the cumulocity-microservice-archetype in your local maven repository

$ cd cumulocity-microservice-archetype
$ mvn install

Step 3: Generate your project by using the archetype

Move to the location you would like to generate the project:

$ cd ..
$ cd workspace

Before you run the mvn archetyp:generate command you should know:

  • Microservice name
  • Cumulocity development environment URL:
  • Cumulocity development environment User credentials in Base64

Your Base64 user credentials you can collect via Developer tools, Network tab. The request header contains the Authorization option.

If you use go-c8y-cli you can use an easier way of using the archetype. Just use the environment variable which are set by that cli C8Y_BASEURL and C8Y_HEADER_AUTHORIZATION. set-session on your terminal and run following maven command Example: $ mvn archetype:generate -DarchetypeGroupId=cumulocity.microservice -DarchetypeArtifactId=cumulocity-microservice-archetype -DinteractiveMode=false -DmicroserviceName=hello -DdevC8yBaseURL="$C8Y_BASEURL" -DdevC8yUserCredentialsBASE64="$C8Y_HEADER_AUTHORIZATION"

If you have all the parameter, then you can run following script (replace Basic XXXXX with your Base64 credentials and https://your.tenant.cumulocity.com with your cumulocity instance)

$ mvn archetype:generate -DarchetypeGroupId=cumulocity.microservice -DarchetypeArtifactId=cumulocity-microservice-archetype -DinteractiveMode=false -DmicroserviceName=hello -DdevC8yBaseURL=https://your.tenant.cumulocity.com -DdevC8yUserCredentialsBASE64="Basic XXXXX"

If you run interactive mode you get for each parameter an input prompt. For more details look at GitHub - SoftwareAG/cumulocity-microservice-archetype: Maven archetype for cumulocity microservice. Developed by Global Competency Center IoT

If maven tells you “BUILD SUCCESS”, you can continue with Step 4

Step 4: Run your fresh generated microservice the first time

$ cd cumulocity-microservice-hello
$ mvn clean install
$ cd target
$ java -jar cumulocity-microservice-hello-1.0.0-SNAPSHOT.jar

Step 5: Open the browser a check if microservice is running successfully

http://localhost:8080/api/hello/devices

The browser will open an user/password dialog. Insert you user in following way {tenant}/{user name}.

If this REST endpoint is returning an array of all devices. Well done, you have already setup your microservice project successfully!!

Step 6: Develop further and use some good examples

Now you can start developing your own features. The project contains a simple controller and service. These classes should be removed and are placeholders for your controllers and services.

If you have special nonfunctional requirements please check the microservice templates. They contain good examples how to solve common problems.

Useful links | Relevant resources

https://cumulocity.com/guides/microservice-sdk/java/
https://github.com/SoftwareAG/cumulocity-microservice-archetype
https://github.com/SoftwareAG/cumulocity-microservice-templates
https://maven.apache.org/guides/introduction/introduction-to-archetypes.html

6 Likes

When running the archetype and filling out all the information I get the following error:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.471 s
[INFO] Finished at: 2023-12-13T18:02:05+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:3.2.1:generate (default-cli) on project standalone-pom: Text must not be null or empty → [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] MojoFailureException - Apache Maven - Apache Software Foundation

Thank you for reporting that issue. I was so free to create an issue at github:
https://github.com/SoftwareAG/cumulocity-microservice-archetype/issues/16

I will investigate why this happend: Could you please tell me which java, maven version and sdk you are using?



Alexander Pester

2 Likes

Issue is fixed Build Failure: Text must not be null or empty · Issue #16 · SoftwareAG/cumulocity-microservice-archetype · GitHub

2 Likes