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个方法任意一个都行,不是两个步骤):
- 1、手动下载对应依赖,可参考我的这篇博客:https://blog.csdn.net/wwq921220/article/details/106918751
- 2、禁用
settings.xml
文件中的私服配置,使maven连接到默认的中心仓库;如果连接不上可配置代理(前提是可以连接公网)
配置代理
<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
;效果如图: