Building project with Maven

01, what Maven is?

Regardless of good English skills, look Tell me what network it gives the introduction to the language:

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.

That roughly means, Apache Maven is a project management and build automation tool, based on the concept of a project object model (POM), you can build the project management, reporting, and documentation. Apache organization as a highly successful open source project, Maven primarily serving the Java-based project build, dependency management, and project information management .
Maven convention over configuration using the principles of these principles in the following table:

table of Contents purpose
${basedir} Pom.xml store and all subdirectories
${basedir}/src/main/java Java source code is stored
${basedir}/src/main/resources Items stored in resource files
${basedir}/src/test/java Storing the Test class
${basedir}/src/test/resources Storage resources used for testing
${basedir}/src/main/webapp Store web front-end file
${basedir}/target After the project package output directory
${basedir}/target/classes When the project is compiled output directory
~/.m2/repository The default Maven repository directory (~ indicates the user directory)

Use convention priority allocation brings the greatest benefit is very unified directory structure diagram of the project, different developers in the development of a Maven project, file location is almost no difference, save a lot of unnecessary trouble, help promote the standardization of the project team.

This is a very open-source advocate the spirit of the era, almost all of Java projects will borrow some third-party open source libraries, these libraries can be introduced to the project by dependent manner. But with the increasing dependence of version conflicts, bloated dependent problems would follow. Hand solve these problems is very boring, fortunately Maven provides an excellent solution for it through a three-dimensional coordinates ( <groupId>, , <artifactId>) <version>to accurately locate each open-source library.

In addition, I think Maven particularly good thing is, it's all dependent on the project open-source library are downloaded from a remote central warehouse to the specified local warehouse, that is to say, these open source libraries can be shared across multiple projects, without re-downloading - if I use Maven to build the project, then my partner would not have to download so many open-source library, his local warehouse may already have.

02, configure Maven environment

1) Before you install Maven, make sure the JDK is already installed on the computer.

2) to Maven official website to download and extract the desired version, download address is:
https://maven.apache.org/download.cgi

3) set the environment variable, mainly MAVEN_HOME and Path.

Here Insert Picture Description
4) Open a command line, enter mvn -vverification Maven successful configuration

Here Insert Picture Description
5) under the Maven conf directory contains a very important document settings.xml, under normal circumstances, I tend to copy the file to ~/ .m2/the directory - I suggest you do the same.

Open the file, <mirrors></mirrors>add the address of the next mirror aliyun node (reference to the following code). Why do you do that? Because the default Maven repository without access to difficult over the wall.
Here Insert Picture Description
It should be noted that the <mirrorOf>values are the following four ways:

  • <mirrorOf>*<mirrorOf>:匹配所有远程仓库,也就是说任何对于中央仓库的请求都会转至该镜像。
  • <mirrorOf>external:*<mirrorOf>:匹配所有不在本机上的远程仓库
  • <mirrorOf>repo1,repo2<mirrorOf>:匹配仓库repo1和repo2,使用逗号分隔多个远程仓库。
  • <mirrorOf>*,!repo<mirrorOf>:匹配所有远程仓库,repo除外,使用感叹号将仓库从匹配中排除。

在Eclipse下依次选择菜单Window→Show View→Other→Maven→Maven WorkspaceBuild,查看镜像是否配置成功:
Here Insert Picture Description

03、快速创建Maven项目

为了尽快步入重点,这一小节会有意避重就轻,没必要的步骤会略过。

1)在Eclipse中新建项目的时候选择Maven Project.

2)在接下来选择项目类型的时候,先择Maven-archetype-quickstart,如下图所示:

Here Insert Picture Description

3)然后指定项目参数的时候填写group id和artifact id

4)项目创建成功后的目录结构图如下图所示

Here Insert Picture Description
Maven3默认使用的依然是JDK1.5,不过我们可以为其配置更高版本的JDK,下面会讲到。

04、详细分析pom.xml

毫无疑问,Maven项目的灵魂只有一个,那就是pom.xml文件,所以接下来我会详细地对其进行分析。

1)项目基本信息
pom.xml文件的第一部分主要用来描述项目的基本信息

<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>com.cmower</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>test</name>

</project>
  1. <project>是pom.xml的根元素,声明了相关的命名空间。
  2. <modelVersion>指定了当前项目对象模型(POM)的版本,对于Maven3.x来说,该值只能是4.0.0
  3. <groupId>定义了项目的组名,这个组往往和项目所在的组织或公司关联
  4. <artifactId>定义了当前Maven项目在组中唯一的ID
  5. <version>定义了项目的版本号,SNAPSHOT为快照的意思,也就是说该项目还处于开发阶段
  6. <packaging>定义了项目的打包类型,可选值有war、jar等
  7. <name>定义了项目的名称

2)变量配置信息
pom.xml文件的第二部分通常用来配置一些变量信息

<properties>

<spring.version>5.1.5.RELEASE</spring.version>

</properties>

有了变量的配置信息后,可以通过${spring.version}的形式来调用这些配置项。这样做的好处显而易见,当依赖项的版本升级的时候,可以直接修改变量值即可。

3)依赖管理
阿里云的Maven仓库下有各种各样的第三方仓库,换句话说,只有你想不到的,没有你找不到的。大多数Maven项目的依赖项列表都会很长很长,为了便于说明,下面只列出某些具有特色的。

Here Insert Picture Description

  1. 上文中曾提到,<groupId><artifactId><version>合起来可以准确地定位一个依赖项。那么怎么找到想要的依赖项呢?

第一步,进入MavenRepository网站,地址如下:http://mvnrepository.com
然后在搜索框中输入第三方类库的关键字,比如说【spring-core】,点击【search】按钮,可以查看到该类库的链接导航。
第二步,点击链接进入到【spring-core】的主页,可以看到所有版本的【spring-core】,选择一个使用率最高的。使用率高在一定程度上表明这个版本的类库最稳定,它已经得到了广大程序员的认可。
第三步,进入该版本的主页,只需要左键轻轻地在[Maven]选项卡内点一下,就已经把该类库的Maven依赖信息复制到粘贴板了(不需要[Ctrl+c],非常人性化),如下图所示:

Here Insert Picture Description
第四步,将类库的依赖信息粘贴到pom.xml文件的<dependencies>节点下,然后按下快捷键[Ctrl+S]保存。紧接着,依次展开test→JavaResources→Libraries→Maven Dependencies节点,你可以看到该类库已经悄悄地添加进来了。
2.<exclusions>主要用于排除依赖
有时候,我们引入的依赖中可能会包含一些不想要的依赖包,我们想引入自己想要的,这时候就要用到排除依赖了。
使用<exclusion>的时候只需要指定groupId和artifactId就行了,并不需要version,这是因为groupId和artifactId就可以定位某种类型的依赖。

3.<scope>用来控制依赖的范围

test:测试依赖范围。典型的例子是 Jnuit,它只有在编译测试代码及运行测试的时候才需要。

compile:编译依赖范围(其实不止是编译,对测试、运行同样有效),缺省项,如果没有指定,就会默认使用该依赖范围。

provided:提供依赖范围。对编译和测试有效,但在运行时候无效。

runtime:运行时依赖范围。对测试和运行有效,但在编译时无效。

PS:如果不知道选哪一种,缺省就对了。
4)构建配置

<build>元素中包含了执行Maven构建周期目标所需的插件以及相关的配置

Here Insert Picture Description
<finalName>,打成 jar 包或者 war 包时的名称。上文中曾提到,项目打包后的输出目录为 ${basedir}/target

<plugins> 用于指定项目在构建过程中使用的插件。

<groupId>、<artifactId>、<version> 用于确定使用的插件。
<configuration>,该插件所需要的特殊配置。Maven 3 默认使用的是 JDK 1.5,本例中我们使用了 JDK 1.8。
<resources> 描述了各个资源在 Maven 项目中的具体路径。

<directory>,资源文件的路径,默认位于 ${basedir}/src/main/resources/ 目录下。
<includes>,用于指定在构建过程中被处理的资源文件;对应 <excludes> 用于省去不被处理的资源文件。

05、使用Maven对项目进行清理、编译、测试、打包
1)清理:mvn clean,该命令会删除目录。可以直接在命令行中执行该命令,只需要切换到项目所在的路径下即可。

2)编译:mvn complie,该命令会编译src/main/java目录下的源码。同时,Maven还会处理在src/main/resources目录下的资源文件,确保它作为编译的一部分。

Here Insert Picture Description

However, it is regrettable that, execute the command error. The command prompt is given, see [Help 1] address given, I tried it, you can mvn compliereplace command for the mvn compiler:compilecommand is executed, the results shown below.

Here Insert Picture Description
You can view the compiled byte code file in the target directory.

3) Testing: mvn test, test command at run-time, will perform compile command; and We have previously performed once compile command, in order to ensure the accuracy of the results, you can perform mvn clean test command to ensure that no residue before testing results are as follows As shown in FIG.
Here Insert Picture DescriptionMaven Surefire plug-in will pass, using the test provider pom.xml file (usually Junit) for testing. Execute test command not only run the tests, but also produce report files, this time under the target directory and the shots are as follows:

Here Insert Picture Description

4) Packaging: mvn installThe command file will follow pom.xml <packaging>embodiment (in this case jar) specified to the compiler result packed. At the same time, but also the packaged files into the local Maven repository, so that other projects use it as a dependency. Command execution results are shown in FIG.

Here Insert Picture Description
Check local Maven repository, you can see just packaged file.

Here Insert Picture Description
06, the last
before the advent of Maven, is a popular build tool Ant; after the advent of Maven, there is a new build tool Gradle, it deliberately chose Maven and opposite principle, does not force the user to follow the rigid construction of the cycle.

In short, Maven is an excellent tool to build, Java developers project it is necessary mastered it.

Released three original articles · won praise 0 · Views 22

Guess you like

Origin blog.csdn.net/weixin_45569634/article/details/104474631