Chapter 2 Introduction to maven

  maven is an excellent cross-platform project building tool. Project construction is also mentioned above, including project creation, compilation, testing, packaging, deployment, etc. Maven is not just a build tool, it is also a dependency management tool and project information management tool. A simple understanding of dependencies is the third-party jar package used by the project. What jar package we use, it is said to have a dependency on this jar package. Project information includes project name, description, project address, developer information, scm information used, continuous integration information, etc.

  To use maven, you must first install it. Today's IDEs generally have built-in maven support by default, such as eclipse. However, the built-in maven is generally relatively new and may not be too stable. It is recommended to use the maven installed by yourself.

  installation steps:

  1. Install jdk. Maven requires jdk to run, so you need to install jdk first.

  2. Download. Maven official download address: http://maven.apache.org/download.cgi

   3. Installation. After downloading, it is a compressed package, which can be decompressed. After decompression, the directory structure is as follows:

 The bin directory contains the executable commands of maven, the boot directory contains a jar file responsible for creating the class loader required for maven to run, the conf directory contains the global configuration file of maven, and the lib directory contains the jar files that maven depends on during runtime;

  4. Configure environment variables. Add the maven bin directory to the PATH environment variable, and add the maven installation directory to the M2_HOME environment variable.

  

  Once installed, you can use maven from the command line. Open a command line terminal and enter the command mvn


 Seeing the above output, maven has been installed.

  Next you can manually create a maven project. Create a new directory under the specified directory, such as hello-world, which is the project name, create a new src directory under hello-world, create a new main directory under the src directory, create a new java directory under the main directory, and create a new package com under the java directory \jiangnan\helloworld, create the com, jiangnan, and helloworld directories in sequence, and create a new class HelloWorld in the helloworld directory:

package com.jiangnan.helloworld;

public class HelloWorld {
	public String sayHello() {
		return "hello maven";
	}
	
	public static void main(String[] args) {
		System.out.println(new HelloWorld().sayHello());
	}
}

 Create a new pom.xml in the hello-world directory. The content of the 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>com.jiangnan</groupId>
  <artifactId>hello-world</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>hello-world</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>4.7</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

 Enter the hello-world directory from the command line and enter mvn clean compile. You can see that the command line is continuously outputting. If you see BUILD SUCCESS, it means that the command was executed successfully. If you see BUILD FAILURE, you need to check the reason for the failure. This is just to demonstrate the use of maven in the command line. We usually integrate maven in the IDE for development.

 

Integrate maven in IDE:

Take eclipse as an example here, open eclipse, Window->Preference->Maven->Installations, you can see that EMBEDDED is checked by default, remove this check, click add, select the unzipped maven installation directory, and check the custom installation maven, click Apply, so maven is integrated into eclipse.

By default, maven's global configuration file is the settings.xml file in the .m2 directory in the user's home directory. If it is a Windows system, it is generally in the user directory of the c drive. The default local repository is located in the repository directory under the home directory. The Windows system is generally on the c drive. Since it will occupy the c drive space, and it may not be formatted when the system is reinstalled, I like to use a customized global configuration file. Open the settings.xml file in the conf directory of the maven installation directory, and change the value of the localRepository element to a custom path. As the local repository path, the build downloaded by maven will be stored here. The concept of the repository will be introduced later.


 In eclipse, Maven->user settings, user settings select the file you just edited, you will see the path to the local repository we just configured below, click Apply, OK, so maven is configured.


 

Create a new maven project:

New projects generally choose the maven archetype to generate the skeleton of the project. The skeleton of the project refers to the basic structure of the project. For example, the general project structure will have a project name, and there will be a src directory in the root directory. If it is a web project, there will also be a WEB-INF directory, a lib directory, or web.xml, etc., The archetype of maven is used to help us generate these directories and files, and the directory structure generated by different archetypes is different.

Create a new maven project in eclipse, File->New->Other->Maven->Maven Project, Next, Next, and then list the default archetype provided by maven



 For ordinary projects, we usually choose maven-archetype-quickstart, for web projects, choose maven-archetype-webapp; then fill in groupId, artifactId, version, these three are the coordinates of maven, which will be introduced in detail in the next chapter



 Click finish, and the maven project is built. The project structure looks roughly like this:


convention over configuration

We have also said before that in traditional projects, the project structure is relatively chaotic, and the storage locations of configuration files and test codes are not uniform. Based on a lot of practical experience, maven has made some basic conventions for the project. We only need to abide by these conventions, such as:

1. There is a pom.xml in the root directory of the project, which is used to configure maven dependencies, plug-ins and other information. We will elaborate later.

2. There is a src directory in the root directory. The src directory is divided into main and test directories. In the main directory, things related to the main code are placed, which will be released to production in the future; in the test directory, test related things, such as unit tests and test configuration files, are placed. Etc., these things are only used during testing and will not be released to the production environment; the main and test directories are divided into java and resources directories, where the code and configuration files are separated, the java directory stores the code, and the resources directory stores the configuration files. This project structure is much clearer;

3. The compiled output of the project is in the target directory under the project root directory by default.

In addition, maven has other conventions, which will be introduced later.

Projects created by maven and using maven's conventions, when we see other projects, they are all familiar structures, and we don't need to spend time to get familiar with them. Of course, this is the default behavior of maven. If you want to change this behavior, it is also possible, but it is not recommended. We will describe how to change the default behavior later.

 

Next, we will explain several core concepts of maven, such as coordinates, dependencies, warehouses, plug-ins, etc.

 

Guess you like

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