Saturday, 31 January 2015

Create a simple project using Maven

Creating a mavenSW project is extremely easy using the maven archetype plugin. Information about archetypes can be found athttp://maven.apache.org/guides/introduction/introduction-to-archetypes.html. We can create a maven project using the "archetype:create" goal in the following manner.
mvn archetype:create -DgroupId=com.test -DartifactId=mytest
This command states to create a project called "mytest" specified by the artifactId. The project belongs to the "com.test" group, specified by the groupId. This group specifies essentially where the project is located hierarchically in a maven repository, and it allows for the grouping of similar projects into similar groups.
Now, I'll run the "archetype:create" goal at the command prompt.
'mvn archetype:create -DgroupId=com.test -DartifactId=mytest' at command prompt
This is the first time I've run the command, so maven will download the resources that it needs from the central repository to my local maven repository. When the goal finishes, we can see that the "mytest" project was successfully created.
'mytest' project successfully created
If we go to Windows Explorer, we can examine the project that we just created. I ran the goal in C:\dev\test, so the "mytest" project was created at C:\dev\test\mytest.
'mytest' project created at C:\dev\test
Notice that mytest contains a set of directories. The src/main/java directory is the default JavaSW source directory for maven projects. This is where you typically place your source code. This directory location is configurable, but in general it's best to stay with the maven defaults unless you have a very compelling reason to change them. Notice that the com.test package structure was created within src/main/java, and that an App.java file was created in src/main/java/com/test. This is a simple HelloWorld-style class.
Sample JUnit test created
The src/test/java directory is the default location for JUnit tests, which are integrated into the maven lifecycle. The com.test package structure was created within src/test/java, and a simple JUnit test class called AppTest.java was created in src/test/java/com/test.
Of great importance is the pom.xml file that was created in the mytest directory (the root level of the project).
pom.xml
The pom.xml (Project Object Model) file is essentially the project's metadata file that describes the various aspects of the project. It describes the project's name, group, version, dependencies, and many many other things. The contents of the generated pom.xml file are shown below.

pom.xml

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>mytest</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>mytest</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
Notice that the pom.xml file features an XSD. This is extremely useful since it allows us to do things such as code-completion in a good XMLW editor, such as the one in EclipseSW.
The pom.xml file has some features worth pointing out. The "modelVersion" refers to which object model the POMW is using. You rarely need to be concerned with this value. The "packaging" refers in general to the type of package that we are building with this project (such as jarW, warW, or ear). The "version" element refers to the version number of that artifact that we are building into a package. This is the number extension that comes in a package file name, such as commons-blah-1.2.3.jar. The "name" element is the display name of the project, as opposed to the "artifactId", which is the artifact file name. So, a project "name" might be "The Blah Project" while "artifactId" might be "commons-blah". The "url" element is to specify the location of the project's "site". A "site" is the generated documentation for a project.
Also, note the junit dependency that's been included in the pom.xml file. The AppTest class is a JUnit test so the project requires the junit dependency.
That about covers it for creating a basic mavenSW project using the "archetype:create" goal.

No comments:

Post a Comment