Before we dive in, it’s important to know why we use maven at all.
Why do we use Maven?
Maven helps us build java projects faster. It automates our build process by:
- Downloading dependencies
- Running unit tests
- Packaging artifacts
Maven creates a simple project structure that can be easily understood and shared.
It's our handy build assistant…and it can learn new skills too!
How do we set Maven up?
What You'll Need
- JBoss Developer Studio w/ JBoss EAP 6.4 or higher. Download here (Steps 1 & 2)
Create a Project
If you’re using an IDE, this is fairly easy. I’m using JBoss Developer Studio, which is basically a flavor of Eclipse with a JBoss Application server runtime.
Step 1: Open JBoss Developer Studio
Step 2: Go to File -> New -> Project -> Maven Project. Click Next.
Step 3: Make sure Create a simple project is left unchecked. Click Next.
Step 4: Select the Internal catalog and find maven-archetype-quickstart
Step 5: Fill in the following and Click Finish
- groupId - a unique identifier for your project. It's usually your domain in reverse order. Ex. org.codelikethewind, org.apache
- artifactId - the name of your project. This will also be the name of our JAR. Ex. simple-maven-project
Step 6: Update Java to 1.6
- Click on the pom.xml. Under Properties, create two properties
Name(s):maven.compiler.source & maven.compiler.target Value:1.6
Step 7: Update Maven
- Right click your project -> Maven -> Update Project
Maven’s standard templates make creating a project very simple. Let’s look around.
POM File (Project Object Model)
Open the pom.xml file and click the pom.xml tab
This file defines our project and specifies how it will be built. Right now, you’ll see
- Our project identifiers (groupId, artifactId, version, name)
- The packaging type (JAR)
- Our list of dependencies
Click on the Dependencies tab.
When you build a project, Maven automatically downloads all required dependencies. This saves us from the frustrating task of managing dependencies ourselves.
There should be one dependency in your pom file called JUnit.
When you build a project, Maven automatically runs your unit tests. Just make sure they are under the src/test/java folder.
There should be a sample test called AppTest
Instead of having to manually package our JAR files, Maven will do this for us. The packaging element we saw in our pom file defines the type of artifact maven will create.
Every capability in Maven is defined as a plugin. If we want to give Maven a new skill, we can define a new plugin in our POM file and it will run during the build cycle.
Ex. jboss-as plugin - after building, maven will automatically deploy applications to a remote JBoss Application Server
Build Your First Maven Project
Step 1: Right click the top level folder of your project -> Click Maven Build
Step 2: In the goals section, type clean install. Click Run
Step 3: You should see a BUILD SUCCESS message
Maven downloaded our dependencies, ran our unit tests, compiled our artifacts. In one click.
Note: the actual build artifacts are stored in the target folder
Run Your First Maven Project
Our sample application prints a simple “Hello World” string to the console.
- Right click the top level folder of your project -> Run-As -> Java Application
- App should be selected. Click Ok.
- "Hello World!"
What’s a best practice for using Maven?
At some point, you will run into the following scenario:
“I don’t see my code changes!?!”
Not to fear. Most likely Maven is using your old build artifacts.
So whenever building a project, remember to use the ‘clean install’ goals like we did above.
The extra clean step will remove old artifacts before creating new ones.
- Maven helps us build java projects quickly by automatically downloading dependencies, running unit tests, and packaging our artifacts
- Maven templates are great starting points for creating new projects
- Use maven clean install goals to remove old artifacts before creating new ones.
Source Code & Useful Links