Welcome!

Industrial IoT Authors: Elizabeth White, Stackify Blog, Yeshim Deniz, SmartBear Blog, Liz McMillan

Blog Feed Post

Creating maven archetype using create-from-project

Maven archetypes are a great way to create templates for standardized projects. The advantage of creating a maven archetype is that it can easily integrate with any IDE that supports maven compilation such as Eclipse. In SMART we provide maven archetypes to get started on developing SMART flows easily. In this blog I want to share our learning when creating these archetypes.

Sample Archetype

We will write an archetype called simple-archetype that generates the following structure:

    pom.xml
    src
     |--main
        |-- java
        |   |-- package (org.smart.demo)
        |       |-- TestMessage.java
        |-- resources
            |-- TestMessageFlow.soa
            |-- package (org.smart.demo)
                |-- TestMessage.soa

when the below command is run:

 mvn archetype:generate \
-DarchetypeGroupId=org.smart.archetypes \
-DarchetypeArtifactId=simple-archetype \  
-DarchetypeVersion=1.0-SNAPSHOT \  
-DgroupId=org.smart.demo \  
-DartifactId=testmessage \  
-Dclassname=TestMessage

Creating the skeleton archetype project

Maven archetypes can be created easily using the create-from-project maven command. To create an archetype from a project run the command from the directory where the template project is present.

mvn archetype:create-from-project

We will use the sample template project from here to create the archetype. This creates an archetype from the current project into the directory target/generated-sources/archetype. When we look at the generated project for the archetype we find that Message.java is hardcoded. Hence using this archetype will always only generate back the same fields and java classes as present in the template project.

archetype properties

How to vary the generated class? By default maven has a set of standard variables defined that can be varied when the project is created. The most common ones that are used:

  • groupId
  • artifactId
  • version
  • package

These are defaulted to the values in the project from which the artefact is created. More variables can be added to this as required for the archetype. For eg., in our project we want to vary the name of Message.java, to do this we will introduce a new variable called “data” that can be specified when the maven project is created.

The maven create-from-project takes in a archetype properties file that define these variables. To use this we define a generate.properties file with content as below (Ensure this file is present outside the directory of your template project):

package=org.smart.flows.message
groupId=org.smart.flows
artifactId=message
version=1.0-SNAPSHOT
data=Message

The values should be the values that are present in the template project. In our example, we have the data class in the package org.smart.flows.message and it is called Message.java. Hence we have defined the “data” variable as Message. This is important because the create-from-project will automatically replace the values with the variables. Now you can run the command as

mvn -Darchetype.properties=../generate.properties archetype:create-from-project

The Message.java that is generated will be as below:

#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${groupId}.${artifactId};

public class ${data} implements java.io.Serializable
{
    private String ${artifactId};

    public ${data}()
    {
    }

    public String get${data}() { return ${artifactId}; }
}

Now the content of the java file will vary based on the “data” and “artifactId” variables set. The generated project has a few things that has to be fixed.

  • The name of the java file is still hardcoded to Message.java.
  • The files in resources directory is still hardcoded with Message

Changing the name of the generated file

The files that will be used to generate the project is present under the directory “src/main/resources/archetype-resources/”. The java file is under “src/main/java”. Move the Message.java file to  __data__.java. This will ensure that the generated file is the same name as the variable passed to the maven command. The package variable is not correct, since the artifactId and groupId reflect the package name. Hence we change the package from ${groupId}.${artifactId} to ${package}. This sets up the java class correctly.

The test classes also need to follow the same pattern, except the file has to have a “Test” post fixed to the name. Hence we rename the test file as __data__Test.java. Any variations in the java classes can be done manually. Any ${…} inside the file is substituted with the variable value and __…__ in the file name is substituted with the variable value.

Changing the resources directory

While the java classes are by default created with substitution variables, the resources are not. These files need to be modified manually. In our case we have two files to be generated. Flow.soa that describes our jar.The .soa describes our data class and has to be present in the same package as the java class. Modification for substitution is simple. The soa file is changed as below:

##set( $symbol_pound = '#' )
##set( $symbol_dollar = '$' )
##set( $symbol_escape = '\' )
name:   ${data}Flow
defaultEnable: all
primeData:
    - ${package}.${data}
events:
    - ${package}.${event}
transitions:
    - ${package}.${transition}
features:
    - name: all
      artefacts:
        - ${package}.${data}
        - ${package}.${event}
        - ${package}.${transition}

We need to add the first three lines in all files. This has to be equal to a comment, hence we use “##”. We replace all places where substitution has to occur with the proper variables. This generates the file with the correct substitutions. But changing the name of the file to “__data__Flow.soa” does not change the file name based on the variable.

To do this we need to change the archetype metadata. This is present under the directory “src/main/resources/META-INF/maven/” in the file archetype-metadata.xml. The fileset command in this file defines how the files are created:

  

    <fileSet filtered="true" packaged="true" encoding="UTF-8">
      src/main/java

        **/*.java

    <fileSet encoding="UTF-8">
      src/main/resources

        **/*.soa

    
    <fileSet filtered="true" packaged="true" encoding="UTF-8">
      src/test/java

        **/*.java

From the above we see that the resources directory is a direct copy. To change this we need two variables defined, filtered and packaged. So we redefine this as below:

    <fileSet encoding="UTF-8" packaged="true" filtered="true">
      src/main/resources

        **/__*__.soa

    filtered="true">
      src/main/resources

        **/*Flow.soa
        **/*.js

We need the Flow.soa directly under the parent directory while the .soa file needs to be present in the packaged directory. Hence we split this into two filesets. Now we rename the files appropriately i.e., MessageFlow.soa is named as __data__Flow.soa and Message.soa is directly under the resources directory and named as __data__.soa file. This ensures that we get the correct project generated when the maven command is run.The generated maven archetype can be checked out here.


Read the original blog entry...

More Stories By Raji Sankar

Raji Sankar is a techie at heart trying his hand at entrepreneur-ship. He is Co-Founder and Director of BrioTribes Technologies India Pvt Ltd. based out of Bangalore. He likes to try out anything that is new, interesting and cutting edge. His latest passion is cloud and creating an application platform for the cloud. Check out SMART the documentation for the open-source cloud application platform here and the demo for the platform here

IoT & Smart Cities Stories
Digital Transformation and Disruption, Amazon Style - What You Can Learn. Chris Kocher is a co-founder of Grey Heron, a management and strategic marketing consulting firm. He has 25+ years in both strategic and hands-on operating experience helping executives and investors build revenues and shareholder value. He has consulted with over 130 companies on innovating with new business models, product strategies and monetization. Chris has held management positions at HP and Symantec in addition to ...
Dynatrace is an application performance management software company with products for the information technology departments and digital business owners of medium and large businesses. Building the Future of Monitoring with Artificial Intelligence. Today we can collect lots and lots of performance data. We build beautiful dashboards and even have fancy query languages to access and transform the data. Still performance data is a secret language only a couple of people understand. The more busine...
The challenges of aggregating data from consumer-oriented devices, such as wearable technologies and smart thermostats, are fairly well-understood. However, there are a new set of challenges for IoT devices that generate megabytes or gigabytes of data per second. Certainly, the infrastructure will have to change, as those volumes of data will likely overwhelm the available bandwidth for aggregating the data into a central repository. Ochandarena discusses a whole new way to think about your next...
CloudEXPO | DevOpsSUMMIT | DXWorldEXPO are the world's most influential, independent events where Cloud Computing was coined and where technology buyers and vendors meet to experience and discuss the big picture of Digital Transformation and all of the strategies, tactics, and tools they need to realize their goals. Sponsors of DXWorldEXPO | CloudEXPO benefit from unmatched branding, profile building and lead generation opportunities.
All in Mobile is a place where we continually maximize their impact by fostering understanding, empathy, insights, creativity and joy. They believe that a truly useful and desirable mobile app doesn't need the brightest idea or the most advanced technology. A great product begins with understanding people. It's easy to think that customers will love your app, but can you justify it? They make sure your final app is something that users truly want and need. The only way to do this is by ...
DXWorldEXPO LLC announced today that Big Data Federation to Exhibit at the 22nd International CloudEXPO, colocated with DevOpsSUMMIT and DXWorldEXPO, November 12-13, 2018 in New York City. Big Data Federation, Inc. develops and applies artificial intelligence to predict financial and economic events that matter. The company uncovers patterns and precise drivers of performance and outcomes with the aid of machine-learning algorithms, big data, and fundamental analysis. Their products are deployed...
Cell networks have the advantage of long-range communications, reaching an estimated 90% of the world. But cell networks such as 2G, 3G and LTE consume lots of power and were designed for connecting people. They are not optimized for low- or battery-powered devices or for IoT applications with infrequently transmitted data. Cell IoT modules that support narrow-band IoT and 4G cell networks will enable cell connectivity, device management, and app enablement for low-power wide-area network IoT. B...
The hierarchical architecture that distributes "compute" within the network specially at the edge can enable new services by harnessing emerging technologies. But Edge-Compute comes at increased cost that needs to be managed and potentially augmented by creative architecture solutions as there will always a catching-up with the capacity demands. Processing power in smartphones has enhanced YoY and there is increasingly spare compute capacity that can be potentially pooled. Uber has successfully ...
SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5–7, 2018, at the Javits Center in New York City, NY. CrowdReviews.com is a transparent online platform for determining which products and services are the best based on the opinion of the crowd. The crowd consists of Internet users that have experienced products and services first-hand and have an interest in letting other potential buye...
When talking IoT we often focus on the devices, the sensors, the hardware itself. The new smart appliances, the new smart or self-driving cars (which are amalgamations of many ‘things'). When we are looking at the world of IoT, we should take a step back, look at the big picture. What value are these devices providing. IoT is not about the devices, its about the data consumed and generated. The devices are tools, mechanisms, conduits. This paper discusses the considerations when dealing with the...