Creating a Maven archetype for WildFly – Part 1

In this two-part tutorial we will learn how to create a Maven archetype for WildFly that you can use to speed up your project creation either from the command line or using an IDE

The first part of this tutorial will demonstrate how to create a basic archetype for a Web application which contains the needed dependencies in their place, and some useful plugins. This archetype will provide just the basic structure for your project. In the next tutorial we will add also test tooling configuration (Arquillian).

Creating a Maven archetype is straightforward process. An archetype in a nuthsell is a plugin, that contains the project prototype you wish to create and consists of:

  • An archetype descriptor (archetype-metadata.xml in directory: src/main/resources/META-INF/maven) which lists all the files that will be contained in the archetype
  • The resources that make up the archetype (in: src/main/resources/archetype-resources/)
  • The prototype for the pom.xml (in: src/main/resources/archetype-resources)
  • A pom.xml file which describes the archetype (pom.xml in the archetype’s root directory).

Here is the initial project structure for our archetype:

├── pom.xml
├── src
│   └── main
│       └── resources
│           ├── archetype-resources
│           │   ├── pom.xml
│           │   └── src
│           │       ├── main
│           │       │   ├── java
│           │       │   └── webapp
│           │       │       ├── index.html
│           │       │       └── WEB-INF
│           │       │           └── beans.xml
│           │       └── test
│           │           ├── java
│           │           └── resources
│           └── META-INF
│               └── maven
│                   └── archetype-metadata.xml

The first piece is the pom.xml which is located the root of the project, and contains the groupId,artifactId and version for our Archetype:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.wildfly</groupId>
  <artifactId>webapp-wildfly</artifactId>
  <version>12</version>
  <name>Archetype Web Applications on WildFly</name>
  <url>http://www.wildfly.org</url>
</project>

Next, we need the actual pom.xml which will be generated in your project, when we use our archetype:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>${groupId}</groupId>
   <artifactId>${artifactId}</artifactId>
   <version>${version}</version>
   <packaging>war</packaging>
   <name>WildFly Webapp Archetype</name>
   <properties>
      <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <version.wildfly.plugin>1.2.1.Final</version.wildfly.plugin>
   </properties>
   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>org.wildfly.bom</groupId>
            <artifactId>wildfly-javaee7-with-tools</artifactId>
            <version>12.0.0.Final</version>
            <type>pom</type>
            <scope>import</scope>
         </dependency>
      </dependencies>
   </dependencyManagement>
   <dependencies>

   </dependencies>
   <build>
      <finalName>${project.artifactId}</finalName>
      <plugins>
         <plugin>
            <groupId>org.wildfly.plugins</groupId>
            <artifactId>wildfly-maven-plugin</artifactId>
            <version>${version.wildfly.plugin}</version>
         </plugin>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
               <source>1.8</source>
               <target>1.8</target>
               <compilerArguments>
                  <endorseddirs>${endorsed.dir}</endorseddirs>
               </compilerArguments>
            </configuration>
         </plugin>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
               <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
         </plugin>
      </plugins>
   </build>
   
</project>


As you can see, the project contains in the first block a set of properties which will be replaced by your project’s actual configuration:

   <groupId>${groupId}</groupId>
   <artifactId>${artifactId}</artifactId>
   <version>${version}</version>

Then, we have included WildFly 12 BOM and a set of libraries you might need in your project. These libraries are commented for your convenience so you can uncomment just the ones you need. Finally, the wildfly-maven-plugin has been included as well so you can deploy/undeploy the project directly from Maven.

The other file we need is archetype-metadata.xml (in src/main/resources/META-INF/maven) which contains the list of files that need to be included in our Archetype. Since our Archetype merely adds an index.html welcome page, we will instruct it to include web pages from the src/main/webapp. In the next tutorial we’ll see how to generate sample Java classes and Test classes in our project.

<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0 http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd" 
                      name="JavaEE7 web application archetype"
    xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <fileSets>
    <fileSet filtered="true" encoding="UTF-8">
      <directory>src/main/webapp</directory>
      <includes>
        <include>**/*.jsp</include>
        <include>**/*.html</include>
        <include>**/*.xml</include>
      </includes>
    </fileSet>
</archetype-descriptor>

That’s all you need to create the archetype. We have included as well a WEB-INF/beans.xml file so that you have it already there, should you need CDI (remember you need to include its dependencies as well in pom.xml)

Now build install the archetype in your local repository with:

$ mvn clean install archetype:update-local-catalog

You will be informed that the archetype has been created by:

[INFO] Installing /home/jboss/maven-projects/webapp-wildfly/target/webapp-wildfly-12.jar to /home/jboss/.m2/repository/org/wildfly/webapp-wildfly/12/webapp-wildfly-12.jar
[INFO] Installing /home/jboss/maven-projects/webapp-wildfly/pom.xml to /home/jboss/.m2/repository/org/wildfly/webapp-wildfly/12/webapp-wildfly-12.pom
[INFO] 
[INFO] --- maven-archetype-plugin:3.0.1:update-local-catalog (default-cli) @ webapp-wildfly ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.993 s
[INFO] Finished at: 2018-05-24T17:38:17+02:00
[INFO] ------------------------------------------------------------------------

Now let’s test it!

Since the archetype is in the local repository we can create a new Web application from it with:

$ mvn archetype:generate -Darchetype.interactive=false   --batch-mode -DarchetypeGroupId=org.wildfly -DarchetypeArtifactId=webapp-wildfly -DarchetypeVersion=12 -DgroupId=com.example -DartifactId=demoweb

Here is the project which has been created for you:

├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── example
│   │   └── webapp
│   │       ├── index.html
│   │       └── WEB-INF
│   │           └── beans.xml
│   └── test
│       ├── java
│       │   └── com
│       │       └── example
│       └── resources

Enter the folder of your application, and try to build and deploy it with:

$ mvn clean install wildfly:deploy

You can download this archetype and use it from here.

In the next tutorial (Creating a Maven archetype for WildFly – Part 2), we will learn how to create a more complex Archetype which contains some Java classes and a test class with Arquillian. Stay tuned!