Create a new Maven project for our Java microservice – CM001

by | Nov 15, 2016




DeegeU Java Course

The “Create a new Maven project for our Java microservice” video is part of a larger microservices tutorial. This is the first video, introducing you to the series.

Transcript – Create a new Maven project for our Java microservice

Hi, in this video we’re going to create a new Maven project for our microservice. This will allow us to consistently build our application on different machines. The Apache Maven project will also help us later when we add our code to our continuous integration process.

We’re going to start with no code, and get a simple microservice running using Wildfly Swarm. This tutorial will just be on the application side. What that means is, we’re not connecting the continuous integration parts just yet.

This is the second video in a larger playlist for creating microservices in Java. You can find the link for the full playlist in the text below.

Overview for creating a new Maven project for our Java microservice

Here’s our plan for this video. We’ll create the Java project using the Maven quickstart archetype. We’ll make some changes to the Maven POM file to incorporate Wildfly Swarm, and then we’ll compile and test it to make sure everything is set up correctly. Our Java microservice won’t do anything yet, but this will make it easy for us to package our application as we add functionality.

Generate a basic Java app using Maven

Lets start creating our new maven project. We’re at a blank slate, so we’ll create a project using a basic Java JAR archetype in Maven. At a command prompt, here’s what we’ll enter.

mvn archetype:generate -DgroupId=com.deegeu -DartifactId=deegeu-quizzer -DarchetypeArtifactId=maven-archetype-quickstart

This will create a basic Java JAR project. If you’re not familiar with Maven, you might want to check out the Maven tutorials on DeegeU.com. We’ll accept the defaults for the version for now. When it’s done, there’s nothing added in our POM file other than JUnit.

Wildfly Swarm

Now that we have created a new Maven project, we need to set up our project to use Wildfly Swarm. Wildfly Swarm has everything we need to create a Java application in a single JAR. Swarm also provides other useful tools for implementing microservices in Java. Wildfly Swarm does provide a wizard to create projects depending on our requirements, but we’re going to manually add dependencies to our POM file. We’re doing it manually only so we better understand all the moving parts, but if you use Swarm for other projects, feel free to use the generator. It’s pretty cool!

Change the packaging to WAR

First, we need to change the Maven packaging to WAR. The reason is technically we’re building a web application. What will happen if we forget to change this is, it won’t trigger JAX-RS to look for our REST application. When we try to access endpoints, nothing will happen.

Define Maven properties for our build

Next we’ll create a properties section in our POM file and add our first property. It’s the WildFly Swarm version. At the making of this video, the current version is 1.0.0.Final.

We’ll also add a property for the source encoding. For almost everyone, it’s UTF-8, but adding this gets rid of a Maven warning.

We need to make sure Maven is using Java 8 for everything, so we’ll set the properties for that.

The failOnMissingWebXml property is because Maven complains when you do not add a web.xml file. That’s to tell Maven we don’t need the web.xml file.

<properties>
  <version.wildfly.swarm>1.0.0.Final</version.wildfly.swarm>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
  <failonmissingwebxml>false</failonmissingwebxml>
  <project.build.sourceencoding>UTF-8</project.build.sourceencoding>
</properties>

Add the Wildfly Swarm dependencies

Next we need to add the dependency for Wildfly Swarm using these Maven coordinates. This tells Maven to include Swarm libraries for our microservice. The version is set from the property we defined in our properties section.

<dependency>
  <groupid>org.wildfly.swarm</groupid>
  <artifactid>bom</artifactid>
  <version>${version.wildfly.swarm}</version>
  <scope>import</scope>
  <type>pom</type>
</dependency>

In order to package our microservice into one uber jar, we need to tell Maven how to build our application. We get that functionality by adding the wildfly-swarm-plugin.

Test our Java microservice

This is what our POM file looks like so far. Let’s build it to make sure everything is right. That command is mvn clean install. It might take a while for the first time compiling. Maven is downloading everything we need to implement the microservice. If everything is setup right, we should BUILD SUCCESS in the info logs from Maven.

Conclusion

That’s all there is to creating a new Maven project in Java. This setup allows us to build the code on any machine. It also imports easily in NetBeans.

Thanks for watching the video. You can find the code on GitHub, and the complete transcripts on DeegeU.com. Next we’ll take a look at the design to our Trivia microservice. If you like what you’re seeing or have any questions, let me know in the comments or on DeegeU.com! See you in the next tutorial!




Related Posts

Tools Used

  • Java
  • NetBeans

Media Credits

All media created and owned by DJ Spiess unless listed below.

  • No infringement intended

Get the code

The source code for “Are you ready to tackle the fizzbuzz test in Java?” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:

 git clone https://github.com/deege/deegeu-quizzer

Go to the Support > Getting the Code page for more help.

If you find any errors in the code, feel free to let me know or issue a pull request in Git.

Don't miss another video!

New videos come out every week. Make sure you subscribe!

Comments

comments

DJ Spiess

DJ Spiess

Your personal instructor

My name is DJ Spiess and I'm a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I've programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I've worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.

Pin It on Pinterest

Share This