IDEA - Configure Maven & Create

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

  1. The Maven that comes with Idea is in the plugins directory of Idea's installation directory
    Insert picture description here

  2. 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
    Insert picture description here

  3. Set up Maven to automatically guide the package
    Insert picture description here

1.2 Configure our own Maven installation

  1. Click Settings in the toolbar
    Insert picture description here

  2. Click OK to save

2. Create a Maven project in Idea

2.1 Create a Java project

  1. Click File→New→Module…(If there is no Project before, select Project)→Maven
    Insert picture description here

  2. 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
    Insert picture description here

  3. Click Finish to create successfully

  4. 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>
  1. 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+"!";
	} 
}
  1. 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);
	} 
}
  1. Use Maven to run Maven projects
    Insert picture description here

2.2 Create a Web project (understand)

  1. 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>
  1. Click Project Structure
    Insert picture description here

  2. Select the corresponding Module, set the Web directory
    Insert picture description here

  3. A prompt box will pop up, click OK after selecting the version
    Insert picture description here

  4. Generate web.xml file
    Insert picture description here

  5. Set the directory for storing web page files and click OK
    Insert picture description here

  6. Click OK

  7. Found that there is an additional web directory in the project, and there is a blue dot on the directory
    Insert picture description here

  8. Create an index.jsp page in the web directory
    Insert picture description here

  9. Deploy to Tomcat and run

3. Import the Maven project in Idea

  1. Click Project Structure
    Insert picture description here

  2. Click Modules→➕→Import Module
    Insert picture description here

  3. Find the location of the project
    Insert picture description here

  4. Select Import module from external model→Maven→Finish

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44556968/article/details/109735831