Detailed explanation of pom file in Maven

Posted by Aleks on Fri, 21 Jan 2022 22:47:23 +0100

Using Maven in the IDE

IDE tools

  • MyEclipse

    Integrated maven plug-in

  • Eclipse

​ Eclipse For Java EE

  • IDEA

    Integrated Maven plug-in

    In IDE

    Configure Maven

    • Specify maven installation directory and configuration file
    • Automatically update maven projects: check import projects automatically (2020 version: Ctrl + Shift + O)

Create Maven project

new Module - > Maven - > check Create from archetype

Perform maven operation

Maven projects displays all Maven projects

Execution lifecycle

If you don't want to execute separately, click Execute Maven Goal in the maven box (on the right side of run maven)

You can also customize and add a maven operation combination

If there is no resources file under the main file, create one, and the same is true under the test file

Type of directory and change type

In IDEA, directories are classified by type. In the 4 permanent business:

  • Source root main code directory: src/main/java

  • Test Sources Root test code directory: src/test/java

  • Resources Root directory of resources required by the main code: src/main/resources

  • Test Resources Root: src/test/resources

    Note: all directories created by default in IDEA are ordinary directories. After creation, you need to set the Directory type

It can be changed in Mark Directory As by right clicking the file

POM in Maven XML file details

brief introduction

pom: project object model

pom.xml is Maven's core configuration file

A Maven project has and has only one POM XML file, which must be in the following directory of the project

coordinate

What are coordinates

It is used to uniquely identify each project. Coordinates must be defined for the project, and the coordinates must be unique

The purpose is to allow other Maven projects to use the jar package generated by the project

Detailed explanation of coordinates

Maven coordinates are defined by some elements: groupid (Organization ID), artifactId and version

Groupid (Organization ID): indicates the project to which the current module belongs

Artifact ID: ID of the module

Version: current version

<groupId>org.example</groupId>
  <artifactId>maven-project</artifactId>
  <version>1.0-SNAPSHOT</version>

dependency

Basic configuration

How to find the coordinates of a jar package

https://search.maven.org/ Or: https://maven.aliyun.com/mvn/view (maven.aliyun.com/nexus)

Find MySQL connector

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

<!--   mysql jar package-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.22</version>
    </dependency>
  </dependencies>

Scope scope

Indicates the scope of the dependency, which is used to configure the usable range of the dependent jar package

Valuemeaninggive an example
compileThis dependency can be used in the whole project and participates in packaging deployment. The default value iscommons-fileupload
testThis dependency can only be used in the test code and does not participate in the packaged deploymentjunit
providedThis dependency is required when writing source code and does not participate in packaging deploymentservlet-api,jsp-api
runtimeThis dependency is not required when writing code, but is required when running, and participates in packaging deploymentmysql-connector
systemIndicates that the jar package under the local system path should be used together with a systemPathojdbc.jar

Use the local jar package (put it under the lib file you created, at the same level as src):

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.22</version>
  <systemPath>${basedir}/lib/mysql-connector-java-8.0.17.jar</systemPath>
</dependency>

properties

The global attribute is generally the same as the version of the jar package that defines the global

Function: for example, define the version in properties < ljy version>4.3.12</ljy. version>

Then add ${ljy.version} under the dependency on

After changing in properties, the corresponding dependent version is also changed (convenient)

alt+ctrl+v (fast extraction) in dependent version

For example:

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>1.7</maven.compiler.source>
  <maven.compiler.target>1.7</maven.compiler.target>
  <mysql-connector-java.version>8.0.22</mysql-connector-java.version>
</properties>

   <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql-connector-java.version}</version>
    </dependency>

repositories

Used to configure the remote warehouse used by the current project

Order of dependency search: local warehouse - > current project POM Remote warehouse configured in XML - > setting Remote warehouse configured in XML

Spring as an example: url is the official address of spring

<repositories>
<!--      Some of the latest jar Package, which may not be available in the central warehouse, can be used at this time jar Warehouse officially provided by the package-->
        <repository>
            <id>spring_repo</id>
            <url>https://repo.spring.io/milestone/</url>
        </repository>

plugin (configuration plug-in)

Configuration plug-in is a tool

Format:

<build>
  <plugins>
    <plugin>
      <groupId>xxxx</groupId>
      <artifactId>zzzz</artifactId>
      <version>zzzz</version>
    </plugin>
  </plugins>
</build>
<build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
        <plugins>
            <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.1.0</version>
            </plugin>
              </plugins>
        </pluginManagement>
    </build>

Super POM

All POMs XML files inherit the parent POM, which is called super POM. You can't see or change it, but you can see it in myeclipse software

Original link: Detailed explanation of pom files in Maven - Hey s, don't make trouble - blog Garden

Topics: Java Maven intellij-idea