Maven: Create a Maven project based on coordinates

Create a Maven project based on coordinates

Article directory

  • Create a Maven project based on coordinates
    • 1. Maven core concept: coordinates
      • ① Coordinates in mathematics
      • ②Coordinates in Maven
        • [1] Vector Illustration
        • [2] Values of the three vectors
      • ③ Correspondence between the coordinates and the storage path of the jar package in the warehouse
    • 2. Experimental operation
      • ①Create a directory as a workspace for subsequent operations
      • ②Open the command line window in the workspace directory
      • ③Use the command to generate the Maven project
      • ④ Adjustment
      • ⑤ Interpretation of automatically generated pom.xml
    • 3. Maven core concept: POM
      • ① meaning
      • ② Model thinking
      • ③Corresponding configuration file
    • 4. Maven core concept: agreed directory structure
      • ①The role of each directory
      • ②The meaning of agreeing on the directory structure
      • ③ Convention is greater than configuration
    • 5. Finally: thanks

1. Maven core concept: coordinates

①Coordinates in mathematics

Using the three “vectors” of x, y, and z as the coordinate system of the space, a “point” can be uniquely positioned in the “space”.

②Coordinates in Maven

In our Maven we use: groupld, artfactld, version to represent the coordinates.

[1] Vector Description

Use three “vectors” to uniquely locate a “jar” package in “Maven’s warehouse”.

  • groupId: the id of the company or organization
  • artifactId: The id of a project or a module in a project
  • version: version number

[2] How to get the value of the three vectors

  • groupId: The reverse order of the company or organization domain name, usually with the project name
    • For example: com.baidu.maven, positive sequence: domain name like maven.baidu.com, the domain name you know is com
  • artifactId: The name of the module, which will be used as the project name of the Maven project in the future
  • version: The version number of the module, set according to your own needs
    • For example: SNAPSHOT indicates the snapshot version, which is in the process of iteration, and the unstable version. If you don’t choose, Maven will default to this.
    • For example: RELEASE means the official release

Example:

-groupId: com.baidu.maven
- artifactId: pro01-baidu-maven
- version: 1.0-SNAPSHOT

③ Correspondence between the coordinates and the storage path of the jar package in the warehouse

coordinate:

 <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>2.5</version>

The location of the jar package corresponding to the above coordinates in the Maven local warehouse:

Maven local warehouse root directory\javax\servlet\servlet-api\2.5\servlet-api-2.5.jar

You must learn to find the corresponding jar package in the local warehouse according to the coordinates. Similar to one of our Java projects, according to the corresponding imported package, find the actual file location of the corresponding package.

2. Experimental operation

①Create a directory as a workspace for subsequent operations

In fact, it is an ordinary directory. In this directory, we are going to write code and develop applications.

For example: E:\Maven_workspane\spaceVideo

At this point we already have three directories, namely:
Maven core program: Chinese military account
Maven local repository: Barracks
Local Workspace: Battlefield

②Open the command line window in the workspace directory

We are in the directory we created: E:\Maven_workspane\spaceVideo and enter the command line window.

A quick access to the command line window of the specified directory is directly in the path display location, just enter cmd, as follows:

③Use commands to generate Maven project

mvn archetype:generate

Run the mvn archetype:generate command

After pressing Enter, you will see that the command line window is continuously downloading things, because it is the first time to start Maven and you need to download a lot of dependent plug-ins, don’t wait for it to load slowly. One thing to mention is: what we are visiting here is the Alibaba Cloud mirror warehouse we configured for Maven in the previous section. download information.

Follow the instructions below

Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 7:【Enter directly, use the default value】

Define value for property 'groupId': com.rainbowSea.maven # indicates the project name

Define value for property 'artifactId': pro01-maven-java # A certain project/module under this project

Define value for property 'version' 1.0-SNAPSHOT: :【Enter directly, use the default value】 # The version number of the project

Define value for property 'package' com.rainbowSea.maven: :【Enter directly, use the default value】# Indicates whether the created package is this.

Confirm properties configuration: groupId: com.atguigu.maven artifactId: pro01-maven-java version: 1.0-SNAPSHOT package: com.rainbowSea.maven Y: : [Press Enter directly to confirm. If there is an input error before and you want to re-enter, enter N and press Enter. 】

Y After pressing Enter, you will see that the command line window is continuously downloading things, because it is the first time to start Maven and you need to download a lot of dependent plugins, don’t wait for it to load slowly.

The last display: BUILD SICCESS means the loading is successful. We will create one in our directory and configure it in the command line creation prompt: a pro01-maven-java folder is as follows:

Open this folder: Note: Every created Maven will automatically create a pom.xml configuration file.

Key points: We will manage each project in Maven in the future, and we will carry out a management configuration and information search for the pom.xml file.

We can open the pom.xml file to see.

④Adjust

The project generated by Maven by default depends on the lower version 3.8.1 of junit, and we can change it to a more suitable version 4.12. NOTE: Don’t forget to Ctrl + S to save.

<!-- Dependency information configuration -->
<!-- dependencies plural tags: contains dependency singular tags -->
<dependencies>
  <!-- dependency singular tag: configure a specific dependency -->
  <dependency>
    <!-- Depend on other jar packages through coordinates -->
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    
    <!-- scope of dependencies -->
    <scope>test</scope>
  </dependency>
</dependencies>

⑤Interpretation of automatically generated pom.xml

<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 tag: 4.0.0 has been fixed since Manven 2 -->
    <!--Represents the label structure adopted by the current pom.xml-->
  <modelVersion>4.0.0</modelVersion>

<!-- The coordinates of the current Maven project -->
    <!--groupId tag: one of the coordinate vectors: represents a certain project developed by the company or organization, here I made it up -->
  <groupId>com.rainbowSea.maven</groupId>
    <!--artifactId tag: one of the coordinate vectors: representing a certain module under the project -->
  <artifactId>pro01-maven-java</artifactId>
    <!--version label: one of the coordinate vectors: represents the version of the current module -->
  <version>1.0-SNAPSHOT</version>
  
  <!-- packaging indicates the packaging method of the current Maven project. There are three optional values: -->
  <!-- jar: Generate a jar package, indicating that this project is a Java project -->
  <!-- war: Generate a war package, indicating that this project is a Web project -->
  <!-- pom: Generate a pom package, indicating that this project is a project that "manages other projects" -->
  <packaging>jar</packaging>

  <name>pro01-maven-java</name>
  <url>http://maven.apache.org</url>

   <!--Custom property values in Maven-->
  <properties>
  <!-- Character set used when reading source code during project construction -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <!-- The jar package that the current project depends on, configure specific dependency information, s: indicates that multiple dependency subtags can be included to configure dependency information -->
  <dependencies>
  <!-- Use dependency to configure a specific dependency information -->
    <dependency>
  
    <!-- Use specific coordinates in the dependency tag to depend on a jar package we need -->
        <!--Coordinate information: which jar package to import, just configure its corresponding coordinate information, and Maven will automatically download it, and the dependency information under the corresponding coordinates -->
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    
    <!-- scope tag configures the scope of the current dependency -->
      <scope>test</scope>
    </dependency>
 </dependencies>
</project>

3. Maven core concept: POM

①Meaning

POM: Project Object Model, project object model. Similar to POM: DOM (Document Object Model), Document Object Model. They are all concrete manifestations of modeling ideas.

②Modeling thinking

POM means to abstract the project into a model, and then use the objects in the program to describe the model. In this way, we can use the program to manage the project. In our development process, the most basic approach is to abstract things in real life into models, and then encapsulate the data related to the model as an object, so that the data related to real things can be calculated in the program.

③Corresponding configuration file

The POM concept is embodied in the configuration file pom.xml in the root directory of the Maven project. So this pom.xml configuration file is the core configuration file of the Maven project. In fact, learning Maven is to learn how to configure this file and what is the use of each configuration.

4. Maven core concept: agreed directory structure

The following is: when we execute mvn archetype:generate to build the Maven workspace, Maven automatically creates directory information for us.

①The role of each directory

There is also a target directory dedicated to storing the output of the build operation. We will introduce it later.

②The meaning of agreeing on the directory structure

In order to make the construction process as automated as possible, Maven must agree on the role of the directory structure. For example: Maven executes the compilation operation, must first go to the Java source program directory to read the Java source code, then execute the compilation, and finally store the compilation result in the target directory.

③Convention is greater than configuration

Maven does not use configuration for the issue of directory structure, but based on convention. This will make it very convenient for us during the development process. It must be very troublesome if you need to configure the location of each directory in detail every time you create a Maven project.

The current technological development trend in the development field is: Convention is greater than configuration, and configuration is greater than coding.

Convention is greater than configuration, configuration is greater than encoding interpretation:

Agreement: It means that everyone has a unified agreement, so there is no need to configure everything, just write it to death, because everyone is the same and will not be modified.

Configuration: Basically, everyone uses the same, but there are some differences, such as configuration information, passwords, account numbers, etc. You only need to configure your own corresponding information.

Coding: Everyone is different, you need to implement it yourself, from scratch, and realize it bit by bit.

5. Finally: Thanks

This article refers to the following blogger’s sharing. Here again, we sincerely thank the bloggers for their enthusiastic sharing of their technology.

Thanks to the following bloggers for sharing

[1]: Weapon | Code Rework