Subject: Create Java Project with Maven
Author: Alex_Raj
In response to: Maven -- Absolutely first tutorial
Posted on: 03/25/2012 11:38:45 PM
On console, type in the following command:
mvn archetype:generate -DgroupId=com.mycompany -DartifactId=MyProject
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
where two parameters needs be specified:
-DgroupId=com.mycompany
-DartifactId=MyProject
The output is as follows:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>
>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<
<
[INFO]
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom --
-
[INFO] Generating project in Batch mode
[INFO] -------------------------------------------------------------------------
---
[INFO] Using following parameters for creating project from Old (1.x) Archetype:
maven-archetype-quickstart:1.0
[INFO] -------------------------------------------------------------------------
---
[INFO] Parameter: groupId, Value: com.mycompany
[INFO] Parameter: packageName, Value: com.mycompany
[INFO] Parameter: package, Value: com.mycompany
[INFO] Parameter: artifactId, Value: MyProject
[INFO] Parameter: basedir, Value: C:\HelloWorld\Maven
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\HelloWorld\Maven\MyProject
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.429s
[INFO] Finished at: Sun Mar 25 14:53:31 PDT 2012
[INFO] Final Memory: 8M/121M
[INFO] ------------------------------------------------------------------------
The generated Maven project is of the following structure:
[+] -- MyProject
[-] -- pom.xml
[+] -- src
[+] -- main
[+] -- java
[+] -- com
[+] -- mycomany
[-] -- App.java
[+] -- test
[+] -- java
[+] -- com
[+] -- mycomany
[-] -- AppTest.java
The content of pom.xml is as follows:
<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.mycompany</groupId>
<artifactId>MyProject</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>MyProject</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>
>
> On 03/25/2012 04:15:08 PM
Alex_Raj wrote:
1) Why do we need another building tool?
Java software development is often associated with repetitive tasks like building project's java classes, generating documentation and reporting. To automate these tasks a build tool is needed. We have ant already in the plate -- why do we need another one?
Among many reasons, the major one is that Maven is capable of manage and resolve all dependencies referenced by the project automatically. This is extremely helpful for bigger java projects that use a huge set of dependencies.
2) Where to download Maven?
Maven can be downloaded at http://maven.apache.org/download.html
3) Set path environment parameter
PATH=/path/to/apache-maven-3.0.4/bin
4) Verify installation
mvn --version
The output is as follows:
C:\HelloWorld\Maven\MyFirstProject>mvn --version
Apache Maven 3.0.4 (r1232337; 2012-01-17 00:44:56-0800)
Maven home: C:\downloads\apache-maven-3.0.4\bin\..
Java version: 1.6.0_29, vendor: Sun Microsystems Inc.
Java home: C:\Program Files\Java\jdk1.6.0_29\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
References: