maven project management and build tool

1. What is maven

Maven 在美国是一个口语化的词语,代表专家、内行的意思,
Maven是一个项目管理工具,
它包含了一个项目对象模型(POM:Project Model),	一组标准集合,
一个项目生命周期(jar包引入、编码、编译、单元测试、打包war/jar),
解决大部分的jar包冲突(类的冲突、多个jar包中存在相同的类(相同的类全路径)),
报错信息(类找不到),依赖管理系统和一键构建用来运行定义在生命周期中插件目标逻辑。

Role: jar package introduction, coding, compilation, unit testing, packaging war/jar

2. What problems can maven solve

Project development is more than just writing code, it
will be accompanied by various essential things to do during the period, such as:

  1. We need to quote various jar packages, especially for relatively large projects.
    There are often dozens or even hundreds of
    jar packages. Every time a kind of jar package is used, it is necessary to manually import the project directory,
    and we often encounter various Crazy jar package conflicts, version conflicts.
  2. We worked so hard to write the Java file,
    but an idiot computer that only understands 0 and 1 can't read it at all and
    needs to compile it into binary bytecode.
    Anyhow, this work can be done for us by various integrated development tools.
    Eclipse, IDEA, etc. can compile the code on the fly.
    Of course, if you think your life is long, why not be extravagant,
    you can also type the code in Notepad, and then use the javac command to compile one by one.
  3. There is no code without bugs in the world.
    Computers like bugs just like people always like beautiful and handsome guys.
    In order to pursue beauty and reduce bugs, after writing the code,
    we have to write some unit tests,
    and then run them one by one to verify the quality of the code.
  4. No matter how elegant the code is, it will be sold.
    Later, we need to integrate the code with various configuration files and resources, and
    finalize and package them. If it is a web project, we also need to publish it to the server for use by others.
    Just imagine, if there is a tool that can free you from the tedious work above, it
    can help you build projects, manage jar packages, compile code,
    and can also help you automatically run unit tests, package, generate reports,
    and even help You deploy the project, generate the Web site, will you be excited?
    Maven can solve the problems mentioned above

3. Maven needs to be configured and downloaded

http://maven.apache.org/download.cgi

  • Configure the environment variables of maven, the environment variables of jdk must be configured with java8 or java11 or above
  • One is that the location of the central warehouse is changed to Aliyun
  • The second is to specify a local warehouse

https://www.cnblogs.com/huangting/p/11079087.html
https://blog.csdn.net/huo920/article/details/82082403

4. Use eclipse to create a maven project

Screenshot as proof


Choose the skeleton webapp



Import the servlet package to complete the

project structure. The

above structure is not perfect yet. A test package and a package for writing java code are required

. Complete project structure
Insert picture description here

5. xml dependent configuration scope

For example, the scope below is provided

<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>servlet-api</artifactId>
	<version>2.5</version>
	<scope>provided</scope>
</dependency>
  • compile

The default scope means that all dependencies can be used in the life cycle.
Moreover, these dependencies will be passed to dependent projects.

  • provided

Similar to compile, but indicates that the dependency is provided by the JDK or container,
such as Servlet API and some Java EE APIs.
This scope can only be used for compiling and testing, and it is not transitive.
When using this, the package will not be entered into the project, but when relying on the
default or other, the dependent project will be marked as a jar package and placed Lib of this project

  • runtime

Indicates that the dependency does not work at compile time, but it will work at runtime and testing

  • test

Indicates that the dependency is used during testing, but not at runtime

6. Common commands of maven

  • clean: clean up

Clean up the target directory under the project root directory

  • compile: compile

compile is the compile command of the maven project, which compiles
the files under src/main/java into class files and
outputs them to the target directory

  • test: unit test

There are requirements for the unit test class name: XxxTest.java (the command only recognizes the end of Test)
will execute the unit test classes in the project root directory and the src/test/java directory

  • package: package

web project —war package
java project —jar package Package
the project and package it to the target directory under the project directory

  • install: install

Solve the problem that multiple local projects share a jar package and package it in a local warehouse.
install is the installation command of the maven project,
execute install to mark maven into a jar package or war package and publish it to the local warehouse.

Find the root path of the project, command line.

mvn install 

Guess you like

Origin blog.csdn.net/qq_44783283/article/details/109290347