Skip to content

Create boilerplate project using maven

Kristian Karl edited this page Mar 4, 2021 · 9 revisions

This will show how to create a boilerplate GraphWalker project using the maven archetype.

Create a boilerplate project

From the command line, run:

mvn archetype:generate -B -DarchetypeGroupId=org.graphwalker -DarchetypeArtifactId=graphwalker-maven-archetype -DgroupId=com.company -DartifactId=myProject -DarchetypeVersion=LATEST

Move into the myProject folder, and test the project:

cd myProject

Now, run it:

mvn compile exec:java -Dexec.mainClass="com.company.Runner"

You now have a complete GraphWalker project, and you also ran a test.

About auto-generated sources

The maven command above generated an interface SmallTest, from the model SmallTest.json. This feature helps the developer keep the code in sync with the model(s). The requirement is that the graphml file needs to be in the folder path, which is the same (package) path as for the java project, but under resources instead.

$ tree
.
├── pom.xml
├── src
│   └── main
│       ├── java
│       │   └── com
│       │       └── company
│       │           ├── Runner.java
│       │           └── SomeSmallTest.java
│       └── resources
│           └── com
│               └── company
│                   └── SmallTest.json
└── target
    ├── classes
    │   └── com
    │       └── company
    │           ├── Runner.class
    │           ├── SmallTest.class
    │           ├── SmallTest.json
    │           └── SomeSmallTest.class
    ├── generated-sources
    │   ├── annotations
    │   └── graphwalker
    │       ├── cache.json
    │       └── com
    │           └── company
    │               └── SmallTest.java
    └── maven-status
        └── maven-compiler-plugin
            └── compile
                └── default-compile
                    ├── createdFiles.lst
                    └── inputFiles.lst

21 directories, 12 files

The interface will be generated, named and placed in the folder: target/generated-sources/graphwalker/com/company/SmallTest.java

// Generated by GraphWalker (http://www.graphwalker.org)
package com.company;

import org.graphwalker.java.annotation.Model;
import org.graphwalker.java.annotation.Vertex;
import org.graphwalker.java.annotation.Edge;

@Model(file = "com/company/SmallTest.json")
public interface SmallTest {

    @Edge()
    void e_FirstAction();

    @Edge()
    void e_SomeOtherAction();

    @Vertex()
    void v_VerifyFirstAction();

    @Edge()
    void e_AnotherAction();

    @Vertex()
    void v_VerifyInitialState();

    @Vertex()
    void v_NewVertex();

    @Edge()
    void e_SomeAction();
}

The test automation code

The class SomeSmallTest implements the interface SmallTest.

package com.company;

import org.graphwalker.core.machine.ExecutionContext;

/**
 * Implements the GraphWalker model: src/main/resources/SmallTest.json
 * <p/>
 * The interface SmallTest, that SomeSmallTest implements, is generated by
 * running: mvn graphwalker:generate-sources
 * also: mvn compile
 */
public class SomeSmallTest extends ExecutionContext implements SmallTest {

  @Override
  public void e_FirstAction() {
    System.out.println("Running: e_FirstAction");
  }

  @Override
  public void e_AnotherAction() {
    System.out.println("Running: e_AnotherAction");
  }

  @Override
  public void e_SomeAction() {
    System.out.println("Running: e_SomeAction");
  }

  @Override
  public void e_SomeOtherAction() {
    System.out.println("Running: e_SomeOtherAction");
  }

  @Override
  public void v_VerifyInitialState() {
    System.out.println("Running: v_VerifyInitialState");
  }

  @Override
  public void v_VerifyFirstAction() {
    System.out.println("Running: v_VerifyFirstAction");
  }

  @Override
  public void v_NewVertex() {
    System.out.println("Running: v_NewVertex");
  }
}

Running the test

When running the the test the output might look something like this:

$ mvn compile exec:java -Dexec.mainClass="com.company.Runner"
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------< org.graphwalker:graphwalker-maven-archetype >-------------
[INFO] Building GraphWalker Example 4.3.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- graphwalker-maven-plugin:4.3.1:generate-sources (generate-sources) @ graphwalker-maven-archetype ---
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ graphwalker-maven-archetype ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.6.1:compile (default-compile) @ graphwalker-maven-archetype ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ graphwalker-maven-archetype ---
11:19:34.487 [com.company.Runner.main()] DEBUG org.graphwalker.io.factory.json.JsonContextFactory - {  "models": [    {      "name": "SmallTest",      "id": "cbca8c67-6d0c-40b4-8afb-032b069a4bc1",      "generator": "random(edge_coverage(100))",      "actions": [],      "vertices": [        {          "id": "d9c15804-24de-47ca-9430-9cb93202d9ff",          "name": "v_VerifyInitialState",          "actions": [],          "requirements": [],          "properties": {            "x": 206.9166717529297,            "y": 54.41667175292969          }        },        {          "id": "fd4435f7-6a42-4a8b-a522-49c6b91a85e1",          "name": "v_VerifyFirstAction",          "actions": [],          "requirements": [],          "properties": {            "x": 348.9166717529297,            "y": 368.4166717529297          }        },        {          "id": "3dbb2cf6-1ac9-4858-9fd8-bc6b874c1daf",          "name": "v_NewVertex",          "actions": [],          "requirements": [],          "properties": {            "x": 98.91667175292969,            "y": 228.4166717529297          }        }      ],      "edges": [        {          "id": "9a6b8be2-3e0c-4e8d-bb38-21107b66a4d3",          "name": "e_FirstAction",          "actions": [],          "requirements": [],          "properties": [],          "sourceVertexId": "d9c15804-24de-47ca-9430-9cb93202d9ff",          "targetVertexId": "fd4435f7-6a42-4a8b-a522-49c6b91a85e1"        },        {          "id": "6034d0f8-9850-4d67-9ddf-02a24c35890e",          "name": "e_AnotherAction",          "actions": [],          "requirements": [],          "properties": [],          "sourceVertexId": "fd4435f7-6a42-4a8b-a522-49c6b91a85e1",          "targetVertexId": "3dbb2cf6-1ac9-4858-9fd8-bc6b874c1daf"        },        {          "id": "71d24b33-fe8e-49ab-9fcc-10cc985dcdeb",          "name": "e_SomeAction",          "actions": [],          "requirements": [],          "properties": [],          "sourceVertexId": "3dbb2cf6-1ac9-4858-9fd8-bc6b874c1daf",          "targetVertexId": "fd4435f7-6a42-4a8b-a522-49c6b91a85e1"        },        {          "id": "2d241ce5-ac46-4f23-85b8-2253c4ce8bed",          "name": "e_SomeOtherAction",          "actions": [],          "requirements": [],          "properties": [],          "sourceVertexId": "3dbb2cf6-1ac9-4858-9fd8-bc6b874c1daf",          "targetVertexId": "3dbb2cf6-1ac9-4858-9fd8-bc6b874c1daf"        }      ],      "startElementId": "d9c15804-24de-47ca-9430-9cb93202d9ff"    }  ]}
11:19:34.630 [com.company.Runner.main()] DEBUG org.graphwalker.core.machine.SimpleMachine - Context: com.company.SomeSmallTest@542a384b
11:19:34.631 [com.company.Runner.main()] DEBUG org.graphwalker.core.machine.ExecutionContext - Execute: 'v_VerifyInitialState' in model: 'SmallTest'
Running: v_VerifyInitialState
11:19:34.669 [com.company.Runner.main()] DEBUG org.graphwalker.core.machine.SimpleMachine - Context: com.company.SomeSmallTest@542a384b
11:19:34.669 [com.company.Runner.main()] DEBUG org.graphwalker.core.machine.ExecutionContext - Execute: 'e_FirstAction' in model: 'SmallTest'
Running: e_FirstAction
11:19:34.674 [com.company.Runner.main()] DEBUG org.graphwalker.core.machine.SimpleMachine - Context: com.company.SomeSmallTest@542a384b
11:19:34.675 [com.company.Runner.main()] DEBUG org.graphwalker.core.machine.ExecutionContext - Execute: 'v_VerifyFirstAction' in model: 'SmallTest'
Running: v_VerifyFirstAction
11:19:34.680 [com.company.Runner.main()] DEBUG org.graphwalker.core.machine.SimpleMachine - Context: com.company.SomeSmallTest@542a384b
11:19:34.681 [com.company.Runner.main()] DEBUG org.graphwalker.core.machine.ExecutionContext - Execute: 'e_AnotherAction' in model: 'SmallTest'
Running: e_AnotherAction
11:19:34.686 [com.company.Runner.main()] DEBUG org.graphwalker.core.machine.SimpleMachine - Context: com.company.SomeSmallTest@542a384b
11:19:34.686 [com.company.Runner.main()] DEBUG org.graphwalker.core.machine.ExecutionContext - Execute: 'v_NewVertex' in model: 'SmallTest'
Running: v_NewVertex
11:19:34.692 [com.company.Runner.main()] DEBUG org.graphwalker.core.machine.SimpleMachine - Context: com.company.SomeSmallTest@542a384b
11:19:34.692 [com.company.Runner.main()] DEBUG org.graphwalker.core.machine.ExecutionContext - Execute: 'e_SomeOtherAction' in model: 'SmallTest'
Running: e_SomeOtherAction
11:19:34.697 [com.company.Runner.main()] DEBUG org.graphwalker.core.machine.SimpleMachine - Context: com.company.SomeSmallTest@542a384b
11:19:34.697 [com.company.Runner.main()] DEBUG org.graphwalker.core.machine.ExecutionContext - Execute: 'v_NewVertex' in model: 'SmallTest'
Running: v_NewVertex
11:19:34.698 [com.company.Runner.main()] DEBUG org.graphwalker.core.machine.SimpleMachine - Context: com.company.SomeSmallTest@542a384b
11:19:34.698 [com.company.Runner.main()] DEBUG org.graphwalker.core.machine.ExecutionContext - Execute: 'e_SomeAction' in model: 'SmallTest'
Running: e_SomeAction
11:19:34.703 [com.company.Runner.main()] DEBUG org.graphwalker.core.machine.SimpleMachine - Context: com.company.SomeSmallTest@542a384b
11:19:34.703 [com.company.Runner.main()] DEBUG org.graphwalker.core.machine.ExecutionContext - Execute: 'v_VerifyFirstAction' in model: 'SmallTest'
Running: v_VerifyFirstAction
Done: [{
  "totalFailedNumberOfModels": 0,
  "totalNotExecutedNumberOfModels": 0,
  "totalNumberOfUnvisitedVertices": 0,
  "verticesNotVisited": [],
  "totalNumberOfModels": 1,
  "totalCompletedNumberOfModels": 1,
  "totalNumberOfVisitedEdges": 4,
  "totalIncompleteNumberOfModels": 0,
  "edgesNotVisited": [],
  "vertexCoverage": 100,
  "totalNumberOfEdges": 4,
  "totalNumberOfVisitedVertices": 3,
  "edgeCoverage": 100,
  "totalNumberOfVertices": 3,
  "totalNumberOfUnvisitedEdges": 0
}]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.155 s
[INFO] Finished at: 2019-10-04T11:19:34+02:00
[INFO] ------------------------------------------------------------------------
Clone this wiki locally