Maven

February 6, 2015 11:34

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model(POM), Maven can manage a project's build, reporting and documentation from a central piece of information.

main page
reference3.2.5

Word maven

maven 英音 /'meɪv(ə)n/ 美音 /ˈmevən/ an expert in a particular subject


Download and Install Maven

Download

Apache Maven 3.2.5 February 6, 2015 09:56

downlaod maven

Install

JDK

Maven is a Java tool, so you must have Java installed in order to proceed.

Install on Uinx-based OS

  1. Extract the distribution archive.Assume you chose /usr/local/apache-maven/apache-maven-3.2.5.
  2. export PAT=$PATH:/usr/local/apache-maven/apache-maven-3.2.5/bin
  3. Make sure JAVA_HOME is setted.
  4. Run mvn --version to verify that it is correctly installed.

install maven on Windows


Quick start

Configuring Maven

configurate at ~/.m2

3 levels configuration:

  1. Project: most static configuration occurs in pom.xml - to project;
  2. Installation: added once for a Maven installation;
  3. User: for a particular user. ~/.m2/settings.xml

Configuring your local Repository

The location of you local repository can be changed in you user configuration. The default vlue is ~/.m2/repository

<settings>
    ...
    <localRepository>/path/to/local/repo/</localRepository>
    ...
</settings>

Creating a Project

mvn archetype:generate -DgroupId=com.csst.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
my-app
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3   <modelVersion>4.0.0</modelVersion>
  4   <groupId>com.csst.app</groupId>
  5   <artifactId>my-app</artifactId>
  6   <packaging>jar</packaging>
  7   <version>1.0-SNAPSHOT</version>
  8   <name>my-app</name>
  9   <url>http://maven.apache.org</url>
 10   <dependencies>
 11     <dependency>
 12       <groupId>junit</groupId>
 13       <artifactId>junit</artifactId>
 14       <version>3.8.1</version>
 15       <scope>test</scope>
 16     </dependency>
 17   </dependencies>
 18 </project>
  1. mvn archetype:generate: prefix archetype is the plugin

Build the Project

mvn package
xugg@xuggs-MacBook-Air ~/P/m/my-app> mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ my-app ---
[WARNING] Using platform encoding (US-ASCII actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/xugg/Project/mvn/my-app/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ my-app ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ my-app ---
[WARNING] Using platform encoding (US-ASCII actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/xugg/Project/mvn/my-app/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ my-app ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ my-app ---
[INFO] Surefire report directory: /Users/xugg/Project/mvn/my-app/target/surefire-reports

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

Results :

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

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ my-app ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.352 s
[INFO] Finished at: 2015-02-06T12:59:52+08:00
[INFO] Final Memory: 9M/118M
[INFO] ------------------------------------------------------------------------
xugg@xuggs-MacBook-Air ~/P/m/my-app>
java -cp target/my-app-1.0-SNAPSHOT.jar com.csst.app.App

java -cp classpath class

Running Maven Tools

Maven Phases

  1. validate: validate the project is correct and all necessary information is avaiable;
  2. compile: compile the source code of the project
  3. test: test the compiled source code using testing framwork.
  4. package: take the compiled code and package it in its distributable format,such as a JAR;
  5. integration-test: process and deploy the package if necessary into a environment where integration tests can be run;
  6. verify: run any checks to verify the package is valid and meets quality criteria;
  7. install: install the package into the local repository, for use as a dependency in other projects locally;
  8. deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

  9. clean: cleans up artifacts crated by prior builds

  10. site: generates site documentation for this project.

Maven webapp

1.Create webapp

$ mvn archetype:generate -DgroupId={project-packaging} 
    -DartifactId={project-name} 
    -DarchetypeArtifactId=maven-archetype-webapp 
    -DinteractiveMode=false

//for example 
$ mvn archetype:generate -DgroupId=com.csst -DartifactId=mybatis_test -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
[INFO] Generating project in Batch mode

too long for wating. adding parameter:
-DarchetypeCatalog=internal

2.mvn dependencies

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.2.2</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.22</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.5</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
        <scope>runtime</scope>
    </dependency>
mvn compile

3. Eclipse IDE supporting

mvn eclipse:eclipse -Dwtpversion=2.0

Description Resource Path Location Type The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path index.jsp /mybatis_test/src/main/webapp line 1 JSP Problem