Maven (four): the use of Maven (middle)

foreword

This blogger will use CSDN to record the experience and knowledge he has personally gained and learned on the way to study software development. Interested friends can pay attention to the blogger! Perhaps a person can go fast alone, but a group of people can go farther!



1. Experiment 4: Create a Maven version of the Web project

1. Description

When using mvn archetype:generatethe command to generate Weba project, you need to use a special one archetype. This one that specifically generates Webthe engineering skeleton archetypecan refer to the official website to see its usage:

insert image description here

The parameter archetypeGroupId, archetypeArtifactId, archetypeVersionis used to specify the coordinates maven-archetype-webappof .

2. Operation

Note: If mvn archetype:generatethe command , Mavenan error will be reported: Cannot create other projects under a project pomother . So don't create a new project in the project just created, please go back to the root directory of the workspace to operate.

Then run the command to generate the project:

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.4

Follow the prompts to perform the following operations:

Define value for property 'groupId': com.atguigu.maven Define value for property 'artifactId': pro02-maven-web Define value for property 'version' 1.0-SNAPSHOT: :【直接回车,使用默认值】

Define value for property 'package' com.atguigu.maven: :【直接回车,使用默认值】 
Confirm properties configuration: groupId: com.atguigu.maven artifactId: pro02-maven-web version: 1.0-SNAPSHOT package: com.atguigu.maven Y: :【直接回车,表示确认】

3. The generated pom.xml

Confirm that the packaging method is in warthe form of a package

<packaging>war</packaging>

4. The directory structure of the generated Web project

insert image description here

webappThere areindex.jsp

WEB-INFThere areweb.xml

5. Create Servlet

5.1 Create a java directory under the main directory

insert image description here

5.2 Create the directory of the package where the Servlet class is located in the java directory

insert image description here

5.3 Create a Servlet class under the package

package com.atguigu.maven;
	
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
	
public class HelloServlet extends HttpServlet{
     
     
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
     
		
		response.getWriter().write("hello maven web");
		
	}
	
}

5.4 Register Servlet in web.xml

  <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>

6. Write a hyperlink on the index.jsp page

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

JSPThe full name is Java Server Page, Thymeleaflike , is a server-side page rendering technology. Here we don't have to care about JSP
syntax details, just write a hyperlink tag.

7. Compile

At this time, an error occurs when mvn compilethe command :

程序包 javax.servlet.http 不存在

程序包 javax.servlet 不存在

找不到符号

符号: 类 HttpServlet

…… 

The above error message shows: Our Webproject uses HttpServletthis class, and HttpServletthis class belongs to servlet-api.jarthis jarpackage . At this point we say that Webthe project needs to depend on servlet-api.jarthe package .

insert image description here

8. Configure the dependency on the servlet-api.jar package

For dependencies that do not know the details, you can go to the https://mvnrepository.com/ website to check. Use keywords to search, and then select the appropriate application in the search result list.

insert image description here

For example, we found the dependency information servlet-apiof :

<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>

In this way, the above information can be added pom.xml. Re-execute mvn compilethe command .

9. Package the Web project into a war package

Run mvn packagethe command to generate warthe location of the package as shown in the figure below:

insert image description here

10. Deploy the war package to run on Tomcat

warCopy the package to Tomcat/webappsthe directory

insert image description here

start Tomcat:

insert image description here
insert image description here

Try to access through browser:http://localhost:8080/pro02-maven-web/index.jsp

2. Experiment 5: Let Web projects depend on Java projects

1. Concept

Clarify an awareness: only Webprojects depend on projectsJava , not vice versa . Essentially, the Java project that the project depends on is actually the imported package in the project. Eventually the project will become a package and placed in the directory of the project.JavaWebWebWebjarJavajarWebWEB-INF/lib

2. Operation

In pro02-maven-webthe project pom.xml, find dependenciesthe tag , dependenciesand configure the following in the tag:

<!-- 配置对Java工程pro01-maven-java的依赖 -->
<!-- 具体的配置方式:在dependency标签内使用坐标实现依赖 -->
<dependency>
	<groupId>com.atguigu.maven</groupId>
	<artifactId>pro01-maven-java</artifactId>
	<version>1.0-SNAPSHOT</version>
</dependency>

3. In Web engineering, write test code

3.1 Supplementary creation directory

pro02-maven-web\src\test\java\com\atguigu\maven

3.2 Confirm that the Web project depends on junit

 <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

3.3 Create a test class

Copy the class of Javathe project to the directoryCalculatorTest.javapro02-maven-wb\src\test\java\com\atguigu\maven

4. Execute the Maven command

4.1 Test command

mvn test

Note: The compilation operation will be automatically executed in advance during the test operation. If the test is successful, it means that the compilation is also successful.

4.2 Packaging command

mvn package

insert image description here

By looking at the structure in warthe package , we can see that the projectWeb that the project depends on will indeed become a package in the directory of the project .JavaWebWEB-INF/libjar

insert image description here

4.3 View the list of jar packages that the current Web project depends on

mvn dependency:list

[INFO] The following files have been resolved:
[INFO] org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] javax.servlet:javax.servlet-api:jar:3.1.0:provided
[INFO] com.atguigu.maven:pro01-maven-java:jar:1.0-SNAPSHOT:compile
[INFO] junit:junit:jar:4.12:test

Description: javax.servlet:javax.servlet-api:jar:3.1.0:providedThe format displays the coordinate information of a jarpackage . The format is:
groupId:artifactId:打包方式:version:依赖的范围

Although this format is different from the format of the coordinates in our XMLconfiguration file, it is still coordinate information in essence. You need to be able to recognize this format. You Mavenwill be able to recognize this format when you see information in this format from the command log or error message in the future. is the coordinate. Then go to Maventhe warehouse to find the corresponding jarpackage according to the coordinates, and use this method to solve the error reporting situation we encountered.

4.4 View the dependency information of the current Web project in a tree structure

mvn dependency:tree

[INFO] com.atguigu.maven:pro02-maven-web:war:1.0-SNAPSHOT 
[INFO] +- junit:junit:jar:4.12:test 
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test 
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:provided 
[INFO] \- com.atguigu.maven:pro01-maven-java:jar:1.0-SNAPSHOT:compile 

We don't have a dependency pom.xmlin hamcrest-core, but it was added to our list of dependencies. The reason is: junitthe dependency is passed hamcrest-core, and then based on the transitivity of the dependency, hamcrest-coreit is passed to our project.

Guess you like

Origin blog.csdn.net/weixin_52533007/article/details/130144838