Maven simple and practical tutorial

1. Introduction to Maven

 

 

1.1. Introduction

 

An automation tool written in java for building systems.

The current version is 2.0.9. Note that there is a big difference between maven2 and maven1. You need to distinguish between versions when reading third-party documents.

 

1.2. Maven resources

 

 

1.3. Differences between Maven and Ant

 

Maven is gradually replacing Ant, and many java open source software (Spring, Struts2...) already use maven.

  • No need to write complex processing scripts;
  • Declarative class library dependency management.

 

1.4. Basic functions of Maven

 

  • Build: such as generating class, jar, war or ear files
  • Generate documentation: such as generating javadoc, website documentation
  • Generate reports: such as junit test reports
  • Generate dependent class library: Generate documentation to explain the project's dependencies on other software
  • About SCM: SCM (Software Configuration Management), software configuration management, such as version control, such as bug management, etc.
  • Publish: generate distribution packages for release, such as generating Struts2 distribution packages for submission to users
  • Deployment: For example, a web application is automatically deployed to a specified server

Through the small example of commodity management I wrote, the function of combining maven and svn is demonstrated.

 

2. Maven usage

 

 

2.1. Maven installation and configuration

 

  • Download the latest Maven distribution package from the official website http://maven.apache.org/download.html , currently 2.0.9;

  • unzip to local;
  • Configure maven, set the maven/bin directory to the Windows environment variable Path
  • Check if maven is installed successfully, execute it on the command line
    mvn -version
     

 

2.2. Basic use of Maven

 

An introduction to the basic use of Maven to write simple java and web projects from the command line .

 

2.2.1. Implementing a Java project

 

Create an ordinary java project under the command line through maven, that is, the project executed by the main method or the class library of the jar file.

 

2.2.1.1. Creating a Maven project

 

implement:

 

mvn archetype:generate

 

In the interactive interface:

  • Choose a number: Just press Enter, that is, choose 15
  • Define value for groupId: Enter the organization id, such as easymorse.com
  • Define value for artifactId: Enter the project name, such as helloworld
  • Define value for version: Enter the version number, you can press Enter directly, the default is 1.0-SNAPSHOT
  • Define value for package: java package name, such as com.easymorse
  • Then press Enter to confirm the above input.

Observe the generated files and directories under the helloworld directory (the project name entered by Define value for artifactId):

  • Project build file: pom.xml
  • Code framework: src\main\java\com\easymorse\App.java
  • Test code: src\test\java\com\easymorse\ AppTest .java

 

2.2.1.2. Running the Maven project

 

The command line enters the helloworld directory (Define value for artifactId input project name).

Project packaging

 

mvn package

 

What did the check command generate?

  • target directory
  • compiled code
  • compiled test code
  • Tested and generated report using junit
  • jar file to generate code

Run the packaged jar file:

 

java -cp  target\helloworld-1.0-SNAPSHOT.jar com.easymorse.App

 

Compile the source program

 

mvn compile

 

compile and test

 

mvn test

 

Empty generated files

 

mvn clean

 

Convert maven project to eclipse project

Command line run:

 

mvn eclipse:eclipse

 

Open eclipse, select from the menu: file>import>general>existing projects into workspace, select the directory in the dialog box, and import it.

If you want to clear the configuration information about the eclipse project:

 

mvn -Dwtpversion=1.0 eclipse:clean

 

joint use

 

mvn eclipse:clean clean

 

 

2.2.2. Implementing the Web Project

 

Create a java web project from the command line through maven.

 

2.2.2.1. Creating a Maven project

 

Enter at the command line, this step is similar to creating a java project:

 

mvn archetype:generate

 

Interaction step description:

  • Choose a number: Just press Enter, that is, choose 18, which is different from ordinary java projects
  • Define value for groupId: Enter the organization id, such as easymorse.com
  • Define value for artifactId: Enter the project name, such as helloworld
  • Define value for version: Enter the version number, you can press Enter directly, the default is 1.0-SNAPSHOT
  • Define value for package: java package name, such as com.easymorse
  • Then press Enter to confirm the above input.

Need to add the plugin of the servlet container in the pom.xml file:

 

        <build>
                <plugins>
                        <plugin>
                                <groupId>org.codehaus.mojo</groupId>
                                <artifactId>tomcat-maven-plugin</artifactId>
                        </plugin>
                        <plugin>
                                <groupId>org.mortbay.jetty</groupId>
                                <artifactId>maven-jetty-plugin</artifactId>
                                <version>6.1.6</version>
                        </plugin>
                        <plugin>
                                <artifactId>maven-compiler-plugin</artifactId>
                                <configuration>
                                        <source>1.6</source>
                                        <target>1.6</target>
                                        <encoding>UTF-8</encoding>
                                </configuration>
                        </plugin>
                </plugins>
        </build>

 

  • tomcat plugin
  • jetty plugin
  • Compile the configuration of the plugin

The role of the repository directory

The location of the repository is in the .m2 directory of the user directory.

The role of the repository directory is to cache the dependent class libraries.

 

2.2.2.2. Running the Maven project

 

Project packaging

 

mvn package

 

start tomcat

 

mvn tomcat:run

 

start jetty

 

mvn jetty:run

 

Convert to eclipse project

 

mvn -Dwtpversion=1.5 eclipse:eclipse

 

This generates the web project of the wtp plugin.

Open eclipse, select from the menu: file>import>general>existing projects into workspace, select the directory in the dialog box, and import it.

In addition, you need to create a classpath variable in eclipse, the name is: M2_REPO, and the value is the .m2/repository directory under the system user.

 

3. Basic configuration of POM file

 

 

3.1. POM introduction

 

 

3.1.1. What is POM

 

Project Object Model, the project object model.

The pom.xml file saved in xml format.

The function is similar to the build.xml file of ant, and the function is more powerful.

This file is used to manage: source code, configuration files, developer information and roles, issue tracking system, organization information, project authorization, project url, project dependencies, etc.

 

3.1.2. A quick tour

 

A complete pom.xml file, placed in the root directory of the project.

 

<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>

  <!-- The Basics -->
  <groupId>...</groupId>
  <artifactId>...</artifactId>
  <version>...</version>
  <packaging>...</packaging>
  <dependencies>...</dependencies>
  <parent>...</parent>
  <dependencyManagement>...</dependencyManagement>
  <modules>...</modules>
  <properties>...</properties>

  <!-- Build Settings -->
  <build>...</build>
  <reporting>...</reporting>

  <!-- More Project Information -->
  <name>...</name>
  <description>...</description>
  <url>...</url>
  <inceptionYear>...</inceptionYear>
  <licenses>...</licenses>
  <organization>...</organization>
  <developers>...</developers>
  <contributors>...</contributors>

  <!-- Environment Settings -->
  <issueManagement>...</issueManagement>
  <ciManagement>...</ciManagement>
  <mailingLists>...</mailingLists>
  <scm>...</scm>
  <prerequisites>...</prerequisites>
  <repositories>...</repositories>
  <pluginRepositories>...</pluginRepositories>
  <distributionManagement>...</distributionManagement>
  <profiles>...</profiles>
</project>

 

 

3.2. Basic Settings

 

 

3.2.1. Collaboration

 

 

<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>org.codehaus.mojo</groupId>
  <artifactId>my-project</artifactId>
  <version>1.0</version>
  <packaging>war</packaging>
</project>

 

  • groupId : Organization ID, for example: org.codehaus.mojo, in the M2_REPO directory, it will be: org/codehaus/mojo directory.
  • artifactId : The project name, for example: my-project, in the M2_REPO directory, it will be: org/codehaus/mojo/my-project directory.
  • version : The version number, for example: 1.0, in the M2_REPO directory, it will be: org/codehaus/mojo/my-project/1.0 directory.
  • packaging : Packaging format, which can be: pom , jar , maven-plugin , ejb , war , ear , rar , par

 

3.2.2. Relationship between POMs

 

 

3.2.2.1. Dependencies

 

The dependency list is an important part of the POM.

 

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.0</version>
      <scope>test</scope>
    </dependency>
    ...
  </dependencies>

 

  • groupId , artifactId , version :
  • scope : compile(default),provided,runtime,test,system
  • exclusions

How to find the dependent class library?

Generally available through this website: http://www.mvnrepository.com

For example, by querying hibernate, you can find the hibernate class library entry in the result list.

Click: http://www.mvnrepository.com/artifact/org.hibernate/hibernate,

Select the version, say 3.2.6ga, ie: http://www.mvnrepository.com/art  ... /hibernate/3.2.6.ga

Copy from the article:

 

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate</artifactId>
    <version>3.2.6.ga</version>
</dependency>

 

to the pom.xml file.

Do I still need to find the pom that hibernate depends on?

No, hibernate will also have a pom, and maven will automatically find the class library it depends on through its pom.

 

3.2.2.2. Inheritance

 

Inherit the contents of other pom.xml configurations.

maven provides a top level parent pom.xml file similar to java.lang.Object.

You can view the current pom.xml affected by the super pom.xml file with the following command:

 

mvn help:effective-pom

 

Create a reusable pom.xml file for various projects: http://easymorse.googlecode.com/svn/trunk/pom/pom.xml

Deploy the pom.xml file to be reused:

 

mvn install

 

Inherit the above pom in your own pom file:

 

        <parent>
                <groupId>com.easymorse</groupId>
                <artifactId>pom</artifactId>
                <version>0.1</version>
        </parent>

 

 

3.2.2.3. Aggregation Relationships

 

Used to aggregate multiple maven projects into one big project.

For example, the directory structure is as follows:

 

.
| - pom.xml
|-- module-a
    `- pom.xml
|-- module-b
    `- pom.xml
|-- module-c
    `- pom.xml
|-- foo-all
    `- pom.xml

 

Then the total pom.xml file looks like:

 

...
    <modules>
        <module>module-a</module>
        <module>module-b</module>
        <module>module-c</module>
        <module>foo-all</module>
    </modules>
</project>

 

Reference documentation: http://maven.apache.org/plugins/maven-eclipse-plugin/reactor.html

The original example is wrong, see: modules.rar

 

3.2.3. Other Configurations

 

The properties of maven are placeholders for values, similar to EL, similar to ant properties, such as ${X}, which can be used in any assignment position in the pom file.

There are the following categories:

  • env.X: Operating system environment variables, such as ${env.PATH}
  • project.x: Properties in the pom file, such as: <project><version>1.0</version></project>, referenced by: ${project.version}

  • settings.x: Properties in the settings.xml file, such as: <settings><offline>false</offline></settings>, reference method: ${settings.offline}

  • Java System Properties: Properties in java.lang.System.getProperties(), such as java.home, reference method: ${java.home}
  • Customization: In the pom file, you can: <properties><installDir>c:/apps/cargo-installs</installDir></properties>, reference method: ${installDir}

 

3.3. Others

 

 

3.3.1. Setting Offline

 

Create a settings.xml file in the .m2 directory (if it doesn't exist)

 

<settings 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/settings-1.0.0.xsd">
        <offline>true</offline>
</settings>

 

 

3.3.2. Installing third-party packages

 

There are often third-party packages that are not available on the online repository for some reasons and need to be installed by yourself.

For example, some versions of sun jar files, such as oracle drivers.

Take the oracle driver as an example, for example, the driver path is c:/driver/ojdbc14.jar, which is version 10.2.0.3.0

It can be found in this URL: http://www.mvnrepository.com/artifact/com.oracle/ojdbc14  artifactId and groupId.

 

mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.3.0 -Dpackaging=jar -Dfile=c:/driver/ojdbc14.jar

 

This makes it possible to rely on references in the pom:

 

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc14</artifactId>
    <version>10.2.0.3.0</version>
</dependency>

 

 

3.3.3. Deploy to tomcat

 

Tomcat is configured with a user with administrative privileges: conf\tomcat-users.xml

 

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="manager"/>
  <user username="marshal" password="password" roles="manager"/>
</tomcat-users>

 

In the tomcat plugin of the pom file add:

 

                        <plugin>
                                <groupId>org.codehaus.mojo</groupId>
                                <artifactId>tomcat-maven-plugin</artifactId>
                                <configuration>
                                        <url>http://localhost:8080/manager</url>
                                        <server>myserver</server>
                                        <path>/mycontext</path>
                                </configuration>
                        </plugin>

 

Add in the .m2/settings.xml file:

 

<settings 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/settings-1.0.0.xsd">
        <servers>
                <server>
                        <id>myserver</id>
                        <username>marshal</username>
                        <password>password</password>
                </server>
        </servers>
</settings>

 

Run the package deployment, in the maven project directory:

 

mvn tomcat:deploy

 

Then visit: http://localhost:8080/mycontext/  .

Undo deployment:

 

mvn tomcat:undeploy

 

Start the web application:

 

mvn tomcat:start

 

Stop the web application:

 

mvn tomcat:stop

 

Redeploy:

 

mvn tomcat:redeploy

 

Deploy the expanded war file:

 

mvn war:exploded tomcat:exploded

 

 

3.3.4. What not to say

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326647862&siteId=291194637