简单maven 插件项目的创建以及使用。

  参考: https://maven.apache.org/guides/plugin/guide-java-plugin-development.html

  https://maven.apache.org/plugin-tools/index.html

  官网建议插件名称的起名为<name>-maven-plugin

  有两种方式, 第一种是注解方式; 第二种是文档的方式。 下面研究其使用。

1. 注解方式

  参考: https://maven.apache.org/plugin-tools/maven-plugin-tools-annotations/index.html

1. IDEA 新建项目pom 文件如下:

<?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>cn.qz.cloud</groupId>
    <artifactId>my-mvn-plugin</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <!--打包方式-->
    <packaging>maven-plugin</packaging>

    <dependencies>
        <!-- dependencies to annotations -->
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.6.2</version>
            <optional>true</optional>
            <!-- annotations are not used at runtime because @Retention(value=CLASS), they are needed only to build the plugin -->
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-core</artifactId>
            <version>3.3.9</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-plugin-plugin</artifactId>
                <version>3.6.2</version>
            </plugin>
        </plugins>
    </build>

</project>

  注意打包方式为maven-plugin

 2. 标准注解如下:

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.InstantiationStrategy;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Settings;

/**
 * Mojo Description. @Mojo( name = "<goal-name>" ) is the minimal required annotation.
 * @since <since-text>
 * @deprecated <deprecated-text>
 */
@Mojo( name = "<goal-name>",
        aggregator = <false|true>,
        configurator = "<role hint>",
        executionStrategy = "<once-per-session|always>", // (unsupported since Maven 3.0)
        inheritByDefault = <true|false>, // (unsupported since Maven 3.0)
        instantiationStrategy = InstantiationStrategy.<strategy>,
        defaultPhase = LifecyclePhase.<phase>,
        requiresDependencyResolution = ResolutionScope.<scope>,
        requiresDependencyCollection = ResolutionScope.<scope>, // (since Maven 3.0)
        requiresDirectInvocation = <false|true>, // (unsupported since Maven 3.0)
        requiresOnline = <false|true>,
        requiresProject = <true|false>,
        requiresReports = <false|true>, // (unsupported since Maven 3.0)
        threadSafe = <false|true> ) // (since Maven 3.0)
@Execute( goal = "<goal-name>",
        phase = LifecyclePhase.<phase>,
        lifecycle = "<lifecycle-id>" )
public class MyMojo
        extends AbstractMojo
{
    /**
     * @since <since-text>
     * @deprecated <deprecated-text>
     */
    @Parameter( name = "parameter",
            alias = "myAlias",
            property = "a.property",
            defaultValue = "an expression, possibly with ${variables}",
            readonly = <false|true>,
            required = <false|true> )
    private String parameter;

    @Component( role = MyComponentExtension.class,
            hint = "..." )
    private MyComponent component;

    // sample objects taken from Maven API through PluginParameterExpressionEvaluator

    @Parameter( defaultValue = "${session}", readonly = true )
    private MavenSession session;

    @Parameter( defaultValue = "${project}", readonly = true )
    private MavenProject project;

    @Parameter( defaultValue = "${mojoExecution}", readonly = true )
    private MojoExecution mojo;

    @Parameter( defaultValue = "${plugin}", readonly = true ) // Maven 3 only
    private PluginDescriptor plugin;

    @Parameter( defaultValue = "${settings}", readonly = true )
    private Settings settings;

    @Parameter( defaultValue = "${project.basedir}", readonly = true )
    private File basedir;

    @Parameter( defaultValue = "${project.build.directory}", readonly = true )
    private File target;

    public void execute()
    {
        ...
    }
}

  可以看到有许多内置的属性以及对象,也就是maven 可以直接获取的相关变量。

1. org.apache.maven.plugins.annotations.Mojo 注解如下:

package org.apache.maven.plugins.annotations;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * This annotation will mark your class as a Mojo (ie. goal in a Maven plugin).
 *
 * @author Olivier Lamy
 * @since 3.0
 */
@Documented
@Retention( RetentionPolicy.CLASS )
@Target( ElementType.TYPE )
@Inherited
public @interface Mojo
{
    /**
     * goal name (required).
     * @return the goal name
     */
    String name();

    /**
     * default phase to bind your mojo.
     * @return the default phase
     */
    LifecyclePhase defaultPhase() default LifecyclePhase.NONE;

    /**
     * the required dependency resolution scope.
     * @return the required dependency resolution scope
     */
    ResolutionScope requiresDependencyResolution() default ResolutionScope.NONE;

    /**
     * the required dependency collection scope.
     * @return the required dependency collection scope 
     */
    ResolutionScope requiresDependencyCollection() default ResolutionScope.NONE;

    /**
     * your Mojo instantiation strategy. (Only <code>per-lookup</code> and <code>singleton</code> are supported)
     * @return the instantiation strategy
     */
    InstantiationStrategy instantiationStrategy() default InstantiationStrategy.PER_LOOKUP;

    /**
     * execution strategy: <code>once-per-session</code> or <code>always</code>.
     * @return <code>once-per-session</code> or <code>always</code>
     *
     * @deprecated unused
     */
    @Deprecated
    String executionStrategy() default "once-per-session";

    /**
     * does your mojo requires a project to be executed?
     * @return requires a project
     */
    boolean requiresProject() default true;

    /**
     * does your mojo requires a reporting context to be executed?
     * @return requires a reporting context
     *
     * @deprecated unused
     */
    @Deprecated
    boolean requiresReports() default false;

    /**
     * if the Mojo uses the Maven project and its child modules.
     * @return uses the Maven project and its child modules
     */
    boolean aggregator() default false;

    /**
     * can this Mojo be invoked directly only?
     * @return invoked directly only
     *
     * @deprecated unused
     */
    @Deprecated
    boolean requiresDirectInvocation() default false;

    /**
     * does this Mojo need to be online to be executed?
     * @return need to be online
     */
    boolean requiresOnline() default false;

    /**
     * @deprecated unused
     */
    @Deprecated
    boolean inheritByDefault() default true;

    /**
     * own configurator class.
     * @return own configurator class
     */
    String configurator() default "";

    /**
     * is your mojo thread safe (since Maven 3.x)?
     * @return is thread safe
     */
    boolean threadSafe() default false;
}
View Code

相关文章:

  • 2021-08-29
  • 2022-12-23
  • 2021-11-07
  • 2021-11-19
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-24
  • 2022-12-23
  • 2021-11-15
  • 2021-10-21
  • 2022-12-23
  • 2021-07-27
相关资源
相似解决方案