In this tutorial we will learn how to use WildFly Maven archetype to speed up your project creation either from the command line or using an IDE. We will also show how to use the built-in test Classes to test the built-in resources.
Maven archetypes in a nutshell
An archetype in a nutshell 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).
WildFly provides a series of Maven archetypes to simplify the creation of Enterprise Applications:
- wildfly-jakartaee-webapp-archetype: WildFly archetype for blank WAR project
- wildfly-jakartaee-ear-archetype: WildFly archetype for blank EAR project
- wildfly-subsystem: WildFly archetype for a subsystem project
- wildfly-getting-started-archetype: WildFly archetype with sample code
To learn more about subsystem creation in WildFly, check this article: Creating a new subsystem in WildFly made simple
In this tutorial we will show how to use the latest one, which includes also a basic REST Application to give you the foundations for a Jakarta EE Applications. You can check the available versions for this archetype on the default Maven repository: https://mvnrepository.com/artifact/org.wildfly.archetype/wildfly-archetypes
How to create an application using WildFly archetype
To create an application, refer to the wildfly-getting-started-archetype
providing the Maven GAV coordinates for your project. For example:
mvn archetype:generate -DgroupId=my.project.org -DartifactId=sampleproject -Dversion=1.0-SNAPSHOT -DarchetypeGroupId=org.wildfly.archetype -DarchetypeArtifactId=wildfly-getting-started-archetype -DarchetypeVersion=31.0.0.Final
Let’s have a closer look at our application using an IDE:
The sample project is basically a REST Service which contains the following built-in endpoint:
import jakarta.inject.Inject; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.PathParam; import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; @Path("/") public class GettingStartedEndpoint { @Inject private GettingStartedService service; @GET @Path("/{name}") @Produces(MediaType.TEXT_PLAIN) public Response sayHello(final @PathParam("name") String name) { String response = service.hello(name); return Response.ok(response).build(); } }
The archetype also created two Arquillian Test Classes. The first one uses Jakarta HTTP Client API to Test the REST Endpoint:
@RunAsClient @ExtendWith(ArquillianExtension.class) public class GettingStartedApplicationIT { @Test public void testHelloEndpoint() { try (Client client = ClientBuilder.newClient()) { Response response = client .target(URI.create("http://localhost:8080/")) .path("/hello/World") .request() .get(); assertEquals(200, response.getStatus()); assertEquals("Hello 'World'.", response.readEntity(String.class)); } } }
Then, the second one, on the other hand, uses CDI Injection to test directly the Service:
@ExtendWith(ArquillianExtension.class) public class GettingStartedServiceIT { @Deployment public static WebArchive createTestArchive() { return ShrinkWrap.create(WebArchive.class, "GettingStartedServiceIT.war") .addClass(GettingStartedService.class); } @Inject GettingStartedService service; @Test public void testService() { String result = service.hello("World"); assertEquals("Hello 'World'.", result); result = service.hello("Monde"); assertEquals("Hello 'Monde'.", result); } }
Building and Testing
To build and test the application you can just run the goal “install” from the Command Line:
mvn install
As a result of the build, you should see the outcome of the two Tests which are in your project:
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 7.510 s -- in org.wildfly.examples.GettingStartedServiceIT [INFO] Running org.wildfly.examples.GettingStartedApplicationIT [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.437 s -- in org.wildfly.examples.GettingStartedApplicationIT 12:34:07,853 INFO [org.jboss.as.server] (management-handler-thread - 1) WFLYSRV0272: Suspending server
To deploy the application on a running WildFly Server you can use the WildFly Maven plugin:
mvn wildfly:deploy
Please notice that the default pom.xml
is configured to deploy the application on the Root Web Context:
<finalName>ROOT</finalName>
As a result, you can access your application at localhost:8080
Conclusion
This article was a quick walk through the set up, build and test of a REST Application using WildFly Maven archetype wildfly-getting-started-archetype
. You can experiment variations of this example choosing a different archetype from the repository.