pom.xml中配置plugin补充

来源:互联网 发布:金盏花洁面啫喱知乎 编辑:程序博客网 时间:2024/05/19 03:20
根据《Maven的Build过程》中所述,配置plugin的时候有一个原则需要遵守:
一个Phase上至多绑定一个Goal。


那如果现在想增加一个新的Goal到项目中,该如何处理呢?
有两种方案:
一、将新的Goal绑定到还未绑定Goal的Phase上
比如default这个Lifecycle中的“validate,initialize,generate-sources,process-sources,generate-resources”等Phase默认没有绑定任何Goal,可以将新的Goal绑定到这些Phase中的任意一个上


二、通过命令行直接调用Goal的形式
比如:mvn dependency:copy-dependencies
就是直接调用"maven-dependency-plugin"这个Plugin提供的"copy-dependencies"这个Goal


采用以上两种方案在pom.xml中的配置有异同点:
1、相同点
1)两种方案都需要在pom.xml中配置提供Goal的Plugin,因为由Plugin提供Goal
2)在配置<plugin>标签时,对于其下的<groupId>,<artifactId>,<version>,<extensions>,<dependencies>,<inherited>,<configuration>子标签的配置两者都一样
2、不相同点
两者的唯一不同之处在于:
前者方案必须要在<plugin>标签下至少配置一个<executions>子标签,并且要在<executions>子标签下至少配置一个<execution>子标签,在该子标签中绑定Phase和Goal的关系

后者方案无需配置<plugin>下的<executions>子标签,配置了也不会产生效果,因为我们通过在命令行直接调用的形式来执行Goal


举例说明,现在想往项目中添加“assembly:single”这个Goal,有两种方案:
1、绑定新Goal到"process-classes"这个Phase
由于default这个Lifecycle中的"process-classes"这个Phase默认未绑定任何Goal,因而可以将新Goal绑定到该Phase上
pom.xml中的配置片段如下:

<plugin>    <artifactId>maven-assembly-plugin</artifactId>    <executions>        <execution>            <id>default-single</id>            <phase>process-classes</phase>            <goals>                <goal>single</goal>            </goals>        </execution>    </executions>    <configuration>        <archive>            <manifest>                <mainClass>fully.qualified.MainClass</mainClass>            </manifest>        </archive>        <descriptorRefs>            <descriptorRef>jar-with-dependencies</descriptorRef>        </descriptorRefs>    </configuration></plugin>
现在想执行新Goal,只需要调用default这个Lifecycle中的"process-classes"或者其后的Phase
比如"mvn clean process-classes"


2、在命令行中直接调用新Goal
pom.xml中的配置片段如下:

<plugin>    <artifactId>maven-assembly-plugin</artifactId>    <configuration>        <archive>            <manifest>                <mainClass>fully.qualified.MainClass</mainClass>            </manifest>        </archive>        <descriptorRefs>            <descriptorRef>jar-with-dependencies</descriptorRef>        </descriptorRefs>    </configuration></plugin>
现在想执行新Goal,直接在命令行中调用
比如"mvn clean compile assembly:single"


0 0
原创粉丝点击