Maven build project, how to build Java and Web projects in IDEA and how to run

Keywords: Java Programming Maven intellij-idea

3. Construction of Maven project

3.1 contents of Maven project

The structure of the project is as follows:

See the following figure for details:

3.2 commands for manually building projects

mvn compile  	#compile
mvn clean 		   #eliminate
mvn test			#test
mvn package		#pack
mvn install			#Install to local warehouse

Just be familiar with the Maven project structure and understand it.

3.3 plug in construction Maven project

3.3.1 creating Java projects

Create a new folder and cmd call out the console to enter it. Paste the following code,

mvn archetype:generate
-DgroupId=com.demo
-DartifactId=java-project
-DarchetypeArtifactId=maven-archetype-quickstart
-Dversion=0.0.1-snapshot
-DinteractiveMode=false

As shown in the figure
When BUILD SUCCESS appears, it can be created successfully, as shown in the figure
Note: in this way, there is one less resources folder under the main and test folders, which needs to be created manually

3.3.2 creating a Java Web project

Java Web project creation operation

mvn archetype: generate
-DgroupId=com.demo
-DartifactId=web-project
-DarchetypeArtifactId=maven-archetype-webapp
-Dversion=0.0.1-snapshot
-DinteractiveMode=false

Note: Java Web project contains only more content than Java project, webapp and WEB-INF folder inside!

3.4 build Maven project configuration in IDEA

3.4.1 change the setting of Maven local warehouse in IDEA

Open IDEA settings in the startup interface, find settings in Configure, click to search Maven, and change it to the following figure,
In this way, Maven in the IDEA will store all relevant resource files in the previously configured local warehouse, so as not to occupy Disk C space!

3.5 building Java projects in IDEA

3.5.1 new Maven project

  • Click new project, find maven and create a new project, as shown in the figure
  • Change the Maven project name, location and the coordinates mentioned above. The coordinates are generally default, as shown in the figure
  • When you start a new Maven project, you will download various resource packages. Don't worry. Just wait patiently for it to be completed
  • The new project is as shown in the figure, but something is missing. You need to manually create a new folder named resources under test
  • Click test and right-click to select new - > directory. Enter resources manually or click, as shown in the figure
  • Change the newly created resources folder into a resource file, as shown in the figure

3.5.2 testing Maven project

  • Configure the test resource in pom.xm and enter the following code:
<dependencies>  
 <dependency> <groupId>junit</groupId>  
 <artifactId>junit</artifactId>  
 <version>4.13</version>  
 <scope>test</scope>  
 </dependency></dependencies>
  • Refresh the project after configuration, as shown in the figure
  • Create a new Demo class under the java directory in main, as shown in the figure,
    And add a test function in the class, as follows:
package com.demo;  
  
/**  
 * @DAuthor: Phil  
 * @Date 2021/11/4 15:58  
 **/public class Demo {  
    public String testMaven(String str)  
    {  
        String s = "hello " + str;  
 		System.out.println(s);  
 		return s;  
 	}  
}
  • Do the same as above. Test the Demo written above in the test directory and add a function. The code is as follows:
package com.demo;  
  
import org.junit.Assert;  
import org.junit.Test;  
  
/**  
 * @DAuthor: Phil  
 * @Date 2021/11/4 16:09  
 **/public class DemoTest {  
    @Test  
 public  void testDemo()  
    {  
        Demo demo = new Demo();  
 		String testMaven = demo.testMaven("Maven");  
// Test whether the expected value meets the expectation  
 		Assert.assertEquals("hello Maven",testMaven);  
	 }  
}
  • Click compile to compile the project, as shown in the figure
    When the following information appears, the compilation can be successful, as shown in the figure
  • Click the test item, and it will be successful when the result is as shown in the figure, as shown in the figure
    Note: shortcuts can also be configured
  • Click add, and the configuration is shown in the figure
  • Create a new operation type, as shown in the figure
  • Add compilation or test operations after searching Maven, as shown in the figure below
    The purpose is to facilitate subsequent Debug operations

3.6 building Web projects in IDEA

3.6.1 building Web projects

  • After opening IDEA, select Maven to create a new project and select a template, as shown in the figure
  • Find the template with web app suffix and select it, as shown in the figure
  • After changing the project name and location, select default one by one
  • After waiting for its loading, it is found that there are few things, as shown in the figure
  • Manually add a directory, as follows:
  1. Click the upper right corner, as shown in the figure
  2. Create a new test directory in the Module, as shown in the figure
  3. Similarly, create a new missing directory under the test and main directories, as shown in the figure
  4. Change the directory properties, as shown in the figure


  • The completed project structure is shown in the figure,

3.6.2 test items

3.6.1 download and execute plug-ins

Tomcat server is required for web project execution, which can be found in https://mvnrepository.com/ Download the plug-in.

  • Enter Tomcat maven in the search box and find the Tomcat plug-in, as shown in the figure below
  • Select Tomcat7 version, because this version is relatively stable, as shown in the figure
  • Select 2.2, as shown in the figure
  • Copy the Maven dependency and paste it into pom.xml, as shown in the figure below
  • Update Maven project, click as shown in the figure
  • After the update, you can see Tomcat in the plug-in management, as shown in the figure

3.6.2 testing

  • Click run in Tomcat plug-in, as shown in the figure
  • After waiting for it to run, find the website, as shown in the figure
  • Click to see the successful operation in the browser, as shown in the figure

Posted by tina88 on Thu, 04 Nov 2021 18:47:40 -0700