Shang Silicon Valley’s Maven course, notes recorded during self-study
B station video: Maven Zero-Basic Introductory Tutorial
Article directory
- I. Overview
- 2. Core concepts of Maven
- 3. Maven operation in IDEA
I. Overview
(1) Why do you need Maven
1. Current technology
Maven is not directly used to assist coding, and its fighting positions are not the above layers. Therefore, it is necessary for us to take a look at which aspects are the deficiencies of our existing technology through the actual needs of enterprise development.
2. Problems in development
2.1 A project is a project
- If the project is very large, it is not suitable to continue to use
package
to divide the module. It is best that each module corresponds to a project, which is conducive to division of labor and collaboration. - A project can be split into multiple projects with the help
Maven
of .
2.2 The jar package required by the project must be manually "copied" and "pasted" to the WEB-INF/lib directory
- The same
jar
package file appears repeatedly in different projects. On the one hand, it wastes storage space, and on the other hand, it also makes the project look bloated. - With the help of this function
Maven
,jar
the package saved in the "warehouse", and the project that needs to be used "quotes" this file interface, and there is no need to actuallyjar
copy the package.
2.3 The jar package needs to be prepared by someone else, or downloaded from the official website
- The official websites of different technologies provide
jar
package downloads in various forms. Some technical official websites provide downloads through specialized toolsSVN
such as . Ifjar
the package , its contents are likely to be non-canonical as well. Maven
Packages can be downloaded in a canonical way with the help ofjar
. Because alljar
packages are storedMaven
in the central warehouse according to unified specifications.jar
Packages downloaded in a canonical manner , the content is also reliable.- Tips: "Unified specification" is not only very important to the field of IT development, but also very important to the entire human society.
2.4 A jar package depends on other jar packages and needs to be manually added to the project
jar
Packages often do not exist in isolation, and manyjar
packages need the support of other jar packages to work normally, which we calljar
dependencies between packages. The most typical example is:commons-fileupload-1.3.jar
Dependscommons-io-2.0.1.jar
, the FileUpload package will not work properly without the IO package.Maven
The dependentjar
packages .
(2) What is Maven?
1. Introduction to Maven
Maven
It is an automated build , focusing on project build and dependency management for Java
the platform . Maven
The original meaning of the word is: expert, expert.
build concept
Building is not creating, creating a project is not equal to building a project.
Using "Java source files", "framework configuration files", "JSP", "HTML", "pictures" and other resources as "raw materials" to "produce" a process that can run the project.
Compile, deploy, build
1.1 compile
Take the Java source file [User.java] --> compile --> Class bytecode file [User.class] --> hand it over to the JVM for execution
1.2 deployment
(1) What a BS project ultimately runs is not the dynamic web project itself, but the "compiled result" of the dynamic web project.
- raw chicken --> processing --> cooked chicken
- Dynamic Web Engineering --> Compilation, Deployment --> Compilation Results
During the development process, all paths or classpaths configured in configuration files are based on the directory structure of the compiled results.
(2) Tips: runtime environment
In fact, it is a set of
jar
package references, andjar
the package itself is not copied into the project, so it is not a directory.
(3) Various links in the construction process
- ① Cleanup: Delete the previous compilation results (old class bytecode files) to prepare for recompilation.
- ② Compile: Compile
Java
the source program into a class bytecode file. - ③ Test: Automatically call
JUnit
the program to test the key points in the project to ensure the correctness of the key points in the iterative development process of the project. - ④ Report: After each test, record and display the test results in a standard format.
- ⑤ Packaging: Package a project containing many files into a compressed file for installation or deployment.
Java
The project corresponds tojar
the package , and the Web project corresponds towar
the package. - ⑥ Installation: In
Maven
the environment it specifically refers to installing the packaged result—jar
package orwar
package to the specified location in the "warehouse". - ⑦ Deployment: Deploy the packaged result to the remote warehouse or deploy
war
the package to the server to run.
(4) Automatic construction
Hand over the stylized work of "compile, package, deploy, and test" to the machine.
2. Deploy the Maven core program
Reference: Shang Silicon Valley_Screenshot of Maven operation steps.pdf + video p7
IDEA version see the third part of the notes
2. Core concepts of Maven
①POM ②Agreed directory structure ③Coordinates ④Dependency management ⑤Warehouse management ⑥Life cycle ⑦Plugins and targets ⑧Inheritance ⑨Aggregation
(1) Agreed directory structure
1. Create the agreed directory structure
- Create the first
Maven
project , project name: Hello. Reference: Maven Operation Guide.txt - [1] root directory: project name; [2] src directory: source code; [3] pom.xml file: core configuration of Maven project; [4] main directory: store main program; [5] test directory: store test program ; [6] java directory: store Java source files; [7] resource directory: store configuration files of frameworks or other tools
2. Why follow the agreed directory structure?
-
Maven
To be responsible for the automated construction of our project, takeMaven
compilation as an example, to compile automatically, it must know where theJava
source files are saved. -
If we want to let the framework or tools know about our own custom things, there are two ways:
-
Tell the framework explicitly in the way of configuration
<param-value>Classpath:spring-context.xml</param-value>
-
Follow the framework's internal and existing conventions
log4j.properties
orlog4j.xml
form
-
-
Now
JavaEE
the development field generally agrees with a point of view: convention > configuration > coding. It means that the problems that can be solved by configuration will not be coded, and those that can be based on the agreement will not be configured. And itMaven
is precisely because of the specified directory where specific files are saved that we canJava
automatically build our project.
(2) Commonly used commands and networking issues
Note: To execute
Maven
commands , you must enterpom.xml
the directory where is located.
- Related to the build process: compile, test, package, …
1. Commonly used Maven commands
Enter the command in cmd
(1) mvn clean: clean up
(2) mvn compile: Compile the main program
(3) mvn test-compile: Compile the test program
(4) mvn test: execute the test
(5) mvn package: package
(6) mvn install: install
(7) mvn site: generate site
2. About networking issues
(1) Maven
Only the abstract life cycle is defined in the core program, but the specific work must be done by a specific plug-in. The plug-in itself is not included in the core program Maven
of .
(2) When Maven
the command needs to use some plug-ins, Maven
the core program will first search in the local warehouse.
(3) The default location of the local warehouse: [the home directory of the current user in the system].m2\repository
C:\Users\[登录当前系统的用户名]\.m2\repository
(4) Maven
If the core program cannot find the required plug-in in the local warehouse, it will automatically connect to the external network and download it to the central warehouse. If the external network cannot be connected at this time, the build will fail.
(5) Modifying the location of the default local warehouse allows Maven
the core program to search for plug-ins in the directory we prepared in advance.
-
Find the
Maven
decompression directoryD:\develop\Maven\apache-maven-3.2.2\conf\settings.xml
-
Find the tag in
settings.xml
the filelocalRepository
and take the tag out of the comment -
Modify the content of the label body to the Maven warehouse directory of the preparation and:
<localRepository>/path/to/local/repo</localRepository>
- Change it to: (Remember to decompress the RepMaven.zip archive first and put it in the RepMaven folder)
<localRepository>D:\develop\Maven\RepMaven</localRepository>
(3) POM
- Project Object Model: Project object model. Encapsulate the relevant information of the Java project into an object as a model for easy operation and management. (DOM Document Object Model: Document Object Model)
pom.xml
ForMaven
the project, it is the core configuration file, and all settings related to the Goi teaching plan process are configured in this file. The level of importance is equivalentweb.xml
to that of dynamic Web engineering.
(4) Coordinates
1. Coordinates in Geometry
(1) Using two vectors x and y in a plane can uniquely determine a point in the plane.
(2) Use the three vectors x, y, and z in the space to uniquely determine a point in the space.
2. Maven coordinates
Use the following three Maven
vectors to uniquely identify a Maven
project in the warehouse of :
(1) g groupId: reverse domain name of the company or organization + current project name
<groupId>com.atguigu.maven</groupId>
(2) a artifactId: the module name of the current project
<artifactId>Hello</artifactId>
(3) v ersion: the version of the current module
<version>1.0.0</version>
3. Correspondence
The correspondence between the coordinates of the Maven project and the path in the warehouse:
- Example: Find resources in the RepMaven folder
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.0.RELEASE</version>
- The path is:
org.springframework/spring-core/4.0.0.RELEASE/spring-core-4.0.0.RELEASE.jar
(5) Warehouse
1. Classification
(1) Local warehouse: Serving all Maven
projects .
(2) Remote warehouse:
- ① Private server: set up in the current local area network environment and serve all
Maven
projects .
- ② Central warehouse: set up on the Internet to serve all
Maven
projects . - ③ Mirror image of the central warehouse: set up on each continent to share traffic for the central warehouse. Reduce the pressure on the central warehouse and respond to user requests faster.
2. Content saved in the warehouse
(1) Maven
Plug-ins you need
(2) Maven
Projects
(3) jar
Packages
- No matter what kind of
jar
package , the directory structure is generated according to the coordinates in the warehouse, so it can be queried or relied on in a unified way.
(6) Life cycle
1 Overview
(1) The order of execution of each construction link: the order cannot be disturbed, and must be executed in accordance with the established correct order.
(2) Maven
An abstract life cycle is defined in the core program, and the specific tasks in each stage of the life cycle are completed by plug-ins.
- Maven has three sets of independent life cycles, namely:
- ①Clean Lifecycle performs some cleaning work before the real build.
- ②The core part of Default Lifecycle construction, compilation, testing, packaging, installation, deployment and so on.
- ③Site Lifecycle generates project reports, sites, and publishes sites.
- They are independent of each other, you can only call clean to clean the working directory, and only call site to generate the site. Of course, you can also run mvn clean install site directly to run all three sets of life cycles.
- Each life cycle consists of a set of phases (Phase), and the commands we usually enter on the command line always correspond to a specific phase. For example, run mvn clean, which is a phase of the Clean lifecycle. There is a Clean life cycle and a clean phase.
2. Clean life cycle
The Clean life cycle consists of three phases:
(1) pre-clean performs some work that needs to be done before clean.
(2) clean removes all files generated by the previous build.
(3) post-clean performs some work that needs to be completed immediately after clean.
3. Site life cycle
The Clean life cycle consists of four phases:
(1) pre-site performs some work that needs to be done before generating site documentation.
(2) site generates the site documentation for the project.
(3) post-site performs some work that needs to be done after the site documentation is generated and prepares for deployment.
(4) site-deploy deploys the generated site documents to a specific server.
- The site stage and site-deploy stage are often used here to generate and publish
Maven
the site This isMaven
a very powerful function, and the Manager likes it more. The documents and statistical data are automatically generated, which is very nice.
4. Default life cycle
The Default life cycle is the most important one in Maven
the life cycle, and most of the work takes place in this life cycle. Here, only some of the more important and commonly used stages are explained:
- validate
- process-resources: Copy and process resource files to the target directory, ready for packaging.
- compile: Compile the source code of the project.
- process-test-resources: Copy and process resource files to the target test directory.
- test-compile: compile test source code.
- process-test-classes
- test: Run tests using a suitable unit testing framework, the test code will not be packaged or deployed.
- prepare-package
- package: Accept compiled code and package it into a releasable format, such as JAR.
- install: Install the package to the local warehouse so that other projects can depend on it.
- deploy: Copy the final package to a remote warehouse for other developers to share with the project or deploy to the server to run.
5. Life cycle and automated construction
- When any phase is run, all previous phases will be run. For example, when we
mvn install
run , the code will be compiled, tested, and packaged. That'sMaven
why it's possible to automate every aspect of the build process. - Also,
Maven
the plugin mechanism is completelyMaven
dependent on the lifecycle of the , so understanding the lifecycle is crucial. - For example, execute
mvn package
the command , and all stages before it will be executed, as shown in the figure:
(7) Plug-ins and targets
(1) Maven
The core only defines the abstract life cycle, and the specific tasks are all completed by the plug-in.
(2) Each plug-in can implement multiple functions, and each function is a plug-in target.
(3) Maven's life cycle is bound to the plug-in goal to complete a specific construction task.
- For example: compile is a goal of the plugin maven-compiler-plugin; testCompile is a goal of the plugin maven-clean-plugin.
life cycle stage | plugin target | plug-in |
---|---|---|
compile | compile | maven-compiler-plugin |
test-compile | testCompile | maven-compiler-plugin |
- You can think of a target as a "command that invokes a plugin's functionality".
(8) Dependence
- Create a second
Maven
project , project name: HelloFriend. Reference: Maven Operation Guide.txt
1. Purpose of reliance
When jar
package A uses jar
some classes in package B, A has a dependency on B, which is a conceptual description. So how to introduce a jar
package ? The answer is very simple, just use dependency
the tag to specify jar
the coordinates of the dependent package.
- The dependency relationship between our two customized projects: the second
Maven
project HelloFriend depends on the firstMaven
project Hello .pom.xml
The configuration in HelloFriend is as follows:
<dependency>
<groupId>com.atguigu.maven</groupId>
<artifactId>Hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
Maven
When parsing dependency information, it will look for dependentjar
packages . ForMaven
the project , we must usemvn install
the command to install it before we can enter the warehouse.
2. Scope of dependency
In the above dependency information, in addition to the coordinates of the target jar
package there is also a scope
setting, which is the scope of the dependency. There are several optional values for the scope of dependencies, and we can use three: compile, test, and provided.
(1) Understand the difference between compile and test from the perspective of project structure:
- For the second
Maven
projectMaven
HelloFriend, the first project Hello must be dependent on both the main program "main" and the test program "test", so the compile scope dependency should be valid for both the main program and the test program, and participate in Pack; - For the second
Maven
project , HelloFriend, the JUnit unit test is only depended on by the test program "test", and the main program "main" does not need it, so the test scope dependency is only valid for the test program and does not participate in packaging.
(2) Understand the difference between compile and provided from the two different stages of development and operation:
- compile scope dependencies:
- Provided scope dependencies:
In the development stage, it needs to depend on the servet-api.jar package, but when it runs on Tomcat, the Servlet container will provide the dependency, so the jar package dependency provided by Tomcat should be used. Therefore provide scope dependencies are only valid for the development process and do not participate.
- Is it valid for the main program: valid
- Is it valid for the test program: Valid
- Whether to participate in packaging: not involved
- Whether to participate in deployment: not involved
(3) Summary of effectiveness
compile | test | provided | |
---|---|---|---|
main program | √ | × | √ |
test program | √ | √ | √ |
Participate in deployment | √ | × | × |
3. Transitivity of dependencies
- Create a third
Maven
project , project name: MakeFriends. Reference: Maven Operation Guide.txt
(1) Example: After adding the spring-core dependency in the Hello project, the spring-core dependency is automatically added in both the HelloFriend project and the MakeFriend project.
- Add spring-core dependency to the Hello project:
- The HelloFriend project and MakeFriend project are automatically added:
- Hierarchy of dependencies: (direct and indirect dependencies)
(2) Benefits: The transitive dependencies do not need to be declared repeatedly in each module project, but only once in the "bottom" project.
(3) Note: Non-compile scope dependencies cannot be passed. Therefore, in each project module, if necessary, you have to repeat the declaration of dependencies.
4. Exclusion of reliance
If we introduce a dependency A in the current project, and A depends on B, then B, which A depends on, Maven
will be automatically introduced into the current project, but in some cases, B may be an unstable version, or it may affect the current project adverse effects. At this time, we can exclude B when introducing A.
(1) Examples of occasions where dependency exclusion needs to be set:
(2) Configuration method:
<dependency>
<groupId>com.atguigu.maven</groupId>
<artifactId>HelloFriend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
(3) Effect after exclusion:
5. The principle of dependence
- The principle of dependence: resolve jar package conflicts
(1) The shortest path is first
(2) When the paths are the same, the one that declares first takes precedence
- The order of "declaration" here refers to the order of configuration of the dependency tag.
6. Unified management of dependent jar packages
It is best to use the same version for a set of jar
packages . In order to facilitate the upgrade of the framework, the version information of jar
the package be extracted uniformly.
(1) Use custom tags in the properties tag to uniformly declare the version number
- The atguigu.spring.version part is a custom label
<properties>
<atguigu.spring.version>4.0.0.RELEASE</atguigu.spring.version>
</properties>
(2) Where the same version is required, use ${custom tag name} to refer to the previously declared version number
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${atguigu.spring.version}</version>
</dependency>
...
</dependencies>
(3) Other usage
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
(9) Inheritance
1 Overview
1.1 Status
(1) The JUnit version that Hello depends on: 4.0
(2) The JUnit version that HelloFriend depends on: 4.0
(3) The JUnit version that MakeFriends depends on: 4.9
- Since the dependencies in the test range cannot be transferred, they will inevitably be scattered in various module projects, which can easily cause version inconsistencies.
1.2 Requirements
Unify the versions that each module project depends on JUnit.
1.3 Solutions
The JUnit dependencies are uniformly extracted into the "parent" project, and the version is not specified when declaring the JUnit dependencies in the child project, and the unified setting in the parent project shall prevail. It is also easy to modify.
2. Operation steps
(1) Create a Maven
project as the parent project Parent
. Note: The packaging method is pom.
<groupId>com.atguigu.maven</groupId>
<artifactId>Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
(2) Declare a reference to the parent project in the subproject.
<parent>
<!-- 父工程坐标 -->
<groupId>com.atguigu.maven</groupId>
<artifactId>Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 指定从当前子工程的pom.xml文件出发,查找父工程的pom.xml的相对路径 -->
<relativePath>../Parent/pom.xml</relativePath>
</parent>
(3) Delete the duplicate content (yellow line) between the coordinates of the sub-project and the coordinates of the parent project.
- 警告:GroupId is duplicate of parent groudId
(4) Unify the dependencies of JUnit in the parent project.
- Use the dependencyManagement tag in the Parent project to configure dependency management.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
(5) Delete the version number part of JUnit dependency in the subproject.
Notice
After the configuration is inherited, the parent project must be installed first when executing the installation command, otherwise the child project will fail to install.
(10) Aggregation
10.1 Why use aggregation?
After splitting multiple projects into modules, dependencies need to be manually installed to the warehouse one by one before they can take effect. After modifying the source code, you also need to manually perform clean operations one by one. After using aggregation, you can install and clean up Maven
projects .
10.2 Configuration method
-
Create a new "total aggregation project", use the modules/module tag combination, configure each module participating in the aggregation, and specify the relative path of the module project.
-
The order does not matter, but it is best to write in the order of dependencies.
<!-- 配置聚合 -->
<modules>
<!-- 指定各个子工程的相对路径 -->
<module>../Hello</module>
<module>../HelloFriend</module>
<module>../MakeFriends</module>
</modules>
10.3 Usage
Right click on the pom.xml of the aggregation project --> run as --> maven install.
Maven cool station
We can go to http://mvnrepository.com/ to search for the dependency information of the required jar package.
3. Maven operation in IDEA
(1) Install the Maven core program
1. Unzip
(1) Download the compressed package from the official website, download address: http://maven.apache.org/ (taken directly from the data here)
(2) Command line input: echo %JAVA_HOME% , check the JAVA_HOME environment variable. Maven
It is developed in Java, so you must know the installation directory of JDK in the current system environment.
(3) Unzip apache-maven-3.6.3.zip to a non- Chinese directory without spaces.
2. Copy path
Copy the upper level path of bin: D:\develop\Maven\apache-maven-3.6.3
3. Configure environment variables
(1) Right-click on this computer --> Properties --> Advanced System Settings --> Environment Variables --> New (system variable or user variable) --> MAVEN_HOME --> Fill in the copied path (D:\develop\ Maven\apache-maven-3.6.3).
(2) Connect the environment variable --> path --> New --> %MAVEN_HOME%\bin --> OK.
(3) The advantage of configuring environment variables is: when we want to replace other versions of maven
the core program, we only need to change the variable value of the MAVEN_HOME environment variable.
4. Test
win+r --> Enter cmd --> Enter mvn -v on the command line --> If relevant information appears, the configuration is successful.
(2) Configure the local warehouse and Alibaba Cloud mirror
1. Configure the local warehouse
(1) Maven
The core program does not contain specific functions, but is only responsible for macro scheduling. The specific functions are completed by plug-ins. Maven
The core program will look for plugins in the local repository. If it is not available in the local warehouse, it will be downloaded from the remote central warehouse. If you cannot access the Internet at this time, you cannot Maven
perform specific functions. In order to solve this problem, we can Maven
point the local repository of to a directory that has been downloaded under network conditions.
- Create a new folder under D:\develop\Maven\: LocalRepository, used as a local warehouse to store downloaded
jar
packages
(2) Maven's default local warehouse: ~.m2\repository directory. Tips: ~ indicates the current user's home directory.
(3) Find Maven's core configuration file settings.xml file:
D:\develop\Maven\apache-maven-3.6.3\conf\settings.xml
(4) Setting method: (path of local warehouse)
<localRepository>D:\develop\Maven\LocalRepository</localRepository>
2. Configure Alibaba Cloud Mirroring
For the convenience of downloading jar
the package , configure the following tags in the tagMaven
of the core configuration file settings.xml file :<mirrors></mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
3. Configure the default JDK version
In settings.xml, modify the default creation to JDK1.8, and the default operating environment of Maven
the projects will be JDK1.8, no need to manually modify it.
(1) Find Maven's core configuration file settings.xml file:
D:\develop\Maven\apache-maven-3.6.3\conf\settings.xml
(2) Configure the 1.8 version in <profile></profiles>
the tag :
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
(3) Using Maven in IDEA
1. Configure Maven
1.1 Configure Maven installed by yourself
- Create a new empty project named: Maven.
(1) Choose Maven
the version .
(2) Modify the path of settings.xml and the path of the local warehouse:
(3) Check the required jar
packages :
1.2 Configure the built-in Maven
In the same way, you can modify the configuration Maven
that to facilitate unified management.
In the plugins directory of Idea’s installation directory, Maven that comes with Idea changes the configuration in settings.xml: (D:\develop\IntelliJ IDEA 2019.3.3\plugins\maven\lib\maven3\conf\ settings.xml)
(1) Local warehouse:
<localRepository>D:\develop\Maven\LocalRepository</localRepository>
(2) Alibaba Cloud image:
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
(3) Default JDK version:
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
(4) Results:
2. Create a Maven project
2.1 Create a Java project
(1) Click File→New→Module... (if there is no Project before, select Project)→Maven.
(2) Configure the module to be inherited (if the project is directly created, this item does not exist), coordinates (GAV), and path.
(3) Click Finish to create successfully.
(4) Configure the core configuration file pom.xml of Maven.
<?xml version="1.0" encoding="UTF-8"?>
<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>cn.irene.maven</groupId>
<artifactId>Hello</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
(5) Write the main code: create a package in the src/main/java directory and create a Hello.java file
package cn.irene.maven;
public class Hello {
public String sayHello(String name) {
return "Hello" + name + "!";
}
}
(6) Write test code: Create a package in the /src/test/java directory and create a HelloTest.java file
package cn.irene.maven;
import org.junit.Test;
public class HelloTest {
@Test
public void testHello(){
Hello hello = new Hello();
String maven = hello.sayHello("Maven");
System.out.println(maven);
}
}
(7) Use Maven to run the Maven project
- Just double-click the command name to run it.
2.2 Create a Web project (understand)
(1) Create a simple Maven project, packaged as a war package
<groupId>cn.irene.maven</groupId>
<artifactId>MavenWeb</artifactId>
<!-- Web工程的打包方式为war -->
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
(2) Click on Project Structure
(3) Select the corresponding Module and set the Web directory
(4) A prompt box will pop up, select the version and click OK
(5) Generate web.xml file
(6) Set the directory for storing web page files and click OK
(7) Click OK
(8) It is found that there is an additional web directory in the project, and there is a blue dot on the directory
(9) Create the index.jsp page in the web directory
(10) Deploy to run on Tomcat
3. Import Maven project
(1) Click on Project Structure
(2) Click Modules→plus sign→Import Module
(3) Find the location of the project
(4) Select Import module from external model (import module from external model)→Maven→Finish