go to  ForumEasy.com   
JavaPro  
 
 
   Home  |  MyForum  |  FAQ  |  Archive    You are not logged in. [Login] or [Register]  
Forum Home » Java Deploying » Maven -- Absolutely first tutorial
Email To Friend  |   Set Alert To This Topic Rewarding Points Availabe: 0 (What's this) New Topic  |   Post Reply
Author Topic: Maven -- Absolutely first tutorial
Alex_Raj
member
offline   
 
posts: 99
joined: 05/16/2006
from: San Jose, CA
  posted on: 03/25/2012 04:15:08 PM    Edit  |   Quote  |   Report 
Maven -- Absolutely first tutorial
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"


 Profile | Reply Points Earned: 0
Alex_Raj
member
offline   
 
posts: 99
joined: 05/16/2006
from: San Jose, CA
  posted on: 03/25/2012 11:38:45 PM    Edit  |   Quote  |   Report 
Create Java Project with Maven
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>

 Profile | Reply Points Earned: 0
Alex_Raj
member
offline   
 
posts: 99
joined: 05/16/2006
from: San Jose, CA
  posted on: 03/26/2012 12:10:57 AM    Edit  |   Quote  |   Report 
Build Java project
With the pom.xml file being available, you can build your Java project's package (jar) by:
mvn package


The output is as follows:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MyProject 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ MyProject
---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\HelloWorld\Maven\MyProject\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ MyProject ---

[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. b
uild is platform dependent!
[INFO] Compiling 1 source file to C:\HelloWorld\Maven\MyProject\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ My
Project ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\HelloWorld\Maven\MyProject\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ MyPro
ject ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. b
uild is platform dependent!
[INFO] Compiling 1 source file to C:\HelloWorld\Maven\MyProject\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ MyProject ---
[INFO] Surefire report directory: C:\HelloWorld\Maven\MyProject\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.mycompany.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.026 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ MyProject ---
[INFO] Building jar: C:\HelloWorld\Maven\MyProject\target\MyProj
ect-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.248s
[INFO] Finished at: Sun Mar 25 21:55:38 PDT 2012
[INFO] Final Memory: 10M/121M
[INFO] ------------------------------------------------------------------------

 Profile | Reply Points Earned: 0
Alex_Raj
member
offline   
 
posts: 99
joined: 05/16/2006
from: San Jose, CA
  posted on: 03/26/2012 12:24:41 AM    Edit  |   Quote  |   Report 
Let's run it!
You may test the newly compiled and packaged JAR with the following command:
java -cp ./target/MyProject-1.0-SNAPSHOT.jar com.mycompany.App

Which will print:
Hello World!


 Profile | Reply Points Earned: 0
Alex_Raj
member
offline   
 
posts: 99
joined: 05/16/2006
from: San Jose, CA
  posted on: 03/29/2012 12:37:33 AM    Edit  |   Quote  |   Report 
Create a web application project
mvn archetype:generate -DgroupId=com.mycompany -DartifactId=MyWebProject
 -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false


Output:
[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-webapp: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: MyWebProject
[INFO] Parameter: basedir, Value: C:\HelloWorld\Maven\MyFirstWebProject
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\HelloWorld\Maven\MyFi
rstWebProject\MyWebProject
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.892s
[INFO] Finished at: Wed Mar 28 22:18:14 PDT 2012
[INFO] Final Memory: 9M/122M
[INFO] ------------------------------------------------------------------------




The generated Maven WebApp project is of the following structure:
 [+] -- MyWebProject
     [-] -- pom.xml
     [+] -- src
         [+] -- main
             [+] -- resources
             [+] -- webapp
                 [-] -- index.jsp
                 [+] -- WEB-INF
                     [-] -- web.xml


The 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.mycompany</groupId>
  <artifactId>MyWebProject</artifactId>

  <packaging>war</packaging>

  <version>1.0-SNAPSHOT</version>

  <name>MyWebProject Maven Webapp</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>

  <build>
    <finalName>MyWebProject</finalName>
  </build>

</project>



The building tool
mvn package

is going to wrap everything into war file.
 Profile | Reply Points Earned: 0
Alex_Raj
member
offline   
 
posts: 99
joined: 05/16/2006
from: San Jose, CA
  posted on: 10/09/2019 06:46:12 PM    Edit  |   Quote  |   Report 
Running Maven

mvn phase[ phase]


Maven Phase
  • validate: validate the project is correct and all necessary information is available
  • clean: cleans up artifacts created by prior builds
  • compile: compile the source code of the project
  • test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package: take the compiled code and package it in its distributable format declared in <packaging>...</packaging>, such as a JAR or WAR.
  • install: install the package into the local repository, for use as a dependency in other projects locally
  • integration-test: process and deploy the package if necessary into an environment where integration tests can be run
  • verify: run any checks to verify the package is valid and meets quality criteria
  • deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
  •  Profile | Reply Points Earned: 0

     
    Powered by ForumEasy © 2003-2005, All Rights Reserved. | Privacy Policy | Terms of Use
     
    Get your own forum today. It's easy and free.