Maven-Plugin开发实践

package com.zte.sdn.oscp.yang.plugin.manager;

import com.google.common.collect.Maps;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.rtinfo.RuntimeInformation;
import org.sonatype.plexus.build.incremental.BuildContext;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by on 2017/6/7.
 */
public class BaseMavenPlugin extends AbstractMojo {
    public MavenPluginInfo pluginInfo;
    @Parameter(property = "basedir", defaultValue = "${basedir}")
    protected String baseDir;
    @Parameter(property = "resources", defaultValue = "${project.build.resources}")
    protected String resources;
    @Parameter(property = "outDirectory", defaultValue = "${project.build.outputDirectory}")
    protected String outDirectory;
    @Parameter(property = "testResources", defaultValue = "${project.build.testResources}")
    protected String testResources;
    @Parameter(property = "testOutDirectory", defaultValue = "${project.build.testOutputDirectory}")
    protected String testOutDirectory;
    @Parameter(property = "project", required = true, readonly = true,
            defaultValue = "${project}")
    protected MavenProject project;
    @Parameter(readonly = true, defaultValue = "${localRepository}")
    protected ArtifactRepository localRepository;
    @Parameter(readonly = true, defaultValue = "${project.remoteArtifactRepositories}")
    protected List<ArtifactRepository> remoteRepository;
    @Component
    protected RuntimeInformation runtime;
    @Component
    protected BuildContext context;

    public void execute() throws MojoExecutionException, MojoFailureException {
        pluginInfo = new MavenPluginInfo(baseDir, resources, outDirectory,
                testResources, testOutDirectory, project,
                localRepository, remoteRepository, runtime, context);
    }

    class MavenPluginInfo {
        protected String baseDir;
        protected String resources;
        protected String outDirectory;
        protected String testResources;
        protected String testOutDirectory;
        protected MavenProject project;
        protected ArtifactRepository localRepository;
        protected List<ArtifactRepository> remoteRepository;
        protected RuntimeInformation runtime;
        protected BuildContext context;
        protected Map<String, String> configuration = new HashMap<>();

        public MavenPluginInfo(String baseDir, String resources, String outDirectory, String testResources, String testOutDirectory, MavenProject project, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepository, RuntimeInformation runtime, BuildContext context) {
            this.baseDir = baseDir;
            this.resources = resources;
            this.outDirectory = outDirectory;
            this.testResources = testResources;
            this.testOutDirectory = testOutDirectory;
            this.project = project;
            this.localRepository = localRepository;
            this.remoteRepository = remoteRepository;
            this.runtime = runtime;
            this.context = context;
        }

        public String getBaseDir() {
            return baseDir;
        }

        public void setBaseDir(String baseDir) {
            this.baseDir = baseDir;
        }

        public String getResources() {
            return resources;
        }

        public void setResources(String resources) {
            this.resources = resources;
        }

        public String getOutDirectory() {
            return outDirectory;
        }

        public void setOutDirectory(String outDirectory) {
            this.outDirectory = outDirectory;
        }

        public String getTestResources() {
            return testResources;
        }

        public void setTestResources(String testResources) {
            this.testResources = testResources;
        }

        public String getTestOutDirectory() {
            return testOutDirectory;
        }

        public void setTestOutDirectory(String testOutDirectory) {
            this.testOutDirectory = testOutDirectory;
        }

        public MavenProject getProject() {
            return project;
        }

        public void setProject(MavenProject project) {
            this.project = project;
        }

        public ArtifactRepository getLocalRepository() {
            return localRepository;
        }

        public void setLocalRepository(ArtifactRepository localRepository) {
            this.localRepository = localRepository;
        }

        public List<ArtifactRepository> getRemoteRepository() {
            return remoteRepository;
        }

        public void setRemoteRepository(List<ArtifactRepository> remoteRepository) {
            this.remoteRepository = remoteRepository;
        }

        public RuntimeInformation getRuntime() {
            return runtime;
        }

        public void setRuntime(RuntimeInformation runtime) {
            this.runtime = runtime;
        }

        public BuildContext getContext() {
            return context;
        }

        public void setContext(BuildContext context) {
            this.context = context;
        }

        public Map<String, String> getConfiguration() {
            return configuration;
        }

        public void setConfiguration(Map<String, String> configuration) {
            this.configuration = configuration;
        }

        public String getConfiguration(String key) {
            return this.configuration.get(key);
        }

        public void addConfiguration(String key, String value) {
            this.configuration.put(key, value);
        }
    }

}

@Mojo(name = "yang2bean", defaultPhase = PROCESS_SOURCES,
        requiresDependencyResolution = COMPILE)
public class YangUtilManager extends BaseMavenPlugin 

<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>

    <parent>
        <groupId>com.zte.sdn.oscp</groupId>
        <artifactId>oscp-yang-plugins</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>oscp-yang-maven-plugin</artifactId>
    <version>${yang.util.version}</version>
    <packaging>maven-plugin</packaging>

    <dependencies>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-core</artifactId>
            <version>3.3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>3.3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.4</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-scr-plugin</artifactId>
            <version>1.21.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-artifact</artifactId>
            <version>3.3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-project</artifactId>
            <version>3.0-alpha-2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-model</artifactId>
            <version>3.3.9</version>
        </dependency>
        <dependency>
            <groupId>org.sonatype.plexus</groupId>
            <artifactId>plexus-build-api</artifactId>
            <version>0.0.7</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.10</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.21</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.10</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>
                                    target/generated-sources/com/zte/sdn/oscp/yangutils/parser/antlrgencode
                                </source>
                                <sourceDirectory>
                                    target/generated-sources/com/zte/sdn/oscp/yangutils/parser/antlrgencode
                                </sourceDirectory>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-plugin-plugin</artifactId>
                <version>3.4</version>
                <configuration>
                    <skipErrorNoDescriptorsFound>true
                    </skipErrorNoDescriptorsFound>
                </configuration>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>descriptor</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--<plugin>-->
                <!--<groupId>org.apache.felix</groupId>-->
                <!--<artifactId>maven-bundle-plugin</artifactId>-->
                <!--<version>2.5.4</version>-->
                <!--<extensions>true</extensions>-->
            <!--</plugin>-->
        </plugins>
    </build>
</project>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值