Install and use Maven in Linux

Source: Internet
Author: User

Install and use Maven in Linux

Apache Maven is a software (especially Java software) project management and automatic building tool provided by the Apache Software Foundation. Based on the concept of the project Object Model (POM), Maven uses a central information segment to manage steps such as building, reporting, and documentation of a project. I used to be a subproject of the Jakarta project and is now an independent Apache project.

Many Apache projects are now using Maven for management.

This article briefly introduces the knowledge of Maven and how to install and use it in Linux. To learn more about Maven, go to the official Maven website. Http://maven.apache.org

 

Install Maven

Maven: http://maven.apache.org/download.cgi
The latest version 3.2.3 is used as an example to install JDK.

Download Maven and decompress it, and move it to the usr/local directory.

$ Wget tar vxf apache-maven-3.2.3-bin.tar.gz $ mv apache-maven-3.2.3/usr/local/maven3

Modify the environment variables and add the following lines to/etc/profile:

MAVEN_HOME =/usr/local/maven3export MAVEN_HOMEexport PATH =$ {PATH }:$ {MAVEN_HOME}/bin

Remember to executesource /etc/profileMake environment variables take effect.

Last runmvn -vVerify that maven is successfully installed. If the installation is successful, the following content is printed:

Apache Maven 3.2.3 (versions; 2014-08-12T04: 58: 10 + 08: 00) Maven home:/usr/local/maven3Java version: 1.7.0 _ 65, vendor: Oracle configurationjava home: /usr/lib/jvm/java-7-openjdk-amd64/jreDefault locale: en_US, platform encoding: UTF-8OS name: "linux", version: "3.20.- 35-generic", arch: "amd64 ", family: "unix" use Maven to create a project

Whether it's a language or a framework, a Hello World is basically a routine. We also build a Hello World project to learn how to use Maven.
Create a helloworld project using maven

$ Mvn archetype: create-DgroupId = helloworld-DartifactId = helloworld

The generated project directory is as follows, where src/main/java is the source code directory and src/test/java is the test file directory.
There are only three files in total. Open these three files and you can see the App. A Hello World Program has been written in java, and the Code for the Junit unit test in AppTest is as for pom. xml to provide some basic information and dependencies of the project.

Helloworld/── pom. xml └ ── src ├ ── main │ └ ── java │ └ ── helloworld │ └ ── App. java └ ── test └ ── java └ ── helloworld └ ── AppTest. java

The content of the pom. xml file 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/xsd/maven-4.0.0.xsd"> <modelVersion> 4.0.0 </modelVersion> <groupId> helloworld </groupId> <artifactId> helloworld </artifactId> <version> 1.0-SNAPSHOT </version> <packaging> jar </packaging> <name> helloworld </name> <url> http://maven.apache.org </url> <properties> <project. build. sourceEncoding> UTF-8 </project. build. sourceEncoding> </properties> <dependencies> <dependency> <groupId> junit </groupId> <artifactId> junit </artifactId> <version> 3.8.1 </version> <scope> test </scope> </dependency> </dependencies> </project>

In the pom. xml file, the definition of the project is described first. The four tuples groupId: artifactId: packaging: version can uniquely mark a project. We can not only mark our project with this triplet, but also mark other projects, for example, describe project dependencies. Perperties defines the properties of a project. You can also define variables and reference them elsewhere. As for the final dependencies, it describes the dependencies of the Project. Maven automatically downloads the corresponding files based on the dependencies and uses them during compilation.

In large-scale project development, it is often divided into several sub-projects. Each sub-project has its own pom. xml, which is equivalent to an inherited relationship with the parent pom. xml.

It can be said that the configuration of the pom. xml file is the core focus of Maven and needs to be detailed in learning Maven. Only the simplest configuration example is provided here. For more information, see the official documentation.

Next, compile and run the Helloworld project.

If this is the first compilation, you need to connect to the Internet, because Maven will automatically download the dependency package.

$ Mvn package

The downloaded dependency package is saved in ~ In the/. m2/repository folder, open this folder and we will find that the packages are stored according to the aforementioned four-tuple directory structure. When Maven depends on a jar package, it first searches for the local library. If it does not find the package, it will download it from the Internet. Of course, not all packages can be downloaded from the Internet. For example, if we develop a jar package, we can usemvn installCommand to install a project to the local repository.

After the package is successful, we will find a target folder in the project. The directory structure is as follows:

Target/├ ── classes │ └ ── helloworld │ └ ── App. class ── helloworld-1.0-SNAPSHOT.jar ├ ── maven-archiver │ └ ── pom. properties ── maven-status │ └ ── maven-compiler-plugin │ ├ ── compile │ └ ── default-compile │ ├ ── createdFiles. lst │ └ ── inputFiles. lst │ ── testCompile │ └ ── default-testCompile │ ── createdFiles. lst │ ── inputFiles. lst ├ ── surefire-reports │ ├ ── helloworld.AppTest.txt │ ── TEST-helloworld.AppTest.xml ── test-classes ── helloworld ── AppTest. class

As you can see, in the package process, maven has compiled and tested the code, generated the test report, and generated the jar package.

Finally, you can manually run the jar package to view the results.

$ Java-cp target/helloworld-1.0-SNAPSHOT.jar helloworld. AppHello World! Common Maven commands

There are many Maven commands. To learn commands, you must first understand the Maven lifecycle.

Maven first verifies and processes the referenced resources, and then compiles the project. If it does not declare that the test is skipped, the test code is compiled and tested to generate a test report. Finally, Maven will package the compiled content for release.

Maven commands correspond to Maven lifecycles. A command often contains multiple lifecycles, suchmvn packageAll the above steps are completed.

Here are several common commands

Mvn compile project mvn test compile run unit test mvn package (jar or war) mvn install the project to the local warehouse mvn clean clear project mvn eclipse: eclipse generate eclipse project vomit

When using Maven to compile some Apache projects, I really want to vomit GFW !! If you do not flip the wall, it is often compiled to half, so there is a file that cannot be downloaded ..

Maven official guide _ Chinese full version clear PDF

Maven 3.1.0 release, Project Build Tool

Install Maven in Linux

Maven3.0 configuration and simple use

Set up sun-jdk and Maven2 in Ubuntu

Get started with Maven

This article permanently updates the link address:

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.