Maven项目的生命周期

参考文章 :https://stackoverflow.com/questions/16602017/how-are-mvn-clean-package-and-mvn-clean-install-different

These are the default life cycle phases in maven

  • validate - validate the project is correct and all necessary information is available
  • compile - compile the source code of the project
  • test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package - take the compiled code and package it in its distributable format, such as a JAR.
  • verify - run any checks on results of integration tests to ensure quality criteria are met
  • install - install the package into the local repository, for use as a dependency in other projects locally
  • deploy - done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.

一个maven项目完整的生命周期路径

validate >> compile >> test (optional) >> package >> verify >> install >> deploy

maven package的生命周期路径

validate >> compile >> test (optional) >> package

maven install的生命周期路径

validate >> compile >> test (optional) >> package >> verify >> install

maven clean

移除之前版本编译的文件,也就是target目录下的所有文件。

Maven: Failed to read artifact descriptor

项目中遇到一个问题,子项目使用mvn compile的时候出现Failed to read artifact descriptor,使用-e参数查看详细日志,看到是父项目没有安装。切换到父项目目录,使用mvn clean install即可。

maven的打包类型

参考文章:https://www.baeldung.com/maven-packaging-types

最近在处理项目的时候遇到了两个问题,一个是在执行main方法的时候找不到resource文件,经过查看编译的target的目录中也没有包含resource相关文件,后经排查是把项目的打包类型声明成了pom,而pom主要用来聚合项目的依赖,资源文件并不会编译打包(参考:https://intellij-support.jetbrains.com/hc/en-us/community/posts/360003420219-Why-can-not-build-resources-),后来把项目改成jar即可。第二个问题是把一个项目的api拆分出来的时候(把相关的服务接口通过feign的方式整合到api项目中,这样其他服务只需要引入api这个包即可,不需要引入其实现服务)把项目错误的声明成了pom,导致把api服务集成到其他服务的时候报错。两个问题都是没有正确的理解pom和jar之间的区别。

  • pom是最简单的一种打包方式,主要用来做依赖聚合,父项目提供依赖传递。一个父项目允许你在不同的pom项目之间定义继承关系。因为它没有resource需要处理也不需要编译代码,所以它不会生成任何可执行文件。它的声明周期只有install->deploy
  • jar是最流行的打包方式,也是默认的打包方式,它在打包的时候会执行完整的声明周期,包含java code, resource, metadata files
  • 其他打包方式类似jar,比如war...

你可能感兴趣的:(Maven项目的生命周期)