Maven的使用
-
Apache Maven 款软件项目管理的开源工具,是基于工程对象模型(Porn )的概念而
设计的 Maven 可以 理项目构建的整个生命周期,包括清理( Clean )、编译( Compile )、打
包( Package )、测试 Test 环节 同时 Maven 提供了非常丰富的插件,使得构建项目和
理项目变得简单。 构建 个项目所 要的流程如下
(1 )生成源码
(2)从源码中 成文档
(3 )编译源码
(4 )测试
(5 )将源码打包成 Jar ,运行在服务器、仓库或者其他位置
Apache Maven 实现了以上的全部功能,并且只需要相关的命令就可以 成相
功能
-
maven的核心概念
-
Maven 的核心 porn 件, porn 文件以 xm 文件的形式来表示资源,包括 些依赖 Jar
插件、构建文件等。
-
首先读取pom文件,porn 文件是 Maven 的核心,所有的项目依赖、插件都在 pom文件中统一管理
-
下载依赖jar到本地仓库,maven命令执行时,首先会检查 porn 文件的依赖 Jar ,当检测到本地没有安装依赖 Jar 时,会默认从 Maven 中央仓库下载依赖 Jar ,中央仓库 地址为 http://repol.maven.org/maven2 依赖 Jar 载成功后,会存放在本地仓库中,如果下载不成功,则该命令执行不会通过。
-
执行构建的生命周期 Maven 的构建过程会被分解成构建阶段和构建目标,它们共同
构成了 Maven 的生命周期
-
执行构建插件 插件可以更方便地执行构建的各个阶段,也可以用插件实现 些额
外的功能 目前 Mave 有非常丰 的插件,如果需要,你也可以自己实现 Maven
插件
-
-
pom 文件是 xm 文件,用于描述项目用到的资源、项目依赖、插件、代码位置等
息,是整个 程的核心
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.hand.hcf.app</groupId> <artifactId>hcf-app-parent</artifactId> <version>2.6.0-SNAPSHOT</version> <!-- lookup parent from repository --> <relativePath/> </parent> <groupId>com.hand.hcf.app</groupId> <artifactId>cbit-expense</artifactId> <version>2.8.0-SNAPSHOT</version> <name>cbit-expense</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>11</java.version> <maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.target>${java.version}</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>com.hand.hcf.app</groupId> <artifactId>expense</artifactId> <version>2.11.0.RELEASE</version> </dependency> <dependency> <groupId>com.hand.hcf.app</groupId> <artifactId>expense</artifactId> <version>2.11.0.RELEASE</version> <exclusions> <exclusion> <groupId>com.hand.hcf.lcn</groupId> <artifactId>txlcn-tc</artifactId> </exclusion> <exclusion> <groupId>com.hand.hcf.lcn</groupId> <artifactId>txlcn-txmsg-netty</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.hand.hcf.lcn</groupId> <artifactId>txlcn-tc</artifactId> <version>5.1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.dameng</groupId> <artifactId>Dm8JdbcDriver18</artifactId> <version>8.1.1.49</version> </dependency> </dependencies> <build> <finalName>cbit-expense</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>build-info</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <groupId>com.google.cloud.tools</groupId> <artifactId>jib-maven-plugin</artifactId> <version>${jib-maven-plugin.version}</version> <configuration> <from> <image>${base_image}</image> </from> <to> <image>${registry_url}/${project.artifactId}:${image_tag}</image> <auth> <username>${registry_username}</username> <password>${registry_password}</password> </auth> </to> <allowInsecureRegistries>true</allowInsecureRegistries> <container> <jvmFlags> <jvmFlag>-Djava.security.edg=file:/dev/./urandom</jvmFlag> </jvmFlags> <mainClass>com.hand.hcf.app.expense.ServiceApplication</mainClass> </container> <allowInsecureRegistries>true</allowInsecureRegistries> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> </build> <repositories> <repository> <id>polars-ecf-maven</id> <name>maven</name> <url>https://polars-maven.pkg.coding.net/repository/ecf/maven/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> </project>
pom.xml的第一行指定了xml的版本和编码方式。project的标签是该文件的根元素,他声明了pom相关的命名空间。modelVersion指定了pom的版本,对于Maven3来说,他的版本为4.0.0。 groupId、artifactId和version是3个最重要的标签,根据这3个标签,可以再maven仓库中唯一确定该依赖jar。其中,groupId代表了公司、组织的名称,一般为公司域名的倒写,如本例中的com.hand.hcf.app;artifactld 代表该项目的全局唯 Id ,如本例中的cbit-expense;version是指该项目的版本。将项日上传到 Mave 仓库中,有这 个标签才能准确无误地找到该 Jar 包。 parent 标签用于指定父 pom,本案例采用的父 pom 是版本号为 2.11.0.RELEASE的 hcf-app-parent的pom。 propertie 标签用于声明一些常量 例如上述代码中的源码编码为 UTF-8,输出代码也为 UTF-8, Java 版本为 1.11。 ependencies 标签为依赖的根元素 里面可以包含多个 dependency 元素 dependency 里具 体为各个依赖 Jar的3个坐标,即 roupId、artifactld、version,其 version 可以缺省,如果 缺省,就会默认为最新发布的版本。 build 为构建标签,它可以包含 plugins (插件〉标签 ,plugins 标签中可以包含若干个 plugin 标签,可以根据项口的需求添加相应的 plugin ,本例中有 spring-boot-maven-plugin 插件,用此 插件可以启动 pring Boot 工程。
-
常用的maven命令
1> mvn clean ---删除工程 target 下的所有文件 2> mvn package 将工程打为jar包 mvn package命令执行过程包含以下6个阶段。 验证、编译代码、处理代码、生成资源文件、生成jar包、测试 3> mvn package -Dmaven.test.skip=true,打包时跳过测试 4> mvn compile编译工程代码,不生成jar包 5> mvn install命令包含了mvn package的所有过程,并且将生成的jar包安装到本地仓库。执行mvn install命令,可以看到终端输出的日志,经过了与mvn package相同的阶段,最后将jar包安装到本地仓库。 6> mvn spring-boot:run 使用spring-boot插件,启动Sping boot工程。该命令执行时先检查Spring boot工程源码是否编译,如果没有就先编译,否则就直接启动工程。