Location>code7788 >text

Maven's Top Features - Dependency Management

Popularity:811 ℃/2024-11-07 16:56:51

Dependency management

Problems solved by dependency management

After we learn the syntax of a language, we should be able to know the function reference or the creation of the class and so on. In order to facilitate the management, we will write some specific functions in a code file, we only need to use the time to import the line, so that either modify or read are more convenient and concise, but this will come out of a problem, once we have more than one file you refer to me, I refer to you, there will be a centipede in general nested (for example, A refer to B, B refer to C), so we migrate or Packaging of the entire project will be very complex, the whole reference is like a maze, manual management will be very troublesome. This time Maven can solve this problem

The Role of Maven

When we declare a dependency on A, Maven will determine if there are other dependencies on this module and other referenced modules, and if there are, it will automatically import the other dependencies without us having to determine if there are other dependencies. So how do we declare it? It's in theInside this file

This is a useful file, it's the configuration file for the project's maven, let's take a closer look at this file

<project ...>


	<modelVersion>4.0.0</modelVersion>
	<groupId></groupId>
	<artifactId>hello</artifactId>
	<version>1.0</version>
	<packaging>jar</packaging>


	<properties>
        <>UTF-8</>
		<>17</>
	</properties>


	<dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>2.0.16</version>
        </dependency>
	</dependencies>


</project>

Let's sort out the structure of the whole document:

We can break it down into a few chunks

<project ...>

  • beThe root element of the project contains all the configuration information for the project.
  • Namespaces and schemas are defined to ensure that files follow the Maven POM specification.
<project ... >

Configuration information for all projects.

</project>

The entire project's own information

<modelVersion>4.0.0</modelVersion>
<groupId></groupId>
<artifactId>hello</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

This is all the information you need to manage your project

A Maven project is a collection ofgroupId​,artifactIdrespond in singingversionas a unique identifier.

<modelVersion>

  • Defines the model version of the POM file, which is currently usually4.0.0​。

<groupId>

  • Indicates the group ID of the project, usually the reverse domain name of the organization to which the project belongs (e.g.​)。
  • Must be unique and is usually used to identify a company or organization.

<artifactId>

  • A unique identifier for the project, indicating the name of the module or project (e.g.my-app​)。
  • artifactIdIt should be in the samegroupIdDown is unique.

<version>

  • Define the version number of the project (e.g.1.0.0​)。
  • If it's a development version, you can use the-SNAPSHOTMarking (e.g.1.0.0-SNAPSHOT), indicating an unstable version.

<packaging>

  • Specify how the project should be packaged, the default isjar​。
  • Other common values arewar(Web application),pom(parent project), etc.

<properties..>

Inside this tab is the unified management of version numbers, encodings, and other configurations.

  • : Indicates the character encoding of the project source code, which should normally be set toUTF-8​;
  • : Indicates the version of the JDK used, e.g.21​;
  • : Indicates the version of the source code read by the Java compiler;
  • : Indicates the version of Class compiled by the Java compiler.
	<properties>
        <>UTF-8</>
		<>17</>
	</properties>
it's worth noting that...

Starting with Java 9, it is recommended to use theattribute to ensure that the source code input at compile time is the same as the compiled output version. If the source and output versions are different, you should set thecap (a poem)​。

pass (a bill or inspection etc)<properties>The defined attribute fixes the JDK version, preventing different developers on the same project from using different versions of the JDK.

<dependencies..>

Here are the dependencies needed for the entire project

  • Define the project's dependencies, each of which is defined with the<dependency>The label indicates.

    	<dependencies>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-simple</artifactId>
                <version>2.0.16</version>
            </dependency>
    	</dependencies>
    
  • Keyword elements:

    • <groupId>: The group ID of the dependency.
    • <artifactId>: The artifact ID of the dependency.
    • <version>: The version number of the dependency.

When we refer to other third-party libraries, we only need to identify them by these 3 variables. For example, a dependency onorg.slfj4:slf4j-simple:2.0.16: This is a dependency that we can download online and call directly, parsed down as
groupIdbecause oforg.slfj4
artifactIdbecause ofslf4j-simple
versionbecause of2.0.16

Generally we use the shorthand form when representing Maven dependenciesgroupId:artifactId:version

This is the time to go back and lookWe then realize that the whole structure is already very clear, a combination of various configurations

xml

Since the mention, then we will introduce the xml format, through the above example we can see that xml is very much like html syntax, but xml has a high degree of freedom, because xml is intended to rely on such hierarchical information to express the subject of a variety of information

The advantage of such a representation is that it is very structured, and structuring is undoubtedly very suitable and efficient for the entire computerized representation of information.

The xml representation is both very formulaic, using the<>Just name the box, and enter whatever information you want in it, because xml functions like an article, it's just there to read. It's also very flexible in that each<>All can be nested