Home > OS >  Defining plugin goal at phase
Defining plugin goal at phase

Time:11-15

I am trying to define a plugin goal so that it can automatically be executed at a particular phase; say for example the test phase. But I am unable to achieve that. For example I coded the following pom.xml. Notice in my XML <execution> element, I have a <phase> sub-element which is "test". I can execute my goal using the command "mvn antrun:run@ant-echo"; but "mvn test" does not execute that goal. I am using Maven version 3.8.3 and my JDK version is 16.

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>rjava.mvn</groupId>
  <artifactId>app2</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>app2</name>
  <url>http://www.example.com</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.var1>value1</project.var1>
  </properties>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>3.0.0</version>
          <executions>
            <execution>
              <!-- 
                The command to maven execute this ant target is 
                mvn antrun:run@ant-echo
              -->
              <id>ant-echo</id>
              <phase>test</phase>
              <configuration>
                <target name="app2">
                  <echo level="info">This is how you echo information into the console.</echo>
                  <echo level="info">Echo levels defined in Ant are the following </echo>
                  <echo level="info">error, warning, info, verbose, debug</echo>
                  <echo level="info"></echo>
                  <echo level="info">The following is the way to echo property values.</echo>
                  <echo level="info">${project.var1}</echo>
                </target>
              </configuration>
              <goals>
                <goal>run</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

</project>

CodePudding user response:

In order to have your plugin executed during the configured lifecycle phase, you have to specify the plugin goal binding and configuration within the <plugin> section:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>rjava.mvn</groupId>
  <artifactId>app2</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>app2</name>
  <url>http://www.example.com</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.var1>value1</project.var1>
  </properties>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>3.0.0</version>
          <executions>
            <execution>
              <!-- 
                The command to maven execute this ant target is 
                mvn antrun:run@ant-echo
              -->
              <id>ant-echo</id>
              <phase>test</phase>
              <configuration>
                <target name="app2">
                  <echo level="info">This is how you echo information into the console.</echo>
                  <echo level="info">Echo levels defined in Ant are the following </echo>
                  <echo level="info">error, warning, info, verbose, debug</echo>
                  <echo level="info"></echo>
                  <echo level="info">The following is the way to echo property values.</echo>
                  <echo level="info">${project.var1}</echo>
                </target>
              </configuration>
              <goals>
                <goal>run</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
  </build>

</project>
  • Related