Maven (four): the use of Maven (middle)
- foreword
- 1. Experiment 4: Create a Maven version of the Web project
-
- 1. Description
- 2. Operation
- 3. The generated pom.xml
- 4. The directory structure of the generated Web project
- 5. Create Servlet
- 6. Write a hyperlink on the index.jsp page
- 7. Compile
- 8. Configure the dependency on the servlet-api.jar package
- 9. Package the Web project into a war package
- 10. Deploy the war package to run on Tomcat
- 2. Experiment 5: Let Web projects depend on Java projects
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:generate
the command to generateWeb
a project, you need to use a special onearchetype
. This one that specifically generatesWeb
the engineering skeletonarchetype
can refer to the official website to see its usage:
The parameter archetypeGroupId
, archetypeArtifactId
, archetypeVersion
is used to specify the coordinates maven-archetype-webapp
of .
2. Operation
Note: If
mvn archetype:generate
the command ,Maven
an error will be reported: Cannot create other projects under a projectpom
other . 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
war
the form of a package
<packaging>war</packaging>
4. The directory structure of the generated Web project
webapp
There areindex.jsp
WEB-INF
There areweb.xml
5. Create Servlet
5.1 Create a java directory under the main directory
5.2 Create the directory of the package where the Servlet class is located in the java directory
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>
JSP
The full name isJava Server Page
,Thymeleaf
like , is a server-side page rendering technology. Here we don't have to care aboutJSP
syntax details, just write a hyperlink tag.
7. Compile
At this time, an error occurs when
mvn compile
the command :
程序包 javax.servlet.http 不存在
程序包 javax.servlet 不存在
找不到符号
符号: 类 HttpServlet
……
The above error message shows: Our
Web
project usesHttpServlet
this class, andHttpServlet
this class belongs toservlet-api.jar
thisjar
package . At this point we say thatWeb
the project needs to depend onservlet-api.jar
the package .
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.
For example, we found the dependency information
servlet-api
of :
<!-- 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-executemvn compile
the command .
9. Package the Web project into a war package
Run
mvn package
the command to generatewar
the location of the package as shown in the figure below:
10. Deploy the war package to run on Tomcat
war
Copy the package toTomcat/webapps
the directory
start
Tomcat
:
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
Web
projects 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.Java
Web
Web
Web
jar
Java
jar
Web
WEB-INF/lib
2. Operation
In
pro02-maven-web
the projectpom.xml
, finddependencies
the tag ,dependencies
and 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
Java
the project to the directoryCalculatorTest.java
pro02-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
By looking at the structure in
war
the package , we can see that the projectWeb
that the project depends on will indeed become a package in the directory of the project .Java
Web
WEB-INF/lib
jar
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:provided
The format displays the coordinate information of ajar
package . The format is:
groupId:artifactId:打包方式:version:依赖的范围
Although this format is different from the format of the coordinates in our
XML
configuration file, it is still coordinate information in essence. You need to be able to recognize this format. YouMaven
will 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 toMaven
the warehouse to find the correspondingjar
package 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.xml
inhamcrest-core
, but it was added to our list of dependencies. The reason is:junit
the dependency is passedhamcrest-core
, and then based on the transitivity of the dependency,hamcrest-core
it is passed to our project.