The first Maven program Hello

  1. Directory structure
    Hello
    src
    ——main
    ————java
    ————resources
    ——test
    ————java
    ————resources
    pom.xml The
    main directory is used to store the main program.
    The test directory is used to store test programs.
    The java directory is used to store source code files.
    The resources directory is used to store configuration files and resource files.
  2. Create Maven's core configuration file pom.xml
<?xml version="1.0" ?>
<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.mojie.maven</groupId>	
<artifactId>Hello</artifactId>	
<version>0.0.1-SNAPSHOT</version>	
<name>Hello</name>
<dependencies>	
<dependency>			<groupId>junit</groupId>		<artifactId>junit</artifactId>			<version>4.9</version>			<scope>test</scope>	
	</dependency>
	</dependencies>
	</project>
  1. Write the main code
    Create a new file Hello.java in the src/main/java/com/mojie/maven directory
package com.mojie.maven;
public class Hello {
    
    	
public String sayHello(String name){
    
    
		return "Hello "+name+"!";
			}
		}
  1. Write test code
    Create a new test file HelloTest.java in the /src/test/java/com/mojie/maven directory
package com.mojie.maven;	
import org.junit.Test;
import static junit.framework.Assert.*;
public class HelloTest {
    
    	
@Test	
public void testHello(){
    
    		
Hello hello = new Hello();		
String results =hello.sayHello("litingwei");		
assertEquals("Hello litingwei!",results);//断言	}
}
  1. Run a few basic Maven commands
    . Open the cmd command line, enter the root directory of the Hello project (the directory where the pom.xml file is located), execute the mvn compile command, check the root directory change
    , continue to enter the mvn clean command in
    cmd , and then check the root directory change again , enter the mvn clean compile command in cmd, check
    Enter the mvn test-compile command in the root directory change cmd, view the target directory change
    cmd enter the mvn clean test command, view the target directory change
    cmd enter the mvn clean package command, view the target directory change
    cmd enter the mvn source:jar command, view Target directory changes
    Note: When running Maven commands, you must enter the directory where the pom.xml file is located!

Guess you like

Origin blog.csdn.net/weixin_44051191/article/details/109238923