How to Create Maven Archetype

Hello all, I have been automating web and mobile apps for a long time. The first step of my automation journey starts with copying and pasting our base classes into the new project. Those base classes generally contain base Selenium methods, intelligent waits, string /date and DB utility classes and browser executables. So why not automate our project creation by creating our custom Maven Archetype? Here’s how you can create a Maven Archetype for your automation projects.

Step 1: Creating a Sample Project

Create a clean Maven project with all the necessary classes, executable, etc inside of it.

In this example, I also have some example PageObjects and Test Classes for newbies to understand how we handle automation logic.

Step2: Creating and Editing the Archetype

This archetype’s name will always contain the project name written in pom.xml file. In our example, it’s selenium that’s why archetype name is selenium-archetype.

By using the terminal, go to project folder containing pom.xml file and execute this command.

mvn archetype:create-from-project

At the end of execution, you will have BUILD SUCCESS message and you will notice that an archetype is created.

Go to the project folder and you’ll notice a target folder is created. Under it, there are many folders as follows “generated-sources/archetype/….”. Under the archetype folder, we have a maven project structure with a new pom.xml.

Move this archetype folder to a new folder because we are going to work on it and I don’t want you to get lost between folders :)

Let’s deep dive into this folder and see what do we have under it!

What’s in archetype-resource Folder?

As you can see, under archetype-resource folder we have our base project folder created with everything we need. Go to the highlighted pom.xml file under archetype folder and open it.

groupId, artifactId, version, name tags are parametrized. Those parameters will be set on the command line or on IDE while trying to create Maven project.

What’s in Maven Folder?

Under this folder, we have archetype-metadata.xml file where you can edit the content of the project structure. You can change which folder to include or exclude from project, file type to exclude or include. I leave it as it is cause I need all of those files.

<fileSets>
    <fileSet filtered="true" packaged="true" encoding="UTF-8">
      <directory>src/main/java</directory>
      <includes>
        <include>**/*.java</include>
      </includes>
      <excludes>
        <exclude>base/BasePage.java</exclude>
        <exclude>base/BasePageUtil.java</exclude>
        <exclude>util/WaitingActions.java</exclude>
      </excludes>
    </fileSet>
    <fileSet filtered="true" encoding="UTF-8">
      <directory>src/main/java</directory>
      <includes>
        <include>base/BasePage.java</include>
        <include>base/BasePageUtil.java</include>
        <include>util/WaitingActions.java</include>
      </includes>
    </fileSet>
    <fileSet filtered="true" packaged="true" encoding="UTF-8">
      <directory>src/test/java</directory>
      <includes>
        <include>**/*.java</include>
      </includes>
    </fileSet>
    <fileSet filtered="true" encoding="UTF-8">
      <directory>properties/XYZWebConfig</directory>
      <includes>
        <include>**/*.properties</include>
      </includes>
    </fileSet>
    <fileSet filtered="true" encoding="UTF-8">
      <directory>properties</directory>
      <includes>
        <include>**/*.properties</include>
      </includes>
    </fileSet>
    <fileSet encoding="UTF-8">
      <directory>properties/driver</directory>
      <includes>
        <include>**/*.</include>
        <include>**/*.exe</include>
      </includes>
    </fileSet>
  </fileSets>

Step3: Including Our Archetype to Local Maven Catalog

Go to your new folder containing pom.xml file and execute below command:

mvn install 

At the end of execution, BUILD SUCCESS message will appear and Local Maven Catalogue is updated with your maven archetype.

Step 4: Creating a New Project with New Archetype

 Go to an empty folder and run below command to create a project with our custom archetype.

mvn archetype:generate

-DarchetypeGroupId=com.sahabt

-DarchetypeArtifactId=selenium-archetype

-DarchetypeVersion=1.0-SNAPSHOT

-DgroupId=com.sahabt

-DartifactId=automation

-Dversion=1.0-SNAPSHOT

By using groupdId, artifactId and version parameters, we set pom.xml related parameters.

Other parameters define the archetype.

Enjoy your archtype :)

2 thoughts on “How to Create Maven Archetype”

    • This is a commercial project and it is forbidden to share the whole code from the company. Please try to figure out how to do it by yourself. Thanks for your understanding. We are sorry. It is not in our hands to share the whole code.

      Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.