Maven from entry to master Chapter 2 Maven to run Java and Web projects

Run a Java project in maven

1. Create a new project

under the main directory

D:\maven_workspace\spaceVideo\pro01-maven-java\src\main\java\com\xyt\maven
 package com.xyt.maven;
  
public class Calculator {
    
    
  
  public int sum(int i, int j){
    
    
    return i + j;
  }
  
}

under the test directory

D:\maven_workspace\spaceVideo\pro01-maven-java\src\test\java\com\xyt\maven
package com.xyt.maven;
  
import org.junit.Test;
import com.xyt.maven.Calculator;
  
// 静态导入的效果是将Assert类中的静态资源导入当前类
// 这样一来,在当前类中就可以直接使用Assert类中的静态资源,不需要写类名
import static org.junit.Assert.*;
  
public class CalculatorTest{
    
    
  
  @Test
  public void testSum(){
    
    
    
    // 1.创建Calculator对象
    Calculator calculator = new Calculator();
    
    // 2.调用Calculator对象的方法,获取到程序运行实际的结果
    int actualResult = calculator.sum(5, 3);
    
    // 3.声明一个变量,表示程序运行期待的结果
    int expectedResult = 8;
    
    // 4.使用断言来判断实际结果和期待结果是否一致
    // 如果一致:测试通过,不会抛出异常
    // 如果不一致:抛出异常,测试失败
    assertEquals(expectedResult, actualResult);
    
  }
  
}

To build related commands, run them in the pom.xml directory, and enter the pom.xml of which project you operate.

2. Run the project

2.1 View the current directory

D:\maven_workspace\spaceVideo>dir
 驱动器 D 中的卷是 新加卷
 卷的序列号是 D4AF-9C80

 D:\maven_workspace\spaceVideo 的目录

2023/05/04  12:06    <DIR>          .
2023/05/04  11:38    <DIR>          ..
2023/05/04  12:06    <DIR>          pro01-maven-java
               0 个文件              0 字节
               3 个目录 118,766,297,088 可用字节

2.2 Compile the project

write main

D:\maven_workspace\spaceVideo>cd  pro01-maven-java

D:\maven_workspace\spaceVideo\pro01-maven-java>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.xyt.maven:pro01-maven-java >-------------------
[INFO] Building pro01-maven-java 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------

The directory where the compilation results of the main program are stored: target/classes

Write in the test directory
and write successfully

D:\maven_workspace\spaceVideo\pro01-maven-java>mvn test-compile
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.xyt.maven:pro01-maven-java >-------------------
[INFO] Building pro01-maven-java 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.0:resources (default-resources) @ pro01-maven-java ---
[INFO] skip non existing resourceDirectory D:\maven_workspace\spaceVideo\pro01-maven-java\src\main\resources
[INFO]
[INFO] --- compiler:3.10.1:compile (default-compile) @ pro01-maven-java ---
[INFO] Nothing to compile - all classes are up to date

The directory where the test program compilation results are stored: target/test-classes

2.3 Test the current project

mvn test
[INFO] Running com.xyt.maven.CalculatorTest
输出测试的程序代码
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.038 s - in com.xyt.maven.CalculatorTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  15.463 s
[INFO] Finished at: 2023-05-04T14:41:58+08:00
[INFO] ------------------------------------------------------------------------

Put the test report in this directory
insert image description here

2.4 Generate jar package

D:\maven_workspace\spaceVideo\pro01-maven-java>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.xyt.maven:pro01-maven-java >-------------------
[INFO] Building pro01-maven-java 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] Building jar: D:\maven_workspace\spaceVideo\pro01-maven-java\target\pro01-maven-java-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.694 s
[INFO] Finished at: 2023-05-04T14:49:04+08:00
[INFO] ------------------------------------------------------------------------

Problem: Did not download to the specified warehouse, but went to the default warehouse

2.5 Migrate data location

[INFO] Installing D:\maven_workspace\spaceVideo\pro01-maven-java\pom.xml to C:\Users\20120\.m2\repository\com\xyt\maven\pro01-maven-java\1.0-SNAPSHOT\pro01-maven-java-1.0-SNAPSHOT.pom
[INFO] Installing D:\maven_workspace\spaceVideo\pro01-maven-java\target\pro01-maven-java-1.0-SNAPSHOT.jar to C:\Users\20120\.m2\repository\com\xyt\maven\pro01-maven-java\1.0-SNAPSHOT\pro01-maven-java-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

It has indeed been migrated to the specified directory.
insert image description here
The effect of the installation is to store the jar package generated during the local build process into the Maven local warehouse. The path of this jar package in the Maven repository is generated based on its coordinates

2 Executing the Web project in MAVEN

insert image description here

1. Configuration items

1.1 Configure web.html

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
    <servlet>
    <servlet-name>helloServlet</servlet-name>
    <servlet-class>com.xyt.maven.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>helloServlet</servlet-name>
    <url-pattern>/helloServlet</url-pattern>
  </servlet-mapping>
</web-app>

1.2 Configure index.jsp

<html>
<body>
<h2>Hello World!</h2>
<a href="helloServlet">Access Servlet</a>
</body>
</html>

1.3 Registering servlets

  <servlet>
    <servlet-name>helloServlet</servlet-name>
    <servlet-class>com.atguigu.maven.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>helloServlet</servlet-name>
    <url-pattern>/helloServlet</url-pattern>
  </servlet-mapping>

1.4 Compile and run directly, and find that the configuration fails

D:\maven_workspace\spaceVideo>mvn compile
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.060 s

Reason:
missing dependent jar package
insert image description here

1.5 Recommended jar package search website

https://mvnrepository.com/

1.6 Compile successfully

D:\maven_workspace\spaceVideo\pro02-maven-web>mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.xyt.maven:pro02-maven-web >--------------------
[INFO] Building pro02-maven-web Maven Webapp 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ war ]---------------------------------

1.7 Packaging operation

D:\maven_workspace\spaceVideo\pro02-maven-web>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.xyt.maven:pro02-maven-web >--------------------
[INFO] Building pro02-maven-web Maven Webapp 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ war ]---------------------------------

2. Let the war package run on tomcat

2.1 copy the war package to the Tomcat/webapps directory

insert image description here

2.2 start tomcat

insert image description here

2.3 Verification effect

insert image description here

Guess you like

Origin blog.csdn.net/CNMBZY/article/details/130485808