Maven 插件默认阶段

Dor*_*hai 5 maven-plugin maven

我正在尝试编写一个 Hello World Maven 插件。

我分配defaultPhase=LifecyclePhase.CLEAN给它,但当我执行时mvn clean它不起作用。

当我执行时mvn gorov:clean它正在工作。

有什么建议么?

DS

代码是:

项目中的位置:maven-plugin\src\main\java\com\gorovdude\plugins\maven\mojos\WriteConsoleMojo.java

package com.gorovdude.plugins.maven.mojos;

import java.io.File;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;

@Mojo(name = "clean", defaultPhase = LifecyclePhase.CLEAN, threadSafe = true, aggregator = true)

public class WriteConsoleMojo extends AbstractMojo {

    public void execute() throws MojoExecutionException, MojoFailureException {
        try {

            String path = "C:\\Apache-maven-3.2.5\\testing.txt";
            System.out.println("testing");
            File f = new File(path);

            f.createNewFile();

        } catch (Exception e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

插件pom.xml的:

地点在maven-plugin\pom.xml

<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>com.gorovdude</groupId>
    <artifactId>gorov-maven-plugin</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>maven-plugin</packaging>
    <description>gorov Maven Plugin</description>

    <properties>
        <mavenVersion>3.0</mavenVersion>
        <mavenPluginVersion>3.1</mavenPluginVersion>
    </properties>

    <prerequisites>
        <maven>${mavenVersion}</maven>
    </prerequisites>

    <dependencies>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-core</artifactId>
            <version>${mavenVersion}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>${mavenVersion}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>${mavenPluginVersion}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-artifact</artifactId>
            <version>2.0.9</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-plugin-plugin</artifactId>
                <version>${mavenPluginVersion}</version>
                <executions>
                    <execution>
                        <id>generate-descriptor</id>
                        <goals>
                            <goal>descriptor</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <goalPrefix>gorov</goalPrefix>
                    <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Run Code Online (Sandbox Code Playgroud)

pom.xml运行mvn命令的:

项目位置:maven-plugin\src\test\pom.xml

<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>com.gorovdude</groupId>
    <artifactId>test-gorov-maven-plugin</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <description>Testing the plugin</description>

    <build>
        <plugins>
            <plugin>
                <groupId>com.gorovdude</groupId>
                <artifactId>gorov-maven-plugin</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </plugin>
        </plugins>
    </build>
</project>
Run Code Online (Sandbox Code Playgroud)

And*_*dis 4

您可以按以下方式调用该插件的源代码

<build>
    <plugins>
        <plugin>
            <groupId>com.gorovdude</groupId>
            <artifactId>gorov-maven-plugin</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <executions>
                <execution>
                    <goals>
                        <goal>clean</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

您必须在执行中指定目标。如果你输入“maven clean”,你只是调用maven clean插件,而不是你的。一个插件可能包含多个目标,除非您在项目的执行中定义了您想要运行的目标,否则它们不会运行。

看这里: https: //maven.apache.org/guides/plugin/guide-java-plugin-development.html