构建的生命周期
validate 校验:校验项目是否正确并且所有必须的信息可以完成项目的构建过程。 initialize 初始化:初始化构建状态,比如设置属性值。 generate-sources 生成源代码:生成包含在编译阶段中的任何源代码。 process-sources 处理源代码:如过滤任意值。 generate-resources 生成资源文件:生成将会包含在项目包中的资源文件。 process-resources 处理资源文件:复制和处理资源到目标目录,为打包阶段做好准备。 compile 编译:编译项目的源代码。 process-classes 处理类文件:处理编译生成的文件,比如:对Java的class文件做字节码改善优化。 generate-test-sources 生成测试源代码:生成包括在编译阶段中的任何测试源代码。 process-test-sources 生成测试源代码:处理测试源代码,比如:过滤任意值。 generate-test-resources 生成测试资源文件:为测试创建资源文件。 process-test-resources 处理测试资源文件:复制和处理测试资源到目标目录。 test-compile 编译测试源代码:编译测试源代码到测试目标目录。 process-test-classes 处理测试类文件:处理测试源代码编译生成的文件。 test 测试:使用合适的单元测试框架运行测试,这些测试应该不需要代码被打包或发布。 prepare-package 准备打包:在实际打包之前,执行任何的必要的操作为打包做准备。 package 打包:将接受编译后的代码,打包成可发布的格式,如 JAR。 pre-integration-test 集成测试前:在执行集成测试前进行必要的动作。比如:搭建需要的环境。 integration-test 集成测试:处理和部署项目到可以运行集成测试环境中。 post-integration-test 集成测试后:在执行集成测试完成后进行必要的动作。比如:清理集成测试环境。 verify 验证:运行任意的检测来验证项目包有效且达到质量标准。 install 安装:安装项目包到本地仓库,这样项目包可以用作其它本地项目的依赖。 deploy 部署:将最终的包复制到远程仓库,供其他开发者和项目使用。
安装
待续...
配置
配置本地仓库地址
在maven项目的..\apache-maven-3.8.6\conf下(C:\Users\namejr\apache-maven-3.8.6\conf)的settings.xml文件中配置
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
就是配置本地仓库的,如:在C:\Users\namejr\apache-maven-3.8.6目录下新建repository文件夹来做仓库,将上述的localRepository注释的放出来并填充对应文件即可
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>C:/Users/namejr/apache-maven-3.8.6/repository</localRepository>
<mirrors>
<!-- 配置具体的仓库下的下载镜像 -->
<mirror>
<!-- 此镜像的唯一标识符,用来区分不同的mirror元素 -->
<id>mirrorId</id>
<!-- 对那种仓库进行镜像,简单说就是替代那个仓库 -->
<mirrorOf>repositoryId</mirrorOf>
<!-- 镜像名称 -->
<name>Human Readable Name for this Mirror.</name>
<!-- 镜像URL -->
<url>http://my.repository.com/repo/path</url>
</mirror>
</mirrors>
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"> <!-- 指定pom的模型版本 --> <modelVersion>4.0.0</modelVersion> <!-- 组id --> <groupId>com.lancoo</groupId> <!-- 项目id --> <artifactId>FirstApp</artifactId> <!-- 版本号:SNAPSHOT一般用来表示测试版本;Release一般用来表示正式版本 --> <version>1.0-SNAPSHOT</version> <!-- 打包方式 --> <packaging>jar</packaging> <!-- 项目名称 --> <name>FirstApp</name>
<!-- 属性设置 --> <properties>
<!-- 具体属性配置,用法:${定义名称} --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<定义名称>内容</定义名称> </properties> <!-- 所有项目依赖 --> <dependencies> <!-- 具体依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <!-- scope 依赖范围: compile:默认值,可省略不写。此值表示该依赖需要参与到项目的编译、测试以及运行周期中,打包时也要包含进去。 test:该依赖仅仅参与测试相关的工作,包括测试代码的编译和执行,不会被打包,例如:junit。 runtime:该依赖项目无需参与项目的编译,不过后期的测试和运行周期需要其参与。与compile相比,跳过了编译而已。例如JDBC驱动,适用运行和测试阶段。 provided:该依赖在打包的时候可以不用包含进去,别的设施会提供。事实上该依赖理论上可以参与编译,测试,运行等周期。相当于compile,但是打包阶段做了exclude(排除)操作。 system:从参与度来说,和provided相同,不过被依赖项不会从maven仓库下载,而是从本地文件系统拿。需要添加systemPath的属性来定义路径 --> <scope>test</scope> <!-- 可选依赖:对外隐藏当前所依赖的资源(不透明)--> <optional>true</optional> <!-- 排除依赖:主动断开(依赖中内部存在的)依赖资源,被排除的资源无需指定版本--> <exclusions> <!-- 指定具体依赖项 --> <exclusion> <groupId>junit</groupId> <artifactId>junit</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <!-- 构建 --> <build> <!-- 引用插件列表 --> <plugins> <!-- 具体插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <webResources> <resource> <directory>${project.basedir}/libs</directory> <targetPath>WEB-INF/lib</targetPath> <includes> <include>**/*.jar</include> </includes> </resource> </webResources> </configuration> </plugin> </plugins> </build> </project>
运行
创建工程,创建命令
mvn archetype:generate —DgroupId=组名 —DartifactId=项目名称 —DarchetypeArtifactld=maven—archetype—quickstart —DinteractiveMode=false
创建java工程
mvn archetype:generate —DgroupId=组名 —Dartifactld=项目名称 —Dversion=版本号 DarchetypeArtifactld=maven—archetype—quickstart DinteractiveMode=false
创建web工程
mvn archetype:generate —DgroupId=组名 —Dartifactld=项目名称 —Dversion=版本号 DarchetypeArtifactId=maven—archetype—webapp DinteractiveMode=fa1se
mvn compile #编译
1、执行后会下载pom.xml里面需要的包;2、编译对应的jar包,并放到对应目录。
mvn clean #清理
//
mvn test #测试
//
mvn package #打包
//
mvn install #安装到本地仓库
//
错误记录
在打本地包打包过程中,
[INFO] ------------------------------------------------------------- [ERROR] ***/service/LoggerService.java:[5,18] 找不到符号 符号: 类 LogPlugIn 位置: 程序包 com.lancoo [ERROR] **/service/LoggerService.java:[8,23] 程序包com.lancoo.bean不存在
需要补充本地jar的依赖和打包配置
<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> ...
... <dependencies> ...
...
<!-- 添加本地jar包的依赖 --> <dependency> <groupId>LogPlugIn</groupId> <artifactId>LogPlugIn</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${project.basedir}/libs/LogPlugIn.jar</systemPath> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.yml</include> <include>**/*.xml</include> <include>**/*.json</include> <include>**/*.exe</include> </includes> <filtering>false</filtering> </resource> <resource> <!-- 注册webapp目录为资源目录 --> <directory>src/main/webapp</directory> <targetPath>META-INF/resources</targetPath> <includes> <include>**/**</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <includeSystemScope>true</includeSystemScope> <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments> </configuration> </plugin> <plugin> <!-- 配置jar包打包工具 --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <webResources> <resource> <directory>${project.basedir}/libs</directory> <targetPath>WEB-INF/lib</targetPath> <includes> <include>**/*.jar</include> </includes> </resource> </webResources> </configuration> </plugin> </plugins> </build> </project>
待续...