其他开发工具

Maven生命周期和插件

2020-12-07  本文已影响0人  xiaogp

摘要:Maven生命周期插件
Maven的生命周期是抽象的,其实际都是由插件完成的,生命周期和插件两者密不可分

三套生命周期

Maven有三套生命周期,分别是cleandefaultsite,clean的目的是清理项目,default的目的是构建项目,site的目的是建立项目站点。每个生命周期包含一些阶段,后面的阶段依赖于前面的阶段,三套生命周期是互相独立的,调用某个生命周期的阶段不会对其他生命周期产生任何影响。

clean阶段

clean生命周期的目的是清理项目,包含三个阶段
(1)pre-clean:执行一些清理前需要完成的工作
(2)clean:清理上一次构建生成的文件
(3)post-clean:执行一些清理后需要完成的工作

default生命周期

default是核心,是构建项目时需要用到的生命周期,它包含主要以下几个阶段
(1)validate
(2)initialize
(3)process-sources:将项目主资源文件src/main/resources复制到项目输出的主classpath目录中
(4)compile:编译项目的主源码,编译src/main/java下的java文件到输出的主classpath中
(5)process-test-sources:处理项目测试资源文件。将src/test/resources下的内容复制到测试classpath中
(6)test-compile:编译代码的测试代码,编译src/test/java下的java文件到输出的测试classpath中
(7)test:使用单元测试进行代码测试,测试代码不会被打包或部署
(8)package:接受编译好的代码,输出成可发布的格式如jar
(9)install:将包安装到本地仓库,提供其他Maven项目使用
(10)deploy:将包复制到远程仓库

site生命周期

site生命周期的目的是建立和发布项目站点
(1)pre-site:执行一些在生成项目站点之前需要完成的工作
(2)site:生成项目站点文档
(3)post-site:执行一些在生成项目站点之后需要完成的工作
(4)site-deploy:将生成的项目站点发布到服务器上

命令行与生命周期

从命令行执行Maven命令的主要任务就是调用Maven的生命周期,比如

root@ubuntu:~/myproject/InstallTest# mvn clean install
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< org.mycom.example:InstallTest >--------------------
[INFO] Building My First APP InstallTest 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ InstallTest ---
[INFO] Deleting /home/myproject/InstallTest/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ InstallTest ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ InstallTest ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/myproject/InstallTest/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ InstallTest ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/myproject/InstallTest/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.2:testCompile (default-testCompile) @ InstallTest ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/myproject/InstallTest/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ InstallTest ---
[INFO] Surefire report directory: /home/myproject/InstallTest/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running MainTest
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ InstallTest ---
[INFO] Building jar: /home/myproject/InstallTest/target/InstallTest-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-shade-plugin:2.3:shade (default) @ InstallTest ---
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing /home/myproject/InstallTest/target/InstallTest-1.0-SNAPSHOT.jar with /home/myproject/InstallTest/target/InstallTest-1.0-SNAPSHOT-shaded.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ InstallTest ---
[INFO] Installing /home/myproject/InstallTest/target/InstallTest-1.0-SNAPSHOT.jar to /home/.m2/repository/org/mycom/example/InstallTest/1.0-SNAPSHOT/InstallTest-1.0-SNAPSHOT.jar
[INFO] Installing /home/myproject/InstallTest/pom.xml to /home/.m2/repository/org/mycom/example/InstallTest/1.0-SNAPSHOT/InstallTest-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.852 s
[INFO] Finished at: 2020-12-07T16:02:30+08:00
[INFO] ------------------------------------------------------------------------

一些常用的插件

maven-compiler-plugin对 Java 代码编译,maven-compiler-plugin 默认的 JDK 版本为 1.5需要修改configuration下的sourcetarget

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.2</version>
        <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
        </configuration>
</plugin>

maven-scala-plugin对 Scala 代码编译,其中每个execution可以执行一个配置,id是随便起的也可以不指定,phase是需要绑定到的生命周期阶段,goal是需要绑定的插件

<plugin>
                                <groupId>org.scala-tools</groupId>
                                <artifactId>maven-scala-plugin</artifactId>
                                <executions>
                                        <execution>
                                                <id>scala-compile-first</id>
                                                <phase>process-resources</phase>
                                                <goals>
                                                        <goal>add-source</goal>
                                                        <goal>compile</goal>
                                                </goals>
                                        </execution>
                                        <execution>
                                                <id>scala-test-compile</id>
                                                <phase>process-test-resources</phase>
                                                <goals>
                                                        <goal>testCompile</goal>
                                                </goals>
                                        </execution>
                                </executions>
                                <configuration>
                                        <scalaVersion>${scala.version}</scalaVersion>
                                        <args>
                                                <arg>-target:jvm-1.5</arg>
                                        </args>
                                </configuration>
                        </plugin>

maven-shade-plugin将依赖的jar包打包到当前jar包,该插件只有一个目标shade,将他绑定到package阶段,该插件会生成2个jar包,original开头的只包含工程的class文件,另一个是可执行jar包,包含class文件和依赖

<plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-shade-plugin</artifactId>
                                <configuration>
                                        <artifactSet>
                                                <includes>
                                                        <include>*:*</include>
                                                </includes>
                                        </artifactSet>
                                </configuration>
                                <executions>
                                        <execution>
                                                <phase>package</phase>
                                                <goals>
                                                        <goal>shade</goal>
                                                </goals>
                                                <configuration>
                                                        <finalName>${project.artifactId}-${project.version}</finalName>
                                                        <filters>
                                                                <filter>
                                                                        <artifact>*:*</artifact>
                                                                        <excludes>
                                                                                <exclude>META-INF/*.SF</exclude>
                                                                                <exclude>META-INF/*.DSA</exclude>
                                                                                <exclude>META-INF/*.RSA</exclude>
                                                                        </excludes>
                                                                </filter>
                                                        </filters>
                                                </configuration>
                                        </execution>
                                </executions>
                        </plugin>

可以在maven-shade-plugin插件中指定main方法

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.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.ManifestResourceTransformer">
                                    <!--可以指定mian方法 -->
                                    <mainClass>com.myproject.Main</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
上一篇下一篇

猜你喜欢

热点阅读