maven插件配置

 

自定义绑定

除了默认绑定的一些操作,我们也可以将一些阶段绑定到指定的插件目标上来完成一些操作,这种自定义绑定让maven项目在构件的过程中可以执行更多更丰富的操作;

mvn source:help

比如验证阶段打包源码:

<build>

    <plugins>

        <plugin>

            <groupId>org.apache.maven.plugins</groupId>

            <artifactId>maven-source-plugin</artifactId>

            <version>3.2.0</version>

            <executions>

                <!-- 使用插件需要执行的任务 -->

                <execution>

                    <!-- 任务id唯一,如果不指定,默认为default -->

                    <id>attach-source</id>

                    <!-- 任务中插件的目标,可以指定多个 -->

                    <goals>

                        <goal>jar-no-fork</goal>

                    </goals>

                    <!-- 绑定的阶段 -->

                    <phase>verify</phase>

                </execution>

            </executions>

        </plugin>

    </plugins>

</build>

比如验证阶段进行清理:

<plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-clean-plugin</artifactId>

    <version>2.5</version>

    <executions>

        <!-- 使用插件需要执行的任务 -->

        <execution>

            <!-- 任务中插件的目标,可以指定多个 -->

            <id>clean-target</id>

            <goals>

                <goal>clean</goal>

            </goals>

            <!-- 绑定的阶段 -->

            <phase>validate</phase>

        </execution>

    </executions>

</plugin>

POM.xml插件配置

<build>

    <plugins>

        <plugin>

            <groupId>org.apache.maven.plugins</groupId>

            <artifactId>maven-surefire-plugin</artifactId>

            <version>2.12.4</version>

            <!-- 插件参数配置,对插件中所有的目标起效 -->

            <configuration>

                <skip>true</skip>

            </configuration>

        </plugin>

    </plugins>

</build>

插件的目标 == 相当于一个方法

插件目标的参数 == 相当于一个方法的参数

 

跳过测试还可以:

mvn -Dmaven.test.skip=tue

mvn -Dskip=true

properties中配置: <maven.test.skip>true</maven.test.skip>

 

例子二:

<build>

    <plugins>

        <plugin>

            <groupId>org.apache.maven.plugins</groupId>

            <artifactId>maven-surefire-plugin</artifactId>

            <version>2.12.4</version>

            <executions>

                <execution>

                    <goals>

                        <goal>test</goal>

                        <goal>help</goal>

                    </goals>

                    <phase>pre-clean</phase>

                    <!-- 这个地方配置只对当前任务有效 -->

                    <configuration>

                        <skip>true</skip>

                    </configuration>

                </execution>

            </executions>

        </plugin>

    </plugins>

</build>

如上自定义了一个绑定,在clean周期的pre-clean阶段绑定了插件maven-surefire-plugin的两个目标test和help,execution元素没有指定id,所以默认id是default;

 

插件仓库

 

与其他maven构件一样,插件构件也是基于坐标存储在maven仓库中,有需要的时候,maven会从本地查找插件,如果不存在,则到远程仓库查找,找到了以后下载到本地仓库,然后使用;

 

pom.xml中可以配置依赖的构件的仓库地址:

 

<repositories>
    <repository>
        <id>Custom Central Repository</id>
        <url>https://repo.maven.apache.org/maven2</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

 

 

 

pom.xml中也可以配置插件的仓库地址:

 

 

 

<pluginRepositories>

 

    <pluginRepository>

 

        <id>myplugin-repository</id>

 

        <url>http://repo1.maven.org/maven2/</url>

 

        <releases>

 

            <enabled>true</enabled>

 

        </releases>

 

    </pluginRepository>

 

</pluginRepositories>

 

 

 

pom.xml中配置插件的时候,如果是官方的插件,可以省略groupId;

 

默认为:

 

<groupId>org.apache.maven.plugins</groupId>

 

 

 

建议写上groupId,比较清晰;

 

posted on 2023-02-02 14:51  companion  阅读(580)  评论(0编辑  收藏  举报