Do you want to learn how to create a basic Maven archetype for setting up quickly a WildFly project? then keep reading!

Basically a Maven archetype is made up of the following elements:

  • An archetype descriptor (archetype-metadata.xml in directory: src/main/resources/META-INF/maven/). It lists all the files that will be contained in the archetype and categorizes them so they can be processed correctly by the archetype generation mechanism..
  • The prototype files which are copied by the archetype plugin (directory: src/main/resources/archetype-resources/).
  • The prototype pom (pom.xml in: src/main/resources/archetype-resources).
  • The pom for the archetype (pom.xml in the archetype's root directory).

The good news is that you don't need to create this structure by yourself. You can create it from an archetype follow as follows:

mvn -B archetype:generate -DarchetypeArtifactId=maven-archetype-archetype \
  -DarchetypeGroupId=maven-archetype \ -DgroupId=com.mastertheboss -DartifactId=wildfly-archetype

The following structure will be generated:

wildfly-project/
├── pom.xml
└── src
    ├── main
    │   └── resources
    │       ├── archetype-resources
    │       │   ├── pom.xml
    │       │   └── src
    │       │       ├── main
    │       │       │   └── java
    │       │       │       └── App.java
    │       │       └── test
    │       │           └── java
    │       │               └── AppTest.java
    │       └── META-INF
    │           └── maven
    │               └── archetype-metadata.xml
    └── test
        └── resources
            └── projects
                └── it-basic
                    ├── archetype.properties
                    └── goal.txt

First off, let's remove the App.java and AppTest.java which are not required in our WildFly project. Then, let's define the pom for the archetype (top's pom.xml):

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mastertheboss.archetypes</groupId>
    <artifactId>maven-archetype</artifactId>
    <version>1.0-SNAPSHOT</version>
	<name>wildfly-archetype</name>
    <packaging>maven-archetype</packaging>
    <description>Archetype used to generate rest application based on jaxrs 2.1</description>
	
    <build>
        <extensions>
            <extension>
                <groupId>org.apache.maven.archetype</groupId>
                <artifactId>archetype-packaging</artifactId>
                <version>${archetype-packaging.version}</version>
            </extension>
        </extensions>

    </build>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <archetype-packaging.version>3.0.1</archetype-packaging.version>
    </properties>

</project>

Next, the prototype pom which contains the actual WildFly's dependencies:

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>${groupId}</groupId>
    <artifactId>${artifactId}</artifactId>
    <version>${version}</version>
    <packaging>war</packaging>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <version.server.bom>${version-wildfly}</version.server.bom>
    </properties>

    <build>
        <finalName>${artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.wildfly.plugins</groupId>
                <artifactId>wildfly-maven-plugin</artifactId>
                <version>2.0.0.Final</version>
            </plugin>
        </plugins>
    </build>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.wildfly.bom</groupId>
                <artifactId>wildfly-javaee8-with-tools</artifactId>
                <version>${version.server.bom}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>

        <dependency>
            <groupId>org.jboss.spec.javax.ws.rs</groupId>
            <artifactId>jboss-jaxrs-api_2.1_spec</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <scope>provided</scope>
        </dependency>

    </dependencies>

</project>

Within the archetype-metadata.xml define which files will be include in the project. In our case the example Java files:

<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
        http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
        xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        name="wildfly">

    <requiredProperties>
        <requiredProperty key="version-wildfly">
            <defaultValue>17.0.0.Final</defaultValue>
        </requiredProperty>
    </requiredProperties>


    <fileSets>
        <fileSet filtered="true" packaged="true">
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.java</include>
            </includes>
        </fileSet>

    </fileSets>

</archetype-descriptor>

Here are a few important tags and their purpose:

  • Required Property can be used to define properties that can be injected in the prototype pom. In our case "wildfly-version" will be prompted if running in interactive mode.
  • FileSet defines how to use the project files located in the jar file to generate a project. If a file or a directory name contains __property__ pattern, it is replaced with corresponding property value
  • packaged = “true” means that the selected files will be generated in a directory structure that is prepended by the package property

Finally, let's add the Java class files, which are added in src/main/resources/archetype-resources/src/main/java. Here is AppConfig.java:

package ${package};

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/rest")
public class AppConfig extends Application {
}

And here is the HelloREST.java file:

package ${package};

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;

@Path("/hello")
public class HelloREST {

    @GET
    public Response hello(){

        return Response
                .ok( "Hello World")
                .build();
    }

}

This is the final structure of your Archetype project:

src
└── main
    └── resources
        ├── archetype-resources
        │   ├── pom.xml
        │   └── src
        │       └── main
        │           └── java
        │               ├── AppConfig.java
        │               └── HelloREST.java
        ├── logback.xml
        └── META-INF
            └── maven
                └── archetype-metadata.xml

You need to install the archetype on your local repository with:

$ mvn clean install

Using the archetype to create a WildFly project

In order to use our archetype, we will refer to the com.mastertheboss.archetypes we have just installed, adding the required GAV information:

mvn archetype:generate -DarchetypeGroupId=com.mastertheboss.archetypes  -DarchetypeArtifactId=maven-archetype -DarchetypeVersion=1.0-SNAPSHOT -DgroupId=com.example -DartifactId=demo-rest -Dversion=1.0-SNAPSHOT

You will be then prompted for the wildfly-version:

[INFO] Using property: groupId = com.example
[INFO] Using property: artifactId = demo-rest
[INFO] Using property: version = 1.0-SNAPSHOT
[INFO] Using property: package = com.example
[INFO] Using property: version-wildfly = 17.0.0.Final
Confirm properties configuration:
groupId: com.example
artifactId: demo-rest
version: 1.0-SNAPSHOT
package: com.example
version-wildfly: 17.0.0.Final
 Y: : Y

And finally your project will be generated. Here's the directory structure of it:

demo-rest/
├── pom.xml
└── src
    └── main
        └── java
            └── com
                └── example
                    ├── AppConfig.java
                    └── HelloREST.java

Build and install it with:

$ mvn clean install wildfly:deploy

Test it with:

curl http://localhost:8080/demo-rest/rest/hello
Hello World

Conclusion

We have just described how to create a Java Enterprise project using WildFly dependencies from a Maven archetype.

You can find the source code for it here: https://github.com/fmarchioni/mastertheboss/tree/master/maven-archetype

0
0
0
s2sdefault

Related articles available on mastertheboss.com