A brief introduction to Maven

What is  Maven

    Most users refer to Maven as a build tool: a build tool used to build source code into releasable form.

    A more formal definition of Apache Maven: Maven is a project management tool that includes a Project Object Model, a set of standards, a Project Lifecycle, and a Dependency Management System ), and the logic used to run the plugin goals defined in the lifecycle phases.

    When you use Maven, you describe your project with a well-defined project object model, and then Maven can apply cross-cutting logic from a set of shared (or custom) plugins.

What to do

    1. Unified development specifications and tools

    2. Unified management of jar packages

introduce

pom.xml

1 <project xmlns="http://maven.apache.org/POM/4.0.0" 
2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4     
5   <modelVersion>4.0.0</modelVersion>
6   <groupId>com.inchlifc.withmaven</groupId>
7   <artifactId>withmaven</artifactId>
8   <version>0.0.1-SNAPSHOT</version>
9   <build/></project>

    This is the core of the maven configuration file:

    1. modelVersion specifies the version number of the current maven model, which is 4.0.0 for both maven2 and maven3

    2. groupId As the name suggests, this is the name of the company or organization.

    3. ArtifactId The name of the project built by maven. If there are sub-projects in the project, you can use "project name-word project name"

   4. version Version number, SNAPSHOT means snapshot, indicating that the project is still under development and is an unstable version. An important point in maven is that the three elements groupId\artifactId\version generate the base coordinates of a maven project.

Other elements:

    1. The packaging type of the packaging project can be jar, war, rar, and the default is jar

    2. Dependencies and dependencies The former includes the latter. An important role of Maven is to manage jar packages in a unified manner. In order to build or run a project, it is inevitable that the project will depend on many other jar packages. In Maven, these dependencies are called dependencies. There is a concept of a local repository and a remote repository here. The configuration of the official downloaded local repository is in "%MAVEN_HOME%\conf\settings.xml", just look for "localRepository"; MyEclipse's default local repository address is in "{user.home}/.m2/repository" Under the path, also look for "localRepository" to find the default local repository of MyEclipse. The local warehouse and the remote warehouse are like this. The Maven project will first obtain the jar package from the local warehouse. When the specified jar package cannot be obtained, the local warehouse will download the jar package from the remote warehouse (central warehouse) and put it into the local warehouse. for future use.

  For example, let's say MyBatis is used in my project, then it can be configured like this:

1 <dependencies>
2     <dependency>
3         <groupId>org.mybatis</groupId>
4         <artifactId>mybatis</artifactId>
5         <version>3.2.5</version>
6     </dependency></dependencies>

    I said before that groupId, artifactId, and version uniquely identify a Maven project. With these three elements, we can go to the remote warehouse to download MyBatis3.2.5.jar to the local warehouse. If you want the jar package of MyBatis and find that there is none, then go to the Internet to download one, you need another jar package, and then go to the Internet to download one, but with Maven, it is much more convenient, you only need to configure the dependency corresponding to the jar package, Maven will Automatically help us to download the jar package from the remote warehouse to the local warehouse.

3. properties It is used to define some configuration properties, such as project.build.sourceEncoding (project build source encoding method), which can be set to UTF-8 to prevent Chinese garbled characters, and can also define relevant build version numbers to facilitate unified upgrades in the future .

4. build indicates the configuration related to the build. For example, there is finalName under build, which indicates the name after the final build.

Maven directory structure:

  • The main directory is the main code of the project, and the test directory stores the test-related code

  • The compiled output code will be placed in the target directory

  • Java code is stored under src/main/java, and configuration files are stored under src/main/resources

  • There is no webapp here, the web project will have a webapp directory, under which the web application related code is stored

  • pom.xml is the configuration file of the Maven project

Add jar format portal to maven repository

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325799599&siteId=291194637