Article Directory
1. IDEA configuration comes with the Maven plug-in
Idea also comes with a Maven plug-in, and we can also configure the built-in Maven plug-in, so we can use the built-in Maven or the Maven core program we installed
1.1 Configure the built-in Maven plugin
-
The Maven that comes with Idea is in the plugins directory of Idea's installation directory
-
After configuring the local warehouse in the built-in Maven, after opening Idea, you will find that the local warehouse automatically becomes the warehouse we set
-
Set up Maven to automatically guide the package
1.2 Configure our own Maven installation
-
Click Settings in the toolbar
-
Click OK to save
2. Create a Maven project in Idea
2.1 Create a Java project
-
Click File→New→Module…(If there is no Project before, select Project)→Maven
-
Click Next, configure the module to be inherited (if the item does not exist in the Project created directly), coordinates (GAV), and path. Different Idea versions may be different, I am using version 2019.3.3
-
Click Finish to create successfully
-
Configure Maven's core configuration file pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dqqqwd.maven</groupId>
<artifactId>Hello</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
- Write the main code
Create a package in the src/main/java directory and create a Hello.java file
package com.dqqqwd.maven;
public class Hello {
public String sayHello(String name){
return "Hello "+name+"!";
}
}
- Write test code
Create a package in the /src/test/java directory and create a HelloTest.java file
package com.dqqqwd.maven;
import org.junit.Test;
public class HelloTest {
@Test
public void testHello(){
Hello hello = new Hello();
String maven = hello.sayHello("Maven");
System.out.println(maven);
}
}
- Use Maven to run Maven projects
2.2 Create a Web project (understand)
- Create a simple Maven project, the packaging method is war package
<groupId>com.dqqqwd.maven</groupId>
<artifactId>MavenWeb</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
-
Click Project Structure
-
Select the corresponding Module, set the Web directory
-
A prompt box will pop up, click OK after selecting the version
-
Generate web.xml file
-
Set the directory for storing web page files and click OK
-
Click OK
-
Found that there is an additional web directory in the project, and there is a blue dot on the directory
-
Create an index.jsp page in the web directory
-
Deploy to Tomcat and run
3. Import the Maven project in Idea
-
Click Project Structure
-
Click Modules→➕→Import Module
-
Find the location of the project
-
Select Import module from external model→Maven→Finish