Maven Archetypes

by | Apr 1, 2015




This video describes the Maven archetypes.  It was originally part of a larger unpublished video tutorial on creating JAXRS applications using Java.  The video was originally posted to YouTube on May 18, 2014.

Maven archetypes

In this video, we’ll look at how to generate Java projects using Maven. We will also learn about Maven archetypes, and how they help us generate the code we need for individual projects.

Transcript

Hello again!

So far we’ve just installed Maven. Now this tool will help us organize and build our Java application. In this video, we’ll look at how to generate Java projects using Maven. We will also learn about Maven archetypes, and how they help us generate the code we need for individual projects.

To start, lets look at the simplest POM file. This file contains all the information you need to compile your application. Pretty simple, right? That’s it, add this file to a code directory and you’re good to go.

It almost sounds too crazy to be true so, let’s go try it. First create a directory from the command prompt. Now you can use any name, I’m going to use testapp. Then create a text file with this information and name it pom.xml. To save yourself from typing, you can also use the pom file included in the code supplement for this video. Once we’re done, go back to your command prompt.

Let’s type in the command mvn install. You’ll see lots of messages fly by your window. It’s nothing we need to worry about now. Basically it’s compiling all the code files, but in our case we don’t have any, and creating a jar file. Our code is compiled and we even have a compiled jar with no code in it.

So you are probably asking how in the heck does all this work?

Maven makes many assumptions about your project and project structure. Maven expects all of your code to be in a directory called /src/main/java, and all unit tests are assumed to be under test/java. These directories are relative from the point where the pom file exists. There are ways to change these directories, but it’s easier to just use the defaults. The defaults are based on years of experience and java best practices.

So your next thought might be, well if Maven is assuming where everything is, why do I need to create anything.

Why do I need to create the POM file, and shouldn’t the startup be the same for all projects?

Well, in a way, yes. Maven uses something called an archetype. An archetype is a Maven project templating toolkit. It allows you to start with a project that is already set up to use the best practices. There are archetypes to create jar files, web archives, enterprise archives, portlets, Eclipse plugins, and just about every project you might need to create. There is even an archetype to create a project for defining, well, new archetypes.

So to start, lets create a simple Hello World jar.

In a directory where you keep your code lets type the following command

mvn archetype:generate -DgroupId=com.yourcompany -DartifactId=helloWorldApp
-DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0-SNAPSHOT

Now the group id here is where we want your project to live in the repository on your machine. You don’t totally really need to understand that bit right now. Usually we use the base package that we plan to use for our project. In this example, we’re going to use com.yourcompany for now.

Now the artifact id here tells us the name of our project. You will want to use one single word without spaces here. This will end up being the name of the jar, war, or ear or whatever project you create. It will also be the directory where your project lives. For this example, we’ll use helloWorldApp.

So lets run this command and see what happens. So you’ll want to confirm your selections by typing in Y at the prompt. And then we’re done.

Your project should look something like this.

Now open the POM file in either textedit or notepad or whatever you’re using to open text files on your system. You will see it is similar to the POM file that we looked at earlier.

This POM file also added something called a dependency. A dependency is a library that your project depends on.

The dependency in this project is for JUnit, so the project we just created already has JUnit testing built in and ready to go. We can add similar XML stanzas to add other dependencies to our project. We just need to specify the correct group id, artifact id, and version. You’ll notice the JUnit dependency stanza also specifies a scope. The scope tells Maven when to include the dependency.

Now at the top of your POM file, you’ll notice the group id and artifact id we provided are also in the POM file. If we were to include our project in another project, we’d include these values the same way we referenced JUnit.

So what’s great about all this is we can take this directory, give it to someone else, and if they have Maven installed, everything will just compile using the same commands. There is no special set up need to run this on another machine.

So in this video we’ve looked at POM files and archetypes. We’ve also had a a quick look at how dependencies are added to our Maven projects. Later in the course when we are ready, we’ll use a different archetype for our web service project. For now, we’ll continue exploring the basics of Maven using this archetype. In the next video, we’ll look at how we can specify other Java libraries while we take a deeper look at the maven POM file.







Tools Used

  • Maven

Media Credits

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

  • No infringement intended

Get the code

The source code for “What are Maven Archetypes” 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-java-intro.git

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