Use of Maven (dependency conflict)

1.1Maven coordinate

Coordinates: each jar package or maven project has a unique ID card on the network

Coordinates (GAV)

  1. Reverse order of groupId company or organization domain name (warehouse: package name)
  2. artifactId project name or module name (warehouse: project name)
  3. Version version number (warehouse: version number)

Is the coordinate related to the local warehouse?

  • It matters.
  • Path: / warehouse name / groupid name / artifact name / version/

1.2 Maven common commands

  • Compile: compile. Compile Java files in the project into class files, and generate target directory
  • clean: clear. Delete target directory
  • Package: package. Package the contents of target directory into jar or war
  • Install: install. Install the generated jar package or war package to the local warehouse
  • All local projects can share their methods and classes

1.3 POM label details

<!-- projectLabels: parent labels ,FuturepomAll content inprojectInside the label-->

<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">

  <!-- Version No -->

  <modelVersion>4.0.0</modelVersion>

 <!--

    groupId+artifactId+version:Global unique, coordinate, abbreviationGAV

   -->

    <!-- groupid: Package name -->   

  <groupId>com.czxy</groupId>

  <!-- Project name -->

  <artifactId>maven01</artifactId>

  <!-- Version number, version number of the current project -->

  <version>0.0.1-SNAPSHOT</version>

  <!-- Packing method  

jar: javaSEproject

war:javaEEProject(WEBProject)

pom: Parent project (multi module explanation)-->

  <packaging>jar</packaging>

  <!-- nameandurl: Optional assignment -->   

  <name>mavendemo01</name>

  <url>http://maven.apache.org</url>

  <!-- properties:Attribute, which can be configured with many attributes. This label is not required -->  

  <properties>

    <!-- Encoding is configured by default -->

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<java.version>1.8</java.version>

<hehe.version>5.1</hehe.version>

  </properties>

  <!-- dependencies:Complex number, which can be used to place multiple -->

  <dependencies>

    <!-- dependency:Singular, specificjarpackage -->

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>${hehe.version}</version>

      <scope>test</scope>

    </dependency>

  </dependencies>

  <build>

    <!-- plugins:Plug-in unit-->

    <plugins>

        <!-- plugin:Specific plug-ins -->

        <plugin>

            <!-- UnifiedjdkVersion of plug-in, mandatory addition -->

            <groupId>org.apache.maven.plugins</groupId>

            <artifactId>maven-compiler-plugin</artifactId>

            <version>3.5.1</version>

            <configuration>

                <target>1.8</target>

                <source>1.8</source>

            </configuration>

        </plugin>

    </plugins>

  </build>

</project>

1.4 add custom dependency for POM

  • Local warehouse: (normal use of disconnected network)

 

  1. Network:

 http://www.mvnrepository.com/

Local warehouse import: it is applicable to know a class name under the project name / jar package name / jar package. The jar package must exist in the local warehouse

Network import: applicable to jar package not in local warehouse

1.5 scope of dependence

  • Different jar packages have their own scope of action, in order to use jar packages more accurately.
  • Dependency scope: jar package scope

2. Dependency conflict

2.1 dependency conflict

In the future, the project will crash due to different versions of the same jar package.

  • jar package dependency in Maven project:

Direct dependence: A depends on B

Project A, directly imported the jar package B

  • Indirect dependence (transitive dependence): A depends on B, B depends on C, A indirectly depends on C

Project A directly imports jar package B, which depends on jar package C, while project A indirectly depends on jar package C

Dependency conflict is caused by indirect dependency

  • Project A directly imports jar package B and jar package C
  • Jar package B relies on jar package D version 1.1
  • Jar package C relies on jar package D version 2.1

2.2 mediation principle depending on Conflict: priority for those who are close to the path

Path closest direct dependency

2.3 mediation principle relying on Conflict: the first statement takes precedence

  • Which indirect dependency is declared first will be imported according to its version

2.4 dependency conflict resolution: direct exclusion

It is recommended to exclude leaf nodes, otherwise it is easy to cause chain reaction

3.1 engineering stratification

Dao domain service web utils commons subproject

3.2 aggregation (multi module) and inheritance

  • Inheritance: in maven project, child project can inherit the dependency and plug-in configuration of parent project.
  • Aggregation: multiple maven subprojects can form a complete project, which is aggregation

  • Inheritance: child projects inherit parent project dependencies and plug-ins
  • Aggregation: the subprojects depend on each other to form a whole. The whole is an aggregation project. Aggregation and inheritance are usually used together

Tags: Programming Maven Apache network Java

Posted on Thu, 07 May 2020 08:10:28 -0400 by graham