自定义maven插件 Hello, mojo.


pom.xml

<?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.go</groupId>
    <artifactId>plugin_demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>maven-plugin</packaging>


    <dependencies>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>3.0</version>
        </dependency>

        <!-- dependencies to annotations -->
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.4</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>


</project>

GreetingMojo.java

package cn.go;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;

@Mojo(name = "go")
public class GreetingMojo extends AbstractMojo {
    public void execute() throws MojoExecutionException {
        getLog().info("Hello, mojo.");
    }
}

运行 install

在这里插入图片描述

install 报错

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:3.2:descriptor (default-descriptor) on project plugin_demo: Execution default-descriptor of goal org.apache.maven.plugins:maven-plugin-plugin:3.2:descriptor failed: Plugin org.apache.maven.plugins:maven-plugin-plugin:3.2 or one of its dependencies could not be resolved: The following artifacts could not be resolved: org.apache.maven.plugin-tools:maven-plugin-tools-annotations:jar:3.2, org.apache.maven.plugin-tools:maven-plugin-tools-api:jar:3.2, org.apache.maven.plugin-tools:maven-plugin-tools-generators:jar:3.2, org.apache.maven.plugin-tools:maven-plugin-tools-java:jar:3.2, org.apache.maven.plugin-tools:maven-plugin-tools-beanshell:jar:3.2, org.apache.maven.plugin-tools:maven-plugin-tools-model:jar:3.2: Failure to find org.apache.maven.plugin-tools:maven-plugin-tools-annotations:jar:3.2 in http://10.129.2.106:8081/nexus/content/groups/public/ was cached in the local repository, resolution will not be reattempted until the update interval of nexus-private has elapsed or updates are forced -> [Help 1]

  • 错误原因是 ,构建时缺失依赖

在这里插入图片描述

  • 上图是一个构建的标准生命周期,报错的阶段是图中圈出的节点;
  • 解决方法(2个方法任意一个都行,不是两个步骤):

配置代理

<proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    -->
	
	
	 <!-- <proxy> -->
        <!-- id 代理的名称(随便设) -->
        <!-- <id>optional</id> -->
        <!-- true 表示生效 -->
        <!-- <active>true</active> -->
        <!-- 协议 -->
        <!-- <protocol>http</protocol> -->
        <!-- 本机上网用户名及密码,如果没有,请注释或者是删除 -->
        <!-- <username>*******.ex</username> -->
        <!-- <password>a*169646</password> -->
        <!-- 公司上网使用ip及端口,即代理,这里替换成相对应的ip和端口 -->
        <!-- <host>127.0.0.1</host> -->
        <!-- <port>8888</port> -->
        <!-- 填写不用代理的地址,以竖线|分割多个地址,一般填写本地Maven仓库地址 -->
        <!-- <nonProxyHosts>local.net</nonProxyHosts> -->
    <!-- </proxy> -->
	
	 <proxy>
        <id>optional</id>
        <active>true</active>
        <protocol>http</protocol>
        <host>127.0.0.1</host>
        <port>8888</port>
        <nonProxyHosts>local.net</nonProxyHosts>
    </proxy>
  </proxies>

pom.xml 中添加配置

<build>
        <plugins>
            <plugin>
                <groupId>cn.go</groupId>
                <artifactId>plugin_demo</artifactId>
                <version>1.0-SNAPSHOT</version>
            </plugin>
        </plugins>
    </build>
  • 刷新项目,可以看到如下效果:

在这里插入图片描述

  • 运行 goal mvn cn.go:plugin_demo:1.0-SNAPSHOT:go ;效果如图:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值