At this point of the automation series, you already know the basic concepts needed to understand the purpose of Maven (what is it, COC, installation). So, creating a project will start this journey to apply an important automation part to your project.
Project Object Model
The Maven core is the Project Object Model, known as POM. This file will provide the uniform build system to the project. Once you learn how to deal with this configuration, you will master any other Maven project. Amazing isn’t it?
Minimal POM
The POM starts with a <project>
tag that stores all configuration. There are few tags to set the minimum for every project.
- modelVersion: should be set to 4.0.0. It’s to ensure the Maven 2 or 3 support although there is no other version at the moment.
- groupId: the unique identification to your group, a unique package. Usually the company organization like tech.cyborgdeveloper.
- artifactId: the project id, the name, like my-awesome-app.
- version: the current project version like 1.0, 3.0.2, usually bound with a SNAPSHOT or others release versions.
<project> <modelVersion>4.0.0</modelVersion> <groupId>tech.cyborgdeveloper</groupId> <artifactId>my-awesome-app</artifactId> <version>3.0.2-SNAPSHOT</version> </project>
Enhancing your POM
Maven simplifies the project configuration using the concept of Super POM. This is a default setting extended by POMs, so you don’t need to configure common things for all projects. But if you need it, you can override any that you want to.
An extra important config to make things explicit is <packaging>
tag. This is how your project will be distributed. By default, it’s defined as jar
. But you can set as war
or ear
. Or an advanced approach as pom
, for modules.
There are other informative tags like <name>
and <url>
that are helpful to project description.
On the upcoming posts, I will explain many others important tags to the project configuration.
Creating a Project
The most common way to handle Maven project is by using IDEs. Eclipse, Intellij, NetBeans are the most known. It’s simple to import project or create new ones.
All available settings are the same as command line options. But it’s easier and faster to use.
Command Line
Let’s create a simple example based on our previous POM. But in this case, I will consider it as a web project. So the packaging
will be defined as .war
.
Find your desired directory to execute the command below:
mvn archetype:generate -DgroupId=tech.cyborgdeveloper -DartifactId=maven-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
- archetype:generate: Maven plugin that contains the goal to make project creation easier.
- -DgroupId and -DartifactId: Values explained on POM section.
- -DarchetypeArtifactId: The project type needed. In this case a web project.
- -DinteractiveMode: Avoid filling the rest of needed configuration, applying the default values.
Another way is to execute the minimum command bellow and being asked for all these values in an interactive way.
mvn archetype:generate
As the documentation says: “If you have just installed Maven, it may take a while on the first run. This is because Maven is downloading the most recent artifacts (plugin jars and other files) into your local repository.”
After the executed command a folder will be created with the DartifactId name.
Archetype Examples
These archetype create the configuration to start the project in a right way. Check all standard archetypes here.
- maven-archetype-webapp to create a web app (the same used above).
maven-web/ ├── pom.xml └── src └── main ├── resources └── webapp ├── WEB-INF │ └── web.xml └── index.jsp
- maven-archetype-quickstart to create a simple Java project.
maven-java/ ├── pom.xml └── src ├── main │ └── java │ └── tech │ └── cyborgdeveloper │ └── App.java └── test └── java └── tech └── cyborgdeveloper └── AppTest.java
- maven-archetype-j2ee-simple to create an advanced JEE project using Maven modules.
maven-jee/ ├── ear │ └── pom.xml ├── ejbs │ ├── pom.xml │ └── src │ └── main │ └── resources │ └── META-INF │ └── ejb-jar.xml ├── pom.xml ├── primary-source │ └── pom.xml ├── projects │ ├── logging │ │ └── pom.xml │ └── pom.xml ├── servlets │ ├── pom.xml │ └── servlet │ ├── pom.xml │ └── src │ └── main │ └── webapp │ ├── WEB-INF │ │ └── web.xml │ └── index.jsp └── src └── main └── resources
Searching Archetypes
It’s also possible to search specific projects in an interactive way. This command filter groupId containing org.apache
and artifactId containing struts
.
mvn archetype:generate -Dfilter=org.apache:struts
It results on a list of Struts 2 project types. Just follow the steps to finish project creation.
... [omitted values] [INFO] Generating project in Interactive mode [INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0) Choose archetype: 1: remote -> org.apache.struts:struts2-archetype-angularjs (-) 2: remote -> org.apache.struts:struts2-archetype-blank (-) 3: remote -> org.apache.struts:struts2-archetype-convention (-) 4: remote -> org.apache.struts:struts2-archetype-dbportlet (-) 5: remote -> org.apache.struts:struts2-archetype-plugin (-) 6: remote -> org.apache.struts:struts2-archetype-portlet (-) 7: remote -> org.apache.struts:struts2-archetype-starter (-) Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 2 Choose org.apache.struts:struts2-archetype-blank version: 1: 2.2.1 2: 2.2.1.1 3: 2.2.3 ...
A simpler search for Spring MVC projects.
$ mvn archetype:generate -Dfilter=spring:mvc ... [omitted values] Generating project in Interactive mode [INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0) Choose archetype: 1: remote -> org.jboss.spring.archetypes:jboss-spring-mvc-archetype (An archetype... 2: remote -> org.jboss.spring.archetypes:spring-mvc-webapp (An archetype... ...
Check out the awesome created project:
maven-web-spring/ ├── README.md ├── pom.xml └── src ├── main │ ├── java │ │ └── tech │ │ └── cyborgdeveloper │ │ ├── controller │ │ │ ├── MemberController.java │ │ │ └── MemberRestController.java │ │ ├── data │ │ │ ├── MemberDao.java │ │ │ └── MemberDaoImpl.java │ │ └── model │ │ └── Member.java │ ├── resources │ │ ├── META-INF │ │ │ ├── persistence.xml │ │ │ └── spring │ │ │ ├── applicationContext.xml │ │ │ └── infrastructure.xml │ │ └── import.sql │ └── webapp │ ├── WEB-INF │ │ ├── jboss-as-spring-mvc-context.xml │ │ ├── jboss-deployment-structure.xml │ │ ├── spring-quickstart-ds.xml │ │ ├── views │ │ │ └── index.jsp │ │ └── web.xml │ └── resources │ ├── css │ │ └── screen.css │ └── gfx │ ├── asidebkg.png │ ├── banner.png │ ├── bkg-blkheader.png │ ├── headerbkg.png │ └── rhjb_eap_logo.png └── test ├── java │ └── tech │ └── cyborgdeveloper │ └── test │ └── MemberDaoTest.java └── resources ├── META-INF │ └── test-persistence.xml ├── import.sql └── test-context.xml
Next Step
On the next post, I will show how Maven defines or expect the project folder structure. It’s the key part to start the migration of your non-Maven project.
Let’s automate!