Tutorial – Simplest TRIMM Java Maven example

In this example we will create the simplest possible TRIMM Java code generation example using Maven.

Prerequisites:

Step 1 – Create a TRIMM Java enabled Maven project

Execute the following command through your commandline/terminal:

mvn archetype:generate -DarchetypeGroupId=dk.tigerteam.maven.archetypes -DarchetypeArtifactId=trimm.java.archetype -DarchetypeVersion=1.0.0 -DarchetypeCatalog=http://www.tigerteam.dk/maven2/archetype-catalog.xml

and for this example we will provide the following values:

groupId dk.tigerteam.trimm.examples
artifactId simplejavaproject
version 1.0-SNAPSHOT
package dk.tigerteam.trimm.examples.simplejavaproject

Step 2 – Import your Maven project into your IDE

Follow this guide – but change the project folder to be our newly created simplejavaproject project (e.g. placed in C:\TRIMM-examples\simplejavaproject)

Step 3 – Model your simple Domain model using Enterprise Architect

Follow this video guide – but change the project folder to be our newly created simplejavaproject project (e.g. placed in C:\TRIMM-examples\simplejavaproject)

TRIMM Simplest Java Model – Enterprise Architect tutorial from Jeppe Cramon on Vimeo.

Step 4 – Configure the TRIMM Java Maven plugin to generate sourcecode

The TRIMM Java code generation functionality is defined in the projects Maven configuration file called pom.xml which is placed in the root of the project folder (e.g. C:\TRIMM-examples\simplejavaproject)

Within the pom.xml, inside the build/plugins section you will find the TrimmJavaMavenPlugin plugin

    
          <plugin>
                <groupId>dk.tigerteam</groupId>
                <artifactId>TrimmJavaMavenPlugin</artifactId>
                <version>1.0.0</version>
                <executions>
                    <execution>
                        <id>generate</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <yamlFile>model/JavaModel.yml</yamlFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

You can see from the value inside the yamlFile the configuration file that is used to configure the TrimmJavaMavenPlugin plugin.
In this case it’s the JavaModel.yml which is placed inside the model folder of the project, together with our EA project file (e.g. SimpleJavaModel.eap) and our XMI export from Step 4 (model.xml).

TRIMM Java configuration file (YAML format)

TRIMM Java’s default configuration format is YAML, which is a very simple file format (like XML but without all the noisy brackets < >)
We create a configuration file e.g. called JavaModel.yml (the name and extension can be anything you like) and place it in C:\TRIMM-examples\simplejavaproject\
You can use NotePad, Edit, NotePad++ or what ever you like to create the file.

The first thing we need to tell TRIMM is where our XMI file is placed:

xmiModelPath: model/mysimplemodel.xml

Next we need to tell TRIMM which UML tool we used. Since we used Enterprise Architect we will use the value EA

umlTool: EA

Note: Other possible values are MagicDraw16, MagicDraw17 or the FQCN (Fully Qualified Class Name) for a Java class on the classpath that implements the XmiReader interface. Alternative you can specify a .groovy script (that also implements the XmiReader interface)

For the Maven build to be a success we need to tell TRIMM where to place the generated code.
We will adhere to the Maven standard and place all our generated code inside the target/generated-sources folder
TRIMM can generate multiple types of files, but for this we can suffice with just specifying where it should place BaseClasses:

generateBaseClassesToPath: target/generated-sources/domainmodel

The complete configuration file looks like this:

xmiModelPath: mysimplemodel.xml
umlTool: EA
generateBaseClassesToPath: target/generated-sources/domainmodel

Note: The TRIMM code generator automatically cleans up files in its output folder that wasn’t part of the code generation phase. It’s therefore necessary that you specify generateBaseClassesToPath to be something different than an empty value, otherwise you will loose all the contents in your project folder!

That’s it for this simple model.

Generate code

The next step is to run Mavens build process as far at it will generate sourcecode e.g. from the command line mvn generate-sources or mvn compile
You can also invoke it from within IntelliJ IDEA using Maven > Generate Source and Update Folders or from within Eclipse.

Code will now be generated into the projects target/generated-sources/domainmodel subfolder and if you instruct IDEA to Generate Source and Update Folders it will also be reflected in your project

The contents of the two files is shown below.
You might have noticed that the code references java.util.Date automatically eventhough we in our Enterprise Model only wrote Date as type. This is because TRIMM when configured using YAML automatically adds the BuiltInTypesListener extension to the configuration, which resolves typically used Java classes which are not defined in java.lang and therefore needs to be explicit (we use FQCN and imports since your model might contains a class by similar name).

Customer.java

package dk.tigerteam.examples.simplestjavamodel;

import dk.tigerteam.examples.simplestjavamodel.ContactInfo;

public class Customer {
    /**
     * No documentation<p/>
     * Defined in Customer<p/>
     */
    private int age;

    /**
     * No documentation<p/>
     * Defined in Customer<p/>
     */
    private java.util.Date created;

    /**
     * No documentation<p/>
     * Defined in Customer<p/>
     */
    private String firstName;

    /**
     * No documentation<p/>
     * Defined in Customer<p/>
     */
    private String lastName;

    /**
     * No documentation<p/>
     * Defined in Customer<p/>
     * Property type hierarchy:
     * <ul>
     *   <li><b>ContactInfo</b></li>
     * </ul>
     */
    private ContactInfo contactInfo;

    public int getAge() {
        return age;
    }

    public void setAge(int parameter) {
        this.age = parameter;
    }

    public java.util.Date getCreated() {
        return created;
    }

    public void setCreated(java.util.Date parameter) {
        this.created = parameter;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String parameter) {
        this.firstName = parameter;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String parameter) {
        this.lastName = parameter;
    }

    public ContactInfo getContactInfo() {
        return contactInfo;
    }

    public void setContactInfo(ContactInfo parameter) {
        this.contactInfo = parameter;
    }
}

ContactInfo.java

package dk.tigerteam.examples.simplestjavamodel;

public class ContactInfo {
    /**
     * No documentation<p/>
     * Defined in ContactInfo<p/>
     */
    private String email;

    /**
     * No documentation<p/>
     * Defined in ContactInfo<p/>
     */
    private String mobilePhone;

    public String getEmail() {
        return email;
    }

    public void setEmail(String parameter) {
        this.email = parameter;
    }

    public String getMobilePhone() {
        return mobilePhone;
    }

    public void setMobilePhone(String parameter) {
        this.mobilePhone = parameter;
    }
}

More advanced code generation

TODO