Java Project Builder Maven's configuration and usage tutorial _java

Source: Internet
Author: User
Tags object model unique id tomcat maven central

First, what is MAVEN?

MAVEN is a Java-developed project building tool that automates the compilation, testing, publishing, and documentation of the project's build process, greatly reducing the burden of programmer deployment.

Second, install Maven

Installing MAVEN is very simple, visit the MAVEN official page to download: http://maven.apache.org/download.cgi
After downloading, configure the M2_home environment variables, and then the terminal runs MVN--version, see the correct output prompts, MAVEN installation completed.

Iii. The basic concept of Maven
The core idea of Maven is Pom, the project object model. The pom file describes the resources (source, dependency, test, etc.) used by a MAVEN project as an XML file. The following figure describes the structure of the Pom file and how maven invokes the Pom file.

When a maven command is executed, a pom file is passed in, and Maven executes on the resource described in the POM

Pom file:

<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" > <modelve Rsion>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>

Modelversion is the POM model version, 4.0.0 supports MAVEN2 and 3
MAVEN coordinates (MAVEN coordinates)

(1) GroupId: It is the unique ID of an organization or project, and in most cases uses the root name of the project's Java package as a groupId, for example com.pastqing

(2) Artifactid: It is the project name being built, such as a payment system Artifactid is Web-pay. Artifactid is a subdirectory name under the GroupID directory in the MAVEN repository

(3) Version: As the name implies the release of the project number

All three of these items are part of the build result name, and a jar package is generated after the project is built, and the path to the MAVEN repository is Maven_repo/com/pastqing/web-pay/1.0/web-pay-1.0.jar

四、一个 The Hello World Java project built with Maven

Build a project with the Maven plugin archetype:
Open the working directory in the terminal
To run the command:

MVN archetype:generate

, the first run is slower because the first time you need to download the project prototype from the MAVEN central repository. After downloading, will select the project model, as well as input groupid, artifactid,version, etc., after the construction will have success prompts.
Packaged items: Switch to the project root directory and run MVN package. After the package is successful, the project generates a target folder with the generated jar files and class files.
To run the jar file:

JAVA-CP Target/helloworld-1.0-snapshot.jar com.pastqing.App

One of the simplest maven-built Java projects is done.

How far Java-web project built with Maven

Building a Web project is basically similar to building a Java project, except for a different model. No longer elaborated here. Let's say specifically how to run a Web project using Tomcat or jetty Plug-ins. Here we take Tomcat for example.

Maven Tomcat Plug-in installation: HTTP://TOMCAT.APACHE.ORG/MAVEN-PLUGIN-2.2/
We added the following plug-in information to the Pom file in the Web project

<plugin>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId> tomcat6-maven-plugin</artifactid>
  <version>2.2</version>
</plugin>
< plugin>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId> tomcat7-maven-plugin</artifactid>
  <version>2.2</version>
</plugin>

This allows us to integrate the Tomcat MAVEN plug-in, which requires only one command to deploy and start the service. The order is as follows:

MVN Tomcat:run

(TOMCAT6)

MVN Tomcat7:run

(TOMCAT7)

Automatically deploy to external tomcat using the Tomcat MAVEN plug-in
The above automatic deployment, which uses the Maven embedded Tomcat, we modify the pom file so that the project is deployed to external Tomcat.

Modify the project Pom file to add server configuration information

<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId> tomcat7-maven-plugin</artifactid>
<version>2.2</version>
<configuration>
  <url>http://localhost:8080/manager/text</url>
  <server>tomcat7</server>
  < username>admin</username>
  <password>admin</password>
</configuration>
</plugin>

The value of server, username, and password corresponds to the server, username, password one by one in Setting.xml in%maven_home.

Start external Tomcat, Run command mvn tomcat7:redeploy

Vi. How to build a multi-module project with Maven

1. Parent Pom

All maven pom files inherit from a parent pom, and if the parent POM is not specified, the Pom file inherits from the root Pom. The integration relationship of the Pom file is shown in the following illustration:

You can have one pom file explicitly inherit another pom file. This allows you to modify the settings of all child pom files by modifying the settings of the public parent pom file. Here we use the parent tag to define the Father Pom. Below we specifically build a multi-module MAVNE project

2. Building the project directory structure

We created a maven-web project named Educationcloud, and then we created several folders under the project directory to divide our modules. They are education-parent (parent module), Education-core (business), education-entity (entity), Education-web (Web service)

The classification of the catalogue can be carried out as needed, and I am divided as above

3. Modify the Pom file

We use module tags to divide the modules. Open the Pom file under the root directory and add the Moudle tag.

<?xml version= "1.0" encoding= "UTF-8"?> <project xmlns= "http://maven.apache.org/POM/4.0.0"
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. educationcloud</groupid>
  <artifactId>EducationCloud</artifactId>
  <version>1.0- snapshot</version>
  <packaging>pom</packaging>
  <modules>
    <module> education-parent</module>
    <module>Education-core</module>
    <module> education-entity</module>
    <module>Education-web</module>
  </modules>
  <name>EducationCloud</name>
</project>

Here we say packaging tags: the most common is packaged as jars, war. Any MAVEN project needs to define the packaging element in the Pom file, and if it is not declared, the default package is the jar, which is packaged as a war package if the definition value is war. If the value is Pom, then none of the packages are generated (typically for the parent module).

To add parent tags to each module:
The parent tag is used to define the coordinate position of the Father Pom, as follows:

<parent>
  <groupid>com. educationcloud</groupid>
  <artifactId>EducationCloud-parent</artifactId>
  <version >1.0-SNAPSHOT</version>
  <relativePath></relativePath>
</parent>

GroupId, Aritfactid, version is the regular element that represents the parent Pom location information, RelativePath is the relative path that represents the parent Pom location, by default. /pom.xml, here's what you need to be aware of GroupID, Aritfactid, version must correspond to the parent pom file

Use dependencymanagement to manage all dependencies of a project
To unify management dependencies, we use dependencymanagement in the parent Project module's POM file to declare all the dependencies of the project, so that when referencing dependencies in other subprojects, you can omit the compilation of the version number and make it easy to modify.
Write the Pom file for the parent Project module **educationcloud-parent, added as follows:
Using Pluginmanagement to manage the Maven Plug-ins in your project
In order to manage the Maven Plug-ins in the project, such as Maven compile Plug-ins, packaged plug-ins, and their configuration information, add pluginmanagement to the parent project Pom file to manage, so that the plug-ins referenced in all the child modules will be treated uniformly and added as follows:

<pluginManagement>
  <plugins>
  <!--complile plug-in configuration-->
    <plugin>
      <groupid >org.apache.maven.plugins</groupId>
      <artifactId>maven-complier-plugin</artifactId>
      <version>3.3</version>
      <configuration>
        <source>1.7</source>
        <target>1.7</target>
        <encoding>UTF-8</encoding>
      </configuration>
    <plugin>
  </plugins>
</pluginManagement>

Above we added the MAVEN compile plug-in configuration, compiles with jdk1.7, after saving we can see the corresponding modification in each child module's effective POM.

To define constants using the properties label:
We define each dependent version number as a different constant so that it is easy to modify later:

<properties>
  <jdkVersion>1.7</jdkVersion>
  <servletversion>3.1.0</ servletversion>
  <mysqlVersion>5.1.34</mysqlVersion>
  <junitversion>4.12</ junitversion>
  <defaultEncoding>UTF-8</defaultEncoding>
</properties>

Reference can be used as a reference in the form of ${jdkversion}.

Note that the above operation can be done in the IDE, more convenient and simple.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.