Maven生命周期

  生命周期(lifecycle)可以理解成由各种plugin按照一定的顺序执行来完成java项目清理、编译、打包、测试、布署等整个项目的流程的一个过程。
  生命周期(lifecycle)由各个阶段组成,每个阶段由maven的插件plugin来执行完成。生命周期(lifecycle)主要包括clean、resources、complie、package、testResources、testCompile、install、等,其中带test开头的都是用业编译测试代码或运行单元测试用例的。
  mvn clean package依次执行了clean、resources、compile、testResources、testCompile、test、jar(打包)等7个阶段。
  mvn clean install依次执行了clean、resources、compile、testResources、testCompile、test、jar(打包)、install等8个阶段。
  mvn clean deploy依次执行了clean、resources、compile、testResources、testCompile、test、jar(打包)、install、deploy等9个阶段。
  package命令完成了项目编译、单元测试、打包功能,但没有把打好的可执行jar包(war包或其它形式的包)布署到本地maven仓库和远程maven私服仓库
  install命令完成了项目编译、单元测试、打包功能,同时把打好的可执行jar包(war包或其它形式的包)布署到本地maven仓库,但没有布署到远程maven私服仓库
  deploy命令完成了项目编译、单元测试、打包功能,同时把打好的可执行jar包(war包或其它形式的包)布署到本地maven仓库和远程maven私服仓库

mvn clean

  maven是具有内置clean插件的,clean的功能就是清除工程目录下的target目录,对应的插件是 maven-clean-plugin:2.5,2.5是版本号,可以使用maven内置的插件,当然也可以自己在pom中配置
在这里插入图片描述
compile插件maven-compiler-plugin
  compile插件执行时先调用resouces插件,功能就是把src\main\java源码编译成字节码生成class文件,并把编译好的class文件输出到target\classes目录下。
在这里插入图片描述
  插件maven-surefire-plugin:2.12.4是执行单元测试类的,如果单测试不通行,构建会失败,在编译正式的项目时可以使用mvn -Dmaven.test.skip=true 来跳过测试类的编译和运行过程。mvn test可以单独执行,但是这个命令其实是包括了resources、compile、testResources、testCompile、test这几个阶段
在这里插入图片描述
  这个插件是把class文件、配置文件打成一个jar(war或其它格式)包。依赖包是不在jar里面的,需要建立lib目录,且jar和lib目录在同级目录。常用的打包插件有maven-jar-plugin、maven-assembly-plugin、maven-shade-plugin三种

maven-jar-plugin
  可执行jar与依赖包是分开,需建立lib目录里来存放需要的j依赖包,且需要jar和lib目录在同级目录

             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-jar-plugin</artifactId>
                 <version>2.6</version>
                 <configuration>
                    <archive>
                         <manifest>
                             <addClasspath>true</addClasspath>
                             <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.xxx.xxxService</mainClass>
                       </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>


maven-assembly-plugin
  这个插件可以把所有的依赖包打入到可执行jar包。但是该插件有个bug会缺失spring的xds文件,导致无法运行jar,同时如果同级目录还有其它可执行jar文件依赖可能会产生冲突。

             <plugin>
                 <artifactId>maven-assembly-plugin</artifactId>
                 <configuration>
                     <descriptorRefs>
                         <descriptorRef>jar-with-dependencies</descriptorRef>
                     </descriptorRefs>
                     <archive>
                         <manifest>
                             <mainClass>com.xxx.xxxService</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

maven-shade-plugin
  所有的依赖包打入到可执行jar包,如果同级目录有其它可执行jar,依赖可能会产生冲突,且运行jar时,有时会出现SF、DSA、RSA文件冲突的提示,需要排除META-INF目录下的文件。

    <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.handlers</resource>
                                </transformer>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.schemas</resource>
                                </transformer>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.tooling</resource>
                                </transformer>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.xxx.xxxInvoke</mainClass>
                                </transformer>
                            </transformers>
                            <minimizeJar>true</minimizeJar>
                            <shadedArtifactAttached>true</shadedArtifactAttached>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

发布插件maven-install-plugin
  发布插件的功能就是把构建好的artifact部署到本地仓库,还有一个deploy插件是将构建好的artifact部署到远程仓库

mvn install

在这里插入图片描述

mvn deploy:deploy-file -DgroupId=com.yak -DartifactId=comp-modbus -Dversion=0.0.1 -Dpackaging=jar -Dfile=本地jar路径 -DpomFile=本地pom路径 Durl=远程仓库url -DrepositoryId=仓库id
Maven生命周期定义了一系列的阶段(phases),每个阶段包含了一组插件目标(goals)。在Maven构建过程中,一般会按照生命周期的顺序执行这些阶段和目标。 Maven生命周期分为三个主要的生命周期: 1. Clean生命周期:该生命周期与项目的清理相关,用于清除先前构建生成的文件。 - clean:清理项目,删除生成的目录和文件。 2. Default生命周期:该生命周期是项目构建的核心,负责编译、测试、打包、部署等操作。 - validate:验证项目是否正确且所有必要信息都可用。 - compile:编译项目的源代码。 - test:使用合适的单元测试框架运行测试。 - package:将编译后的代码打包成可发布的格式,如JAR、WAR。 - verify:对集成测试的结果进行检查,以保证质量达标。 - install:将包安装到本地仓库,供其他项目使用。 - deploy:将最终的包复制到远程仓库,供其他开发人员和项目使用。 3. Site生命周期:该生命周期用于生成项目的站点文档。 - site:生成项目站点的文档。 - site-deploy:将生成的站点发布到服务器上。 需要注意的是,Maven生命周期中的每个阶段和目标都有其预定的执行顺序,且每个阶段都会执行其前面的所有阶段。你可以通过在命令行中执行`mvn <phase>`来运行指定的生命周期阶段。 需要注意的是,Maven生命周期是可扩展的,可以通过自定义插件来添加额外的阶段和目标。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值