Detailed tutorial on using Maven in IDEA

Application of Maven in IDEA

Article directory

3.1 IDEA integrates Maven

There is a built-in maven in idea, but the built-in one is generally not applicable, because it is inconvenient to modify the maven configuration with the built-in maven. To use maven installed by yourself, you need to override the default settings in idea. Let idea know about the maven installation.

There are two places to configure: File-settingsandFile-other settings

The configuration in settings is for the currently opened project, and the other settings are for the newly created project. So both places have to be configured.

image-20210806105102623

If not File-other settings, it File - New Project Settingscan be found in .

image-20210806112029004

3.1.1 settings settings

The following is a description of the settings in the settings:

image-20210806105342383 image-20210806105738349

After entering the maven interface, we need to configure the following three pieces of information:

  • Maven home path: maven installation directory
  • User settings file: files in the maven installation conf/settings.xmldirectory
  • Local repository: local repository location

image-20210806105940771

First is the Maven home path:

image-20210806110403075

Then set the settings.file:

image-20210806110745821

image-20210806110936765

If the Local repository is wrong, it can also be modified.

Then we make two modifications under Maven-Runner:

  • -DarchetypeCatalog=internal: When maven creates a project, it will download the template file online, which is relatively large. If you use this option, you don't need to download it and create a maven project block.
  • Set JDK version

image-20210806111629745

  • For new project settings, the setting methods and steps are basically the same as the above settings, and will not be repeated here.

When you create a new project, the following settings will take effect.

image-20210806112155942

3.2 IDEA creates a Maven version of the Java project

3.2.1 Creating a project using a template

First we create an empty project and create a new module under the project:

The template we choose is: maven - archetyoe - quickstart, which represents the template of a common java project.

image-20210806114929675Then click next to go to the next step.

image-20210806115057185

Then enter the coordinates to fill in:

image-20210806115344709

Finish is over.

image-20210806115647948

But there is no resources directory under main. This is because the template we selected does not have it, so if necessary, create a resources directory in the new directory of idea. Then select the newly created resources directory, right-click, and select Make directory asFurther to Resources Rootcomplete the settings.

image-20210806134125009

Next, we also set the resources directory under test. After the setting is completed, it is the complete directory structure:

image-20210806134601181

3.2.2 Creation of test classes

image-20210806141259382

To run the Test method, you can directly select the corresponding running interface:

image-20210806141633014

Using the maven tool window in idea, we can get the effect of the previous common maven commands.

image-20210806142939724

3.3 IDEA creates a Maven version web project

First, we create a new maven module like the above to create a Javase project. Choose the appropriate template:

image-20210806145201940

The following steps are the same as the steps for creating a java project. Pay attention to observe whether the settings such as settings are correct.

In addition, you can add the groupId and other information you set yourself.

Finally, we complete the directory of the complete web project:

image-20210806150429046

Then we add the relevant dependencies to the pom file:

<!--加入servlet依赖(servlet的jar包)-->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>

<!--jsp的相关依赖(jsp相关的jar添加进来)-->
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.1</version>
    <scope>provided</scope>
</dependency>

image-20210806152612413

3.4 Import an existing maven project in Idea

3.4.1 Common Tips

image-20210806200951331

image-20210806201148575

3.4.2 Importing Existing Modules

  • Press and hold Ctrl+Alt+Shift+Sto open the project structure settings

    Or the folder icon with three small blue squares in the upper right corner of the interface, enter settings

    image-20210806201932222

  • Click the plus sign in the pop-up interface: Add, select Import Module to import the module

  • Select the module to be imported and perform further JDK and other settings.

Guess you like

Origin blog.csdn.net/m0_54850467/article/details/123708785