一直在使用Maven,用了各种各样的插件,但是有时候没有的话,还是需要自己写点。写一个插件并不难,会写插件的另一个好处就是了解了更多的Maven工作机制的内幕。对更好的使用Maven有帮助。
首先创建一个Maven项目,名叫plugin-example1。
这里要理解一个术语:mojo,就是Maven Plain Old Java Object,也就是一个普通的Java类。
我们需要mojo的api库,所以在pom.xml中添加一个依赖:
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
创建一个Example类,继承于AbstractMojo,实现execute方法。
代码很简单:
public class Example extends AbstractMojo{
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("Hello world");
}
}
getLog()获取的是AbstractMojo内部的log,类型是:org.apache.maven.plugin.logging.Log;
至少在Mojo的开发中,不要使用其他的Log基础设施。
现在修改一下工程的描述信息:
<groupId>org.freebird</groupId>
<artifactId>plugin-example1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>plugin-example1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
注意,packaging的值是maven-plugin
现在编译吧,很快就遇到错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:2.9:descriptor (default-descriptor) on project plugin-example1: Error extracting plugin descriptor: 'No mojo definitions were found for plugin: org.freebird:plugin-example1.' -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:2.9:descriptor (default-descriptor) on project plugin-example1: Error extracting plugin descriptor: 'No mojo definitions were found for plugin: org.freebird:plugin-example1.'
需要加入一个maven-plugin-plugin来生成descriptor。不知道Maven的官方文档中为什么不提。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.0</version>
<executions>
</executions>
<configuration>
<!-- Needed for Java 5 annotation based configuration, for some reason. -->
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
</plugin>
</plugins>
</build>
这样编译就通过了。或者在类的注视上添加一个descriptor:
/**
*
* @goal sayhi
*/
public class Example extends AbstractMojo{
为了将Maven部署在私服上,需要加上如下配置:
<distributionManagement>
<snapshotRepository>
<id>snapshots</id>
<url>http://your_server:8080/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
然后运行mvn clean package deploy
部署成功。
分享到:
相关推荐
gRPC-maven-helloworld HelloWorld代码基于以下快速入门: : 该测试的目的是根据以下有关如何构建POM插件和依赖关系的说明,看它是否使用maven进行构建: : 从github克隆此存储库,然后运行: mvn clean ...
一个MapReduce版的单词计数程序,相当于MapReduce的HelloWorld,用装有Maven插件的Eclipse打开
使用Maven的Scala的Hello-world...它利用了maven插件 (用于编译和运行)和 (用于运行单元测试) 编译中 可以使用标准命令mvn compile 跑步 要运行该应用程序,请键入mvn scala:run 要运行单元测试,请键入mvn test
maven管理项目从安装到使用过程,同时带有总结,做项目少不了管理,如果只是做一个hello world项目,那当然你可以不用考虑使用maven等来管理项目,但是你应该不会只做着娱乐,所以有必要学习maven等管理项目的方法 ,...
这是一个实施ImageJ命令的Maven项目示例。 它是在您选择的IDE中开发新ImageJ命令的理想起点。 您甚至可以使用与您不同的IDE与开发人员进行协作。 例如,在,它就像File> Import ...> Existing Maven Project一样...
续上一节(maven使用手册),本文档详细描述了m2eclipse插件的使用,与上一份文档相同,都有非常详细的图解,让你轻松上手m2eclipse插件,附带2个简单的实例(helloworld和Spring2.5.6+Struts2.1.8.1框架的搭建)
这个有用的Maven插件可以在Java项目中提供对本地化消息的构建时验证。 这个想法是在Java代码中使用一些标记(@Annotations)来提供有关已定义的本地化消息的信息。 然后在构建时,maven插件将使用此信息来分析和...
用法捆绑一个应用程序: mvn install payara-micro:bundle运行捆绑的应用程序: java -jar target/payara-maven-helloworld-1.0-SNAPSHOT-microbundle.jar在测试正在运行的应用程序在不捆绑的情况下启动应用程序: ...
Java-Maven-Junit-HelloWorld “你好,世界!” 使用Maven进行Java编写的示例构建,展示了一些非常简单的测试。 此示例说明: 用编写的单元测试 使用进行单元测试以模拟类并测试System.exit() 用编写的集成测试 ...
们都知道Alfresco 4.0以后采用了Activiti BPMN2.0 引擎(关于Activiti, 是一个open source 项目。 Alfresco从jBPM公司挖出了jBPM的创建人和若干核心人员,...在安装Activiti之前,我们要首先安装Maven Eclipse插件。
在Azure Functions上运行的示例“ Hello,world” Spring Boot应用程序 这是一个示例应用程序,用于在Azure Functions之上展示Spring Cloud Function的用法。 制作工具 该分支使用Maven构建应用程序,请改用gradle...
我们安装Maven Helper的插件,用来查看spring-boot-starter-web模块的依赖。核心 POM,包含自动配置支持、日志库和对 YAML 配置文件的支持。 创建 SpringBoot Web 应用 为快速进行开发,推荐你使用IDEA这类开发工具...
你好一个简单的hello world Java应用程序,带有一些编码错误,用于演示Coverity和Polaris与各种CI系统的集成。 生成命令 -mvn干净编译蚂蚁清理构建 -Gradle清洁组装CI集成示例(Maven) -Circle CI管道适用于自托管...
标签 项目 行家 目录 本指南将引导您使用 Maven 构建一个简单的 Java 项目。 你将构建什么 您将创建一个提供时间的应用程序,...link : initial / src / main / java / hello / HelloWorld . java[] src/main/java/
Spring Boot 学习示例 Spring Boot 2.0 Mysql 5.6 ...spring-boot-helloworld:Spring Boot 3.0 Hello World Test 单元测试示例 spring-boot-scheduler:Spring Boot 3.0 定时任务 scheduler 使用示例 .....
这个插件创建了新的基于Maven的JAX WS项目,并支持Spring,JPA,Hibernate和SLF4J。... 核心项目包含一个示例Hello World Service和其他业务逻辑。 Web项目包含与Spring和Web Deployment有关的XML。
##spring-boot-prototype 考虑利用 Spring Boot 为您的应用程序或企业快速开发 REST...world您应该会看到以下响应: {"hello":"world"}构建一个可运行的 Jar 文件使用 Spring Boot 的一大优势是 Spring Boot Maven 插件
__artifactId__.test : eclipse-test-plugin (Fragment project) | |---__artifactId__.site : eclipse-repository生成的插件基于PDE向导中的Hello World模板,使用Eclipse 3.x体系结构: [...] creates a simple ...
学习用Eclipse+Maven来构建并打包一个简单的单词统计的例程。 第一步 在EclipseIDE中安装Scala插件 在Eclipse中安装Scala插件 第二步 创建Scala Project 创建Scala 项目 第三步 给Scala项目注入maven依赖 将Scala ...
Jetty和Maven HelloWorld教程 Jetty(6)入门 (www.itjungle.com) Jetty Start.jar 配置Jetty 如何设置上下文(Context Path) 如何知道使用了那些jar包 如何配置SSL 如何使用非root用户监听80端口 如何配置连接器...