Welcome!

Industrial IoT Authors: Zakia Bouachraoui, William Schmarzo, Elizabeth White, Stackify Blog, Yeshim Deniz

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
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...
Nicolas Fierro is CEO of MIMIR Blockchain Solutions. He is a programmer, technologist, and operations dev who has worked with Ethereum and blockchain since 2014. His knowledge in blockchain dates to when he performed dev ops services to the Ethereum Foundation as one the privileged few developers to work with the original core team in Switzerland.
René Bostic is the Technical VP of the IBM Cloud Unit in North America. Enjoying her career with IBM during the modern millennial technological era, she is an expert in cloud computing, DevOps and emerging cloud technologies such as Blockchain. Her strengths and core competencies include a proven record of accomplishments in consensus building at all levels to assess, plan, and implement enterprise and cloud computing solutions. René is a member of the Society of Women Engineers (SWE) and a m...
Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereal. Andrew's role at ConsenSys Enterprise is a mul...
Whenever a new technology hits the high points of hype, everyone starts talking about it like it will solve all their business problems. Blockchain is one of those technologies. According to Gartner's latest report on the hype cycle of emerging technologies, blockchain has just passed the peak of their hype cycle curve. If you read the news articles about it, one would think it has taken over the technology world. No disruptive technology is without its challenges and potential impediments t...
If a machine can invent, does this mean the end of the patent system as we know it? The patent system, both in the US and Europe, allows companies to protect their inventions and helps foster innovation. However, Artificial Intelligence (AI) could be set to disrupt the patent system as we know it. This talk will examine how AI may change the patent landscape in the years to come. Furthermore, ways in which companies can best protect their AI related inventions will be examined from both a US and...
In his general session at 19th Cloud Expo, Manish Dixit, VP of Product and Engineering at Dice, discussed how Dice leverages data insights and tools to help both tech professionals and recruiters better understand how skills relate to each other and which skills are in high demand using interactive visualizations and salary indicator tools to maximize earning potential. Manish Dixit is VP of Product and Engineering at Dice. As the leader of the Product, Engineering and Data Sciences team at D...
Bill Schmarzo, Tech Chair of "Big Data | Analytics" of upcoming CloudEXPO | DXWorldEXPO New York (November 12-13, 2018, New York City) today announced the outline and schedule of the track. "The track has been designed in experience/degree order," said Schmarzo. "So, that folks who attend the entire track can leave the conference with some of the skills necessary to get their work done when they get back to their offices. It actually ties back to some work that I'm doing at the University of San...
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...
Bill Schmarzo, author of "Big Data: Understanding How Data Powers Big Business" and "Big Data MBA: Driving Business Strategies with Data Science," is responsible for setting the strategy and defining the Big Data service offerings and capabilities for EMC Global Services Big Data Practice. As the CTO for the Big Data Practice, he is responsible for working with organizations to help them identify where and how to start their big data journeys. He's written several white papers, is an avid blogge...