In one of my previous blog posts, I showed you what you need to create your first Jakarta EE 8 project. Manually creating a Maven project for a Jakarta EE application from scratch is cumbersome. Especially for new developers, the folder and file setup might be overwhelming. To overcome this manual process and to quickly bootstrap a new Jakarta EE 8 Maven project in seconds, I've created a Maven archetype. This includes all the necessary ingredients (Java 11, MicroProfile, JUnit, Mockito, etc.) to kickstart your new project.
Read this blog post to understand how to use this archetype and bootstrap your new Jakarta EE 8 projects in seconds.
Bootstrap a new project with a Maven archetype
First, you need Maven (at least version 3) available on your machine. You can download Maven here and just have to put the Maven binary on your path. Next, open a terminal window and use the following command to create the project:
1 2 3 4 5 6 7 | mvn archetype:generate \ -DarchetypeGroupId=de.rieckpil.archetypes \ -DarchetypeArtifactId=jakartaee8 \ -DarchetypeVersion=1.0.0 \ -DgroupId=de.rieckpil.blog \ -DartifactId=jakartaee-8-project \ -DinteractiveMode=false |
HINT: Double-click on the code above to get the raw result to copy and paste it without any additional formatting.
You don't need to download anything in advance, as this command will download everything from Sonatype. It's important to specify the correct archetypeGroupId
, archetypeArtifactId
, archetypeVersion
like in the example above.
Both the groupId
and the artifactId
are free to choose. As a basic convention, I'll use the artifactId
for the folder name and the groupId
for the initial package structure.
The interactiveMode
attribute is optional but speeds up the process. Otherwise, you need to further confirm the creation of the project.
So the basic usage of this Maven archetype is the following:
1 2 3 4 5 | mvn archetype:generate -DarchetypeGroupId=de.rieckpil.archetypes \ -DarchetypeArtifactId=jakartaee8 \ -DarchetypeVersion=1.0.0\ -DgroupId=<your project Group Id> \ -DartifactId=<your project artifact Id> |
Components of the pre-defined Jakarta EE 8 project
For the initial components of this Jakarta EE 8 project, I try to keep it to the bare minimum. Alongside the Jakarta EE and MicroProfile specifications, JUnit 5 and Mocktio are available for efficient testing. Without further configurations, the pom.xml
of the project looks like the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | <?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>de.rieckpil.blog</groupId> <artifactId>jakartaee-8-project</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <failOnMissingWebXml>false</failOnMissingWebXml> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <jakarta.jakartaee-api.version>8.0.0</jakarta.jakartaee-api.version> <microprofile.version>3.0</microprofile.version> <mockito-core.version>3.1.0</mockito-core.version> <junit-jupiter.version>5.5.0</junit-jupiter.version> </properties> <dependencies> <dependency> <groupId>jakarta.platform</groupId> <artifactId>jakarta.jakartaee-api</artifactId> <version>${jakarta.jakartaee-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.eclipse.microprofile</groupId> <artifactId>microprofile</artifactId> <version>${microprofile.version}</version> <type>pom</type> <scope>provided</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>${junit-jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>${mockito-core.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>jakartaee-8-project</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> </plugin> </plugins> </build> </project> |
Furthermore, the project includes a basic persistence.xml
, microprofile-config.properties
, beans.xml
file to start using JPA, CDI and MicroProfile Config.
In addition a first JAX-RS resource is available to ensure the package structure is created and the application is up- and running:
1 2 3 4 | @ApplicationPath("resources") public class JAXRSConfiguration extends Application { } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | @Path("sample") public class SampleResource { @Inject @ConfigProperty(name = "message") private String message; @GET public Response message() { return Response.ok(message).build(); } } |
Build and deploy the application
As a default deployment target, you'll get Open Liberty. The project includes a Dockerfile
to create a Docker container of your application running on Open Liberty:
1 2 3 | FROM open-liberty:kernel-java11 COPY --chown=1001:0 target/jakartaee-8-project.war /config/dropins/ COPY --chown=1001:0 server.xml /config |
The configuration for Open Liberty is basic, but enough and can be extended or reconfigured at any time:
1 2 3 4 5 6 7 8 9 10 11 12 | <?xml version="1.0" encoding="UTF-8"?> <server description="new server"> <featureManager> <feature>javaee-8.0</feature> <feature>microProfile-3.0</feature> </featureManager> <httpEndpoint id="defaultHttpEndpoint" httpPort="9080" httpsPort="9443"/> <quickStartSecurity userName="duke" userPassword="dukeduke"/> </server> |
HINT: For production usage, make sure to further adjust the security settings properly.
To serve the application from the root path, I'm including a ibm-web-ext.xml
file with to configure the context root path:
1 2 3 4 5 6 7 | <web-ext xmlns="http://websphere.ibm.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-ext_1_0.xsd" version="1.0"> <context-root uri="/"/> </web-ext> |
Last but not least, you can build and deploy the application with a script for convenience. It's available for both Windows buildAndRun.bat
) and Mac/Linux (buildAndRun.sh
). Once you execute it, Maven will create the .war
file and a new Docker container is spawned.
Once the application is up- and running, you can visit http://localhost:9080/resources/sample and should get a greeting.
In total, these are the required commands to have your next Jakarta EE 8 Maven project up in seconds:
1 2 3 | mvn archetype:generate -DarchetypeGroupId=de.rieckpil.archetypes -DarchetypeArtifactId=jakartaee8 -DarchetypeVersion=1.0.0 -DgroupId=de.rieckpil.blog -DartifactId=jakartaee-8-microservice -DinteractiveMode=false cd jakartaee-8-microservice ./buildAndRun.sh |
Live demo of this Jakarta EE 8 archetype
If you are looking for further Maven archetypes to bootstrap a Java EE 8 project with or without JSF setup, have a look at the following tutorials:
- #HOWTO: Bootstrap a JSF 2.3 Maven project in seconds
- #HOWTO: Bootstrap a Java EE 8 and MicroProfile 2.0.1 Maven project in seconds
You can find the archetype on GitHub.
Have fun creating new Jakarta EE 8 projects with Maven,
Phil