Maven - 人生若只如初见(四)
细心是最珍贵的品质。
先看一段命令的输出
sunyelw@windows:eureka_consume$
sunyelw@windows:eureka_consume$ mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building eureka_consume V1.5.7
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ eureka_consume ---
[INFO] Deleting C:\idea\workspace\cloud\eureka_consume\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.132 s
[INFO] Finished at: 2020-04-12T11:12:49+08:00
[INFO] Final Memory: 14M/155M
[INFO] ------------------------------------------------------------------------
sunyelw@windows:eureka_consume$
这段输出中就看一行
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ eureka_consume ---
前面的maven-clean-plugin:2.6.1:clean
是插件及具体执行目标,最后的eureka_consume
是服务名,请问中间的default-clean
是什么?
我们知道maven的生命周期有三个, clean,default,site。maven只是制定了一套代码处理流程,并不负责具体实现,具体实现交由插件的目标来完成,这点就不再赘述了(可以看下之前的maven系列文件)
而mvn
命令有两种执行方式
- 基于phase,比如 clean、package、test等
- 基于goal,需要指定插件版本与具体目标,举例
sunyelw@windows:eureka_consume$ mvn org.apache.maven.plugins:maven-clean-plugin:2.6.1:clean
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building eureka_consume V1.5.7
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-cli) @ eureka_consume ---
[INFO] Deleting C:\idea\workspace\cloud\eureka_consume\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.444 s
[INFO] Finished at: 2020-04-12T11:29:25+08:00
[INFO] Final Memory: 11M/155M
[INFO] ------------------------------------------------------------------------
sunyelw@windows:eureka_consume$
很明显,我们执行的是基于phase(阶段),然后clean生命周期的clean 阶段绑定了 clean插件的clean目标
那么回到开头的那个问题,default-clean
是不是表示default生命周期?
如果你没有自己定义过任何插件,那么你看到的执行目标都是以 default 开头的,这里的default并不是指default生命周期,而是mave官方默认绑定的。
我们加个我们自己定义的插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.sun.yelw</groupId>
<artifactId>hy-maven-plugin</artifactId>
<version>3.5.1</version>
<executions>
<execution>
<id>diy-1</id>
<phase>pre-clean</phase>
<goals>
<goal>hy-an</goal>
</goals>
</execution>
<execution>
<id>diy-2</id>
<phase>post-clean</phase>
<goals>
<goal>hy-doc</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
- 当有多个execution时,必须指定execution-id,这里分为diy-1和diy-2
- 插件 hy-maven-plugin 中有两个目标 hy-an、hy-doc,分别表示自定义插件的两种方式:@Mojo和文件注释
- 分别绑定到 pre-clean 和 post-clean
接下来再执行一下,这里为了执行到post-clean就直接执行post-clean了
sunyelw@windows:eureka_consume$ mvn post-clean
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building eureka_consume V1.5.7
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- hy-maven-plugin:3.5.1:hy-an (diy-1) @ eureka_consume ---
[ERROR] ++++++++++++++++++++
[ERROR] hy-plugin-execute-by-annotation
[ERROR] ++++++++++++++++++++
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ eureka_consume ---
[INFO] Deleting C:\idea\workspace\cloud\eureka_consume\target
[INFO]
[INFO] --- hy-maven-plugin:3.5.1:hy-doc (diy-2) @ eureka_consume ---
[ERROR] ++++++++++++++++++++
[ERROR] hy-plugin-execute-by-document
[ERROR] ++++++++++++++++++++
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.036 s
[INFO] Finished at: 2020-04-12T11:37:50+08:00
[INFO] Final Memory: 14M/155M
[INFO] ------------------------------------------------------------------------
sunyelw@windows:eureka_consume$
执行顺序
- diy-1
- default-clean
- diy-2
所以这个 default-clean 是 execution-id,那么在哪定义的呢?
我们先看下 clean 插件的clean目标 CleanMojo.java代码地址
@Mojo( name = "clean", threadSafe = true )
public class CleanMojo
extends AbstractMojo
{
...
}
这里我们只看到了一个clean名称,并没有看到 default-clean 字段。
其实上面我们发现这个 execution-id 是定义在添加插件里面的,而这个是maven官方添加的,当然需要去看下其源码实现 maven源码
拉下来
git clone --depth 1 git@github.com:apache/maven.git
找下有没有 default 或 default- 开头的代码
sunyelw@windows:maven$ grep 'default-' * -Rn > default_.md
sunyelw@windows:maven$ grep 'goal' default_.md
maven-compat/src/test/java/org/apache/maven/project/EmptyLifecycleExecutor.java:93: pluginExecution.setId( "default-" + goal );
maven-compat/src/test/java/org/apache/maven/project/EmptyLifecyclePluginAnalyzer.java:70: pluginExecution.setId( "default-" + goal );
maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecyclePluginAnalyzer.java:256: String base = "default-" + goal;
maven-core/src/test/java/org/apache/maven/lifecycle/EmptyLifecyclePluginAnalyzer.java:69: pluginExecution.setId( "default-" + goal );
maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/LifeCyclePluginAnalyzerStub.java:66: pluginExecution.setId( "default-" + goal );
maven-core/src/test/java/org/apache/maven/project/EmptyLifecycleExecutor.java:92: pluginExecution.setId( "default-" + goal );
sunyelw@windows:maven$
然后在输出中,排除test目录下的,还真找到一行
maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecyclePluginAnalyzer.java:256: String base = "default-" + goal;
这就有点激动了,打开这个文件看看
DefaultLifecyclePluginAnalyzer-1
继续看下这个方法调用
DefaultLifecyclePluginAnalyzer-2
所以execution-id是在这里塞进去的,default就是其字面意思,总算弄明白了。
这篇文章就跟到这里了,后面有需要再跟下这个源码
又带着复习了一遍maven,感觉一上午啥也没做。。