Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Maven and idea use maven

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

1. Overview of maven 1. Background

In general, in project development, the code we develop depends on some additional libraries. To solve these dependencies, we need to manually download the corresponding jar package from the Internet, and then add it to the lib directory in the project to exist as a third-party library. And when there is a lot of dependence, this approach is very inefficient.

2. The function of maven

The core function of maven is to reasonably describe the dependencies between projects. In popular terms, you can specify the dependent project in the pomxml file, and then maven will automatically download the corresponding dependency and add it to the project without having to solve it manually.

3. Maven's concept of warehouse.

​ maven warehouse is divided into local warehouse, third-party warehouse (internal private warehouse) and central warehouse (online shared warehouse).

The ​ project itself will first look for dependencies in the local warehouse, and if it cannot be found, it will go to the central warehouse to download it locally and then use it.

4. The structure of maven java project

When you create a java project using maven, the directory structure is different, as follows:

Project_name-- pom.xml the dependent configuration file of the project-- src-- main-- java java source directory-- resources resource directory, including pictures Configuration files, etc.-- test-- java test java source directory-- resources test resource directory-- target compilation file output directory 5. Pom.xml dependency.

The dependencies that pom.xml uses to configure the project have been mentioned earlier. So what if it describes dependencies, such as how to uniquely determine dependencies (or coordinates), and so on.

How to get coordinates

A dependent coordinate has three basic necessary keywords (and many other keywords), namely groupId, artifactId, and version. The basic format for configuring a dependency is as follows:

All dependencies are included here xxx is generally the package name xxx is generally the version of jar required by the project name xxx, because a project may have multiple versions

Generally speaking, you can find the complete coordinates by looking for the corresponding table and project in the central warehouse.

For a more detailed explanation, see https://www.cnblogs.com/whgk/p/7112560.html

II. Configuration of maven itself

The configuration file of maven is mainly conf/settings.xml, and the configuration contents are as follows:

F:\ java_maven_repo alimaven aliyun maven http://maven.aliyun.com/nexus/content/groups/public/ central jdk-1.8 true 1.8 1.8 1.8 1.8 3. Configure IDEA to use maven1, configure IDEA to use the maven we just configured

File--setting-- searches for maven-Click Maven-- to set Maven Home Directory to the directory of the maven we set above, and modify User settings file to the settings.xml we configured above.

Note: each new project must be reconfigured here, otherwise it will be restored by default.

2. Configure maven system environment variables

Add the M2_HOME environment variable:

Add M2_HOME=F:\ apache-maven-3.3.9 to the system environment variable. And then save it.

Add the bin directory of maven to the Path environment variable:

% M2roomHOME%\ bin

3. Configure dependencies in maven

Create the maven project in IDEA, and then click pom.xml to add dependencies after you have created it.

4. The use of maven sub-module

Usually in a project, there will be multiple subprojects, or the existence of submodules. In general, a total maven project is created first, and then sub-modules can be added by right-clicking in idea, and after adding sub-modules, the information of sub-modules is automatically generated in the parent project, such as:

The modules tag ct_producer ct_consumer ct_analysis is generated in the pom.xml in the parent maven project, which means that there are three child modules named ct_producer, ct_consumer, and ct_analysis

Then each submodule has its own pom.xml. In the pom.xml of the parent project, write dependencies can be used by all child modules, but self-defined dependencies in child modules can only be used by themselves.

In general, we introduce dependencies that are required by all child projects in the parent project, and then introduce our own dependencies in the child projects.

Another way to use it is:

Declare but do not introduce dependencies in the parent project in the following ways:

Here is the specific dependency on org.scala-lang scala-library ${scala.version}

When you want to use this dependency in the submodule, you need to declare it again, but do not specify the dependent version again.

Org.scala-lang scala-library

In this way, dependencies and corresponding versions can be specified at the parent project level, which can standardize the version of common dependencies throughout the project and prevent problems caused by inconsistent dependencies.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report