Apache Maven is a software project management and comprehension tool. It manages the project build using the project object model (POM) XML. The pom.xml
file is the Project Object Model (POM), a declarative description of a project. Other than the project’s POM, there is also a super POM defined within Maven. When a maven goal is run, it always executes against an effective POM, a combination of settings from this project’s pom.xml
, all parent POMs and the Super POM.
Generate a Maven Project
In order to create a maven project, we need to execute the below command:
mvn archetype:generate -DgroupId=com.javarticles.maven -DartifactId=mavensamples -DpackageName=com.javarticles.maven
archetype:generate
is the plugin goal where archetype
is the identifier of a plugin and generate
is the identifier of a goal. The prefix archetype
is the plugin that contains the goal. A Maven plugin is a collection of one or more goals and a goal is similar to ant’s concept of task.
The generate
goal creates a simple project based upon an archetype. There are many examples of in-built plugins, for example, a Jar plugin that contains goals for creating JAR files.
Goals are configured via configuration properties that can be used to customize behavior. For our generate
goal, we have passed key configuration elements groupId
, artifactId
and packageName
. These are called Maven coordinates, which uniquely identify a project.
You can specify the archetype kind as a configuration parameter based on the type of project. By default it is quick start up.
archetypeArtifactId
is the configuration parameter and its default value is set to maven-archetype-quick
.
start
Output:
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Stub Project (No POM) 1 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] >>> maven-archetype-plugin:2.4:generate (default-cli) > generate-sources @ standalone-pom >>> [INFO] [INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) < generate-sources @ standalone-pom <<< [INFO] [INFO] --- maven-archetype-plugin:2.4:generate (default-cli) @ standalone-pom -- - [INFO] Generating project in Interactive mode [INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven. archetypes:maven-archetype-quickstart:1.0) .... [INFO] Using property: groupId = com.javarticles.maven [INFO] Using property: artifactId = mavensamples Define value for property 'version': 1.0-SNAPSHOT: : [INFO] Using property: package = com.javarticles.maven Confirm properties configuration: groupId: com.javarticles.maven artifactId: mavensamples version: 1.0-SNAPSHOT package: com.javarticles.maven Y: : [INFO] ------------------------------------------------------------------------- --- [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1 [INFO] ------------------------------------------------------------------------- --- [INFO] Parameter: basedir, Value: C:\ [INFO] Parameter: package, Value: com.javarticles.maven [INFO] Parameter: groupId, Value: com.javarticles.maven [INFO] Parameter: artifactId, Value: mavensamples [INFO] Parameter: packageName, Value: com.javarticles.maven [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] project created from Old (1.x) Archetype in dir: C:\mavensamples [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 44:49 min [INFO] Finished at: 2015-11-18T10:45:06+05:30 [INFO] Final Memory: 16M/351M [INFO] ------------------------------------------------------------------------
Maven Directory Structure
Once the command is executed, you will see a new directory with the same name given as the artifactId.
POM XML
The quick start archetype generates a minimal project shell that contains a POM and a single class.
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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javarticles.maven</groupId> <artifactId>mavensamples</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>mavensamples</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
App:
package com.javarticles.maven; /** * Hello world! * */ public class App { public static void main( String[] args ) { System.out.println( "Hello World!" ); } }