==> 学习汇总(持续更新)
==> 从零搭建后端基础设施系列(一)-- 背景介绍
一.环境准备
- 安装thrift
方法一:brew install thrift
方法二:
去官网下载并解压https://thrift.apache.org/download。
进入解压后的目录执行configure,make,make install
二、创建maven插件工程
1.new project
2.和正常的maven工程一样,填写即可
3.在pom中添加插件注解依赖和指定java版本
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.2</version>
</dependency>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
如果不指定java版本,用maven打包build的时候,会给你使用默认的java版本,我这里它会默认给我指定java5,我也不知道咋回事,所以干脆直接指定1.8就好了。
三、编写插件
项目创建后,大致结构如下,其中MyMojo继续了AbstractMojo,当插件运行的时候,会执行execute方法。所以可以得出结论,我们只需要在execute方法中编写我们的逻辑。
这里代码我已经写好,就不一一的解释了,可以去github上下载下来运行一下。
注意:@goal xxx 其中的xxx就是这个run
一个插件里面,可以有多个goal,每一个goal对应一个mojo类。
四、使用插件
编写完,打包后,在别的项目引用即可
1.将thrift文件放在当前工程目录下的任意位置都可以
2.生成的thrift文件默认放在src/main/java,可以使用配置文件*.tconfig进行配置
3.需要引入thrift jar包,版本最好和你电脑安装的一样或者大于,否则可能会不兼容。
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.11.0</version>
</dependency>
==> CODE