Maven Learning Series 2----maven simple configuration

Source: Internet
Author: User
Tags repetition

Maven several concepts 1,pom (Project object model)

Describe how the project builds, declares project dependencies, and so on. In the form of XML, named Pom.xml, a MAVEN project corresponds to a pom.xml. Equivalent to Ant's Build.xml file, Gradle's Build.gradle file.

2, Component,

Using the Maven tool, you can compile your project into many types of packages, often jar/war/pom-type packages, which are called artifacts in the Maven world.

3, Warehouse,

A place where all MAVEN project-shared artifacts are stored uniformly. stored in the groupid/artifactid/version/artifactid-version.packaging path. It is divided into local warehouses, remote warehouses, central warehouses, and. The latter two belong to the remote repository, but are listed here because the two warehouses are more special, followed by detailed instructions.

4, coordinates,

The concept comes from the geometric plane, in order to accurately locate the concept of the position of the component (a Java project will depend on a lot of open-source jar packages, each jar package will have many versions, with the concept of coordinates can easily specify a specific jar package). MAVEN defines 5 coordinate elements:groupid/artifactid/version/packaging/classifier, so that a single component can be uniquely identified in a repository.

5, Reliance,

This is a good understanding, that is, if a project uses the functionality of other artifacts, it makes a dependency, such as using the spring framework to rely on the jar package provided by spring.

6, Generation cycle,

Abstract and unify all the building processes. Includes: Project cleanup, initialization, compilation, testing, packaging, integration testing, validation, deployment, and site generation. The most common are the four stages of cleaning, compiling, testing, and packaging. The specific tasks for these build cycles are done by the Maven plugin (for example, the Maven-compiler-plugin plugin can accomplish the compile target).

7, aggregation,

When a project is large, it will be divided into many modules, if a module of the compilation of a module will be very troublesome. Aggregation is a feature that appears to solve this problem. You can compile all the modules together at one time (you can also configure only some of the modules to be compiled).

8, inheritance,

In multiple modules in a large project there is inevitably a dependency on multiple identical dependencies, which is repetition. This allows the repetition to be made into a single module, and the other modules inherit the corresponding configuration from this module, just like the parent-child inheritance structure of a class.


Maven configuration file 1,settings.xml,

The global profile of MAVEN is controlled on the machine, we can copy this file to ~/.m2/, and then modify this file to customize Maven's behavior in user scope. (but now all a person a computer, should not involve multiple users to share the problem, anyway I did not encounter)


<?xml version= "1.0" encoding= "UTF-8"? ><settings xmlns= "http://maven.apache.org/SETTINGS/1.0.0"          xmlns : xsi= "http://www.w3.org/2001/XMLSchema-instance"          xsi:schemalocation= "http://maven.apache.org/SETTINGS/ 1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd ">  <!--localrepository   | The path to the local repository maven would use to store artifacts.   Default: ${user.home}/.m2/repository  <localRepository>/path/to/local/repo</localRepository>  -  <strong><localRepository>E:\.m2\repository</localRepository></strong> </settings>

    • All configurations are placed between the <settings></settings> tags
    • Here only the <LOCALREPOSITORY/> is configured, and from the name can be guessed out is to configure a local warehouse, the concept of the warehouse before talking about the simple role of the warehouse. Maven searches the repository for the required artifacts, in the order of the local warehouse, which is not found and then searched from the configured remote repository (if the remote repository is not configured, as configured here, it is searched from the central repository provided by MAVEN, and the central repository is currently located at:./http REPO.MAVEN.APACHE.ORG/MAVEN2)
    • The local repository can also be not configured, and MAVEN will create the ~/.m2/repository/locally as a local repository by default. Under Windowns This is the C drive, I hate to put the data on the C-drive, so I am here to configure to another disk.

2,pom.xml,

This is the configuration file corresponding to the MAVEN project, the following gives the pom.xml file of the HelloWorld project, through which the POM can build an executable jar package, the specific configuration here does not do a detailed explanation.

<pre name= "code" class= "HTML" ><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.itsenlin</groupid> <artifactId>helloworld.demo</artifactId> <version>0.0.1-snapshot </version> <name>Archetype-helloworld.demo</name> <url>http://maven.apache.org</url > <dependencies> <dependency> <groupId>junit</groupId> <artifactid>junit</ artifactid> <version>4.12</version> <scope>test</scope> </dependency> </ Dependencies> <build><plugins><plugin> <groupid>org.apache.maven.plugins</groupid&      Gt <artifactId>maven-compiler-plugin</artifactId> <configuration> &LT;SOURCE&GT;1.8</source> <target>1.8</target> <encoding>${project.build.sourceencoding}&lt ;/encoding> </configuration> </plugin> <plugin> &LT;GROUPID&GT;ORG.APACHE.MAVEN.PLUGINS&L t;/groupid> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> &lt  ;configuration> <archive> <manifest> <!--tell Maven-jar-plugin to add a Class-path elements to MANIFEST. MF file and include all dependencies in the Class-path element--<addClasspath>true</addClasspath> <!- -All dependencies should be in the Lib folder--<classpathPrefix>lib/</classpathPrefix> <!--when              When a user executes a jar file using the Lib command, the element is used to define the class name to be executed-<mainClass>com.itsenlin.Helloworld</mainClass> </manifest> </archive> </configuration> </plugin> </plugins></buiLd></project> 

HelloWorld Project 1, project directory structure

The last picture is a combination of two pictures, which is the location of the main program, the following is the location of the test program map

This directory structure is not random, this is another MAVEN feature "contract better than Configuration", in Maven has the following conventions:

    • Source Path: Src/main/java
    • Resource directory for the main program: Src/main/resources
    • Test program path: Src/test/java
    • Resource file path for the test program: src/test/resources
    • Compile Output directory: target/classes/
    • Package Output Directory: target/
    • Package format defaults to jar

MAVEN will look for the appropriate files from these agreed paths, which can reduce many of the user's configuration.

2, source code

Helloworld.java

Package Com.itsenlin;public class Helloworld {public string SayHello (String name) {return "Hello," + name;} public static void Main (string[] args) {System.out.println (New Helloworld (). SayHello ("world!"));}}
Helloworldtest.java

Package Com.itsenlin;import Org.junit.assert;import Org.junit.test;public class HelloWorldTest {@Testpublic void Sayhellotest () {Helloworld HW = new Helloworld (); Assert.assertequals ("Hello, world!", Hw.sayhello ("world!"));}}


3, command line style build


Go to the Pom.xml directory and execute "mvn install",



MAVEN will automatically generate the target directory and target/classes directory in the directory where the Pom.xml is located, put the compiled class file under Target/classes/, and put the compiled jar in the target directory



4. Import the project into the IDE

Here's how to import maven engineering in eclipse

    • Click the file-->import item, then select existing Maven Projects under MAVEN and click Next



    • Click the Browse button to select the parent directory where the MAVEN project Pom.xml, the top level directory of Maven, and then press the Finish button to



    • The HelloWorld project that was imported in eclipse is shown in



Maven Learning Series 2----maven simple configuration

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.