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.


    生命周期阶段与插件的绑定关系.png

一个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常用运维指令

参考文章:https://www.cnblogs.com/hiver/p/7850954.htmlhttps://blog.51cto.com/u_330478/3625582
反应堆(Reactor)是指所有模块组成的一个构建结构

  • am(--also-make):安装其需要的依赖
  • amd(-also-make-dependents):同时构建依赖与所列模块的模块
  • pl(--projects):构建指定的模块,模块间用逗号分隔
  • rf(-resume-from):从指定的模块构建反应堆
    比如现在有个项目envctl-exporter依赖通用模块common-db,父模块为demo
# 输出调试日志
mvn install -e -X
# 语法格式
mvn clean package -Dmaven.test.skip=true -pl ${group_id}:${task_name} -am
# demo,common-db,envctl-exporter都会构建
mvn clean package -Dmaven.test.skip=true -pl envctl-exporter -am
# common-db,envctl-exporter都会构建
mvn clean package -Dmaven.test.skip=true -pl common-db -amd
# 只会构建envctl-exporter
mvn clean package -Dmaven.test.skip=true -pl common-db -amd -rf envctl-exporter

Maven fails to find local artifact

1c4c67f6335cc57192b53ab81b37437.png

Maven插件

  • Maven插件帮助指令
# 输出插件maven-source-plugin的帮助文档
mvn help:describe -Dplugin=org.apache.maven.plugins:maven-source-plugin:2.1.1 -Ddetail
# 输出插件maven-help-plugin的帮助文档
mvn help:describe -Dplugin=org.apache.maven.plugins:maven-help-plugin
  • 配置插件执行目标,创建项目的源码jar包
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.1.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>verify</phase>
                        <goals>
                            <!--配置插件执行目标,创建项目的源码jar包-->
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

指定jar编译版本

          <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>

spring boot maven插件
通过maven启动spring boot项目:mvn spring-boot:run -Dapp.profiles=test

<!--https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/htmlsingle/-->
           <plugin>
              <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

Maven私服

# 使用docker安装
# 创建宿主机数据目录
mkdir /usr/local/nexus/data
# 赋予数据目录全部权限
chmod 777 /usr/local/nexus/data
# docker安装
docker run -d --name nexus3 -p 8081:8081 --restart always -v /usr/local/nexus/data:/nexus-data sonatype/nexus3

maven仓库类型

  • group:多个仓库的集合,比如maven-public集成了maven-releases,maven-snapshots,maven-central;通常在配置镜像时指定该私服链接;
      <mirror>
            <id>nexus</id>
            <mirrorOf>*</mirrorOf>
            <name>双胞胎Nexus私服</name>
            <url>http://172.19.7.199:8081/repository/maven-public/</url>
        </mirror>
  • proxy:代理仓库,依赖请求转发到代理服务器下载,比如把中央仓库代理到阿里云


    中央仓库代理.png
  • hosted: 宿主仓库,也就是本地仓库,存储本地依赖(包含本地配置推送的和第三方库),比如:maven-releases(发布版本),maven-snapshots(快照版本)
<!--分发到私服配置,release版本分发到maven-releases仓库;snapshot版本分发到maven-snapshots仓库-->
    <distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://172.19.7.199:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshots Repository</name>
            <url>http://172.19.7.199:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容