Git Product home page Git Product logo

cumulocity-microservice-archetype's Introduction

cumulocity-microservice-archetype

Maven archetype to generate cumulocity microservice project. Based on https://github.com/SoftwareAG/cumulocity-clients-java and https://cumulocity.com/guides/microservice-sdk/java/#java-microservice

The project will contain following project structure:

project
|-- pom.xml
`-- src
    |-- main
    |  | -- java
    |  |    `-- package
    |  |         | -- App.java
    |  |         | -- controller/ExampleController.java
    |  |          `-- service/ExampleService.java
    |  | -- resources
    |  |    |-- application.properties
    |  |    |-- application-dev.properties
    |  |    |-- application-test.properties
    |  |    |-- application-prod.properties
    |  |     `-- banner.txt
    |   `-- configuration
    |       |-- cumulocity.json
    |        `-- logging.xml

The project contains also an example REST controller which must be replaced or removed depending on your further development. However the complete project is directly runnable without any additional changes. It uses also some best practices like:

  • using spring profiles (dev, test and prod)
  • using local configured application-dev.properties to run localy on development env
  • using Logback configuration file
  • using current java cumulocity microservice SDK + configuration via cumulocity.json
  • using custom banner with cumulocity SDK version
  • using JUnit 5 and SpringBootTest to check if context load is successful

In order to make it even faster to setup your project, the archetype contains a post-generation script which registers your microservice automatically on your tenant. This step is optional.

The post-generation script does following steps:

  • Generate microservice application on tenant
  • Subscribes microservice to tenant
  • Acquires microservice credentials
  • writes all information to application-dev.properties

Prerequisites

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

Run

Cloning this repository into you local GIT repository

git clone ...

Install archetype localy in your local maven repository

mvn install

Go to the folder you want to generate the project

cd ..

Generate C8y miroservice project using interactive mode

mvn archetype:generate -DarchetypeGroupId=cumulocity.microservice -DarchetypeArtifactId=cumulocity-microservice-archetype

Note: In case you use go-c8y-cli you can use directly following environment variables C8Y_BASEURL and C8Y_HEADER_AUTHORIZATION in the command:

mvn archetype:generate -DarchetypeGroupId=cumulocity.microservice -DarchetypeArtifactId=cumulocity-microservice-archetype -DdevC8yBaseURL=%C8Y_BASEURL% -DdevC8yUserCredentialsBASE64=%C8Y_HEADER_AUTHORIZATION%

Step 1: Define your microservice name

[INFO] Generating project in Interactive mode
[INFO] Archetype [cumulocity.microservice:cumulocity-microservice-archetype:1.0.0-SNAPSHOT] found in catalog local
Define value for property 'microserviceName':

If your microservice name has more than one words, seperate the words by '-'

Step 2: Define your artifact id (default value cumulocity-microservice-)

[INFO] Generating project in Interactive mode
[INFO] Archetype [cumulocity.microservice:cumulocity-microservice-archetype:1.0.0-SNAPSHOT] found in catalog local
Define value for property 'microserviceName': hello-devices
[INFO] Using property: groupId = cumulocity.microservice
[INFO] Using property: version = 1.0.0-SNAPSHOT
[INFO] Using property: c8yVersion = 1013.0.0
[INFO] Using property: devC8yBaseURL = null
[INFO] Using property: devC8yUserCredentialsBASE64 = null
Define value for property 'artifactId' cumulocity-microservice-hello-devices: :

You can now just hit enter to continue with default or enter your own artificat id.

Step 3: Define your package name (default value cumulocity.microservice.)

[INFO] Generating project in Interactive mode
[INFO] Archetype [cumulocity.microservice:cumulocity-microservice-archetype:1.0.0-SNAPSHOT] found in catalog local
Define value for property 'microserviceName': hello-devices
[INFO] Using property: groupId = cumulocity.microservice
[INFO] Using property: version = 1.0.0-SNAPSHOT
[INFO] Using property: c8yVersion = 1013.0.0
[INFO] Using property: devC8yBaseURL = null
[INFO] Using property: devC8yUserCredentialsBASE64 = null
Define value for property 'artifactId' cumulocity-microservice-hello-devices: :
Define value for property 'package' cumulocity.microservice.hello-devices: : cumulocity.microservice.hello_devices

You can now just hit enter to continue with default or enter your own artificat id. !!! Beaware that '-' can't be used at java packages. In that case you must replace '-' with '_'. !!!

Step 4.1: Confirm your configuration with 'Y' without running post-generation script

[INFO] Generating project in Interactive mode
[INFO] Archetype [cumulocity.microservice:cumulocity-microservice-archetype:1.0.0-SNAPSHOT] found in catalog local
Define value for property 'microserviceName': hello-devices
[INFO] Using property: groupId = cumulocity.microservice
[INFO] Using property: version = 1.0.0-SNAPSHOT
[INFO] Using property: c8yVersion = 1013.0.0
[INFO] Using property: devC8yBaseURL = null
[INFO] Using property: devC8yUserCredentialsBASE64 = null
Define value for property 'artifactId' cumulocity-microservice-hello-devices: :
Define value for property 'package' cumulocity.microservice.hello-devices: : cumulocity.microservice.hello_devices
Confirm properties configuration:
microserviceName: hello-devices
groupId: cumulocity.microservice
version: 1.0.0-SNAPSHOT
c8yVersion: 1013.0.0
devC8yBaseURL: null
devC8yUserCredentialsBASE64: null
artifactId: cumulocity-microservice-hello-devices
package: cumulocity.microservice.hello_devices
 Y: : Y

Now you have created your microservice project successfully! In case your devC8yBaseURL and devC8yUserCredentialsBASE64 variable wasn't set, you can continue with Step 4.2, in order to initialze them via interactive mode and run the post-generation script. However running this script is optional!

IMPORTANT!!!

If you haven't setup your application-dev.properties to a specific tenant, the predefined unit test will not succeed! This unit test is starting the spring boot application and checks if the application is successfully starting. The microservice can't start if the c8y configuration isn't setup. However you can build with skipping the test by:

mvn clean install -Dmaven.test.skip=true

Step 4.2: Confirm your configuration with 'N' with running post-generation script

And repeate step 1 - 3 and insert devC8yBaseURL and devC8yUserCredentialsBASE64

 Y: : N
Define value for property 'microserviceName': hello-devices
Define value for property 'groupId' cumulocity.microservice: :
Define value for property 'version' 1.0.0-SNAPSHOT: :
Define value for property 'c8yVersion' 1013.0.0: :
Define value for property 'devC8yBaseURL': https://ms-template.eu-latest.cumulocity.com
Define value for property 'devC8yUserCredentialsBASE64': Basic XXXXX
Define value for property 'artifactId' cumulocity-microservice-hello-devices: :
Define value for property 'package' cumulocity.microservice.hello-devices: : cumulocity.microservice.hello_devices
Confirm properties configuration:
microserviceName: hello-devices
groupId: cumulocity.microservice
version: 1.0.0-SNAPSHOT
c8yVersion: 1013.0.0
devC8yBaseURL: https://ms-template.eu-latest.cumulocity.com
devC8yUserCredentialsBASE64: Basic XXXXX
artifactId: cumulocity-microservice-hello-devices
package: cumulocity.microservice.hello_devices
 Y: : Y

Step 5: Build your fresh generated project

Go to the project folder

cd cumulocity-microservice-hello-devices/

and build the project:

mvn install

Running the microservice locally you have to add microservice service user to application-dev.properties, have you run the script with step 4.2 the properties are automatically configured

C8Y.bootstrap.tenant=<tenant ID>
C8Y.baseURL=<URL>
C8Y.bootstrap.user=<service-user>
C8Y.bootstrap.password=<service-user-password>

Step 6: Start the microservice and test

Go to target and run the spring boot appliation

java -jar cumulocity-microservice-hello-devices-1.0.0-SNAPSHOT.jar

Open the browser and open link http://localhost:8080/api/hello/devices, you have to insert your Cumulocity credentials, keep in mind to set the tenant Id in front of your user name like t2134/[email protected].

Now your microservice is ready to evolve!!!

For building docker container please change property in pom file to:

<c8y.docker.skip>false</c8y.docker.skip>

Live Demo

https://youtu.be/2j21ULZbtlg

Authors

Alexander Pester

Disclaimer

These tools are provided as-is and without warranty or support. They do not constitute part of the Software AG product suite. Users are free to use, fork and modify them, subject to the license agreement. While Software AG welcomes contributions, we cannot guarantee to include every contribution in the master project.

Contact

For more information you can Ask a Question in the TECHcommunity Forums.

You can find additional information in the Software AG TECHcommunity.


Contact us at TECHcommunity if you have any questions.

cumulocity-microservice-archetype's People

Contributors

cpoder avatar dependabot[bot] avatar haraldmeyer avatar pestusatsag avatar techcommunity avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

cumulocity-microservice-archetype's Issues

:generate does not complete

Please see the log below.

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  32.263 s
[INFO] Finished at: 2022-06-30T11:07:25+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:3.0.1:generate (default-cli) on project cumulocity-microservice-archetype: org.apache.maven.archetype.exception.InvalidPackaging: Unable to add module to the current project as it is not of packaging type 'pom' -> [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] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

spring dev tools not working anymore

With new version 10.15, spring dev tools not working anymore:

Exception:
Caused by: java.lang.IllegalAccessError: class com.cumulocity.microservice.api.CumulocityClientFeature$TenantPlatformConfig$$EnhancerBySpringCGLIB$$23091115_2 cannot access its superclass com.cumulocity.microservice.api.CumulocityClientFeature$TenantPlatformConfig (com.cumulocity.microservice.api.CumulocityClientFeature$TenantPlatformConfig$$EnhancerBySpringCGLIB$$23091115_2 is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @c263e7e; com.cumulocity.microservice.api.CumulocityClientFeature$TenantPlatformConfig is in unnamed module of loader 'app')

production spring profile

Hello!

I am using a run configuration in Eclipse to package a prod version of my microservice created using the microservice archetype. These are the steps I took:

  1. Created Maven profiles with the <spring.profiles.active>prod</spring.profiles.active> property
  2. Create a Maven run configuration passing the spring.profiles.active parameter as prod
  3. Set [email protected]@ in application.properties

But doing only this, the build was complaining about the application name, which seemed to be missing. Then I included the following parameters in application-prod.properties:

application.name
application.key
C8Y.bootstrap.tenant
C8Y.baseURL
C8Y.microservice.isolation

This way it worked and my question is: Is this a proper way of configuring the profiles?

Thanks!

Build Failure: Text must not be null or empty

[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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.