thrift maven

maven-thrift-plugin该插件可以让我们在maven中使用 编译.thrift文件

<?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>com.liyao</groupId>
  <artifactId>thrift_plugin_project</artifactId>
  <version>1.0-SNAPSHOT</version>
 
  <name>thrift_project</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
 
  <dependencies>
    <!-- https://mvnrepository.com/artifact/org.apache.thrift/libthrift -->
    <dependency>
      <groupId>org.apache.thrift</groupId>
      <artifactId>libthrift</artifactId>
      <version>0.9.3</version>
    </dependency>
  </dependencies>
 
  <build>
      <plugins>
          <plugin>
              <groupId>org.apache.thrift.tools</groupId>
              <artifactId>maven-thrift-plugin</artifactId>
              <version>0.1.11</version>
              <configuration>
                  <!--<thriftExecutable>/usr/local/bin/thrift</thriftExecutable>-->
                  <!--<thriftSourceRoot>src/main/thrift</thriftSourceRoot>-->
                  <!--<outputDirectory>src/main/java</outputDirectory>-->
              </configuration>
              <executions>
                  <execution>
                      <id>thrift-sources</id>
                      <phase>generate-sources</phase>
                      <goals>
                          <goal>compile</goal>
                      </goals>
                  </execution>
                  <!--<execution>-->
                      <!--<id>thrift-test-sources</id>-->
                      <!--<phase>generate-test-sources</phase>-->
                      <!--<goals>-->
                          <!--<goal>testCompile</goal>-->
                      <!--</goals>-->
                  <!--</execution>-->
              </executions>
          </plugin>
      </plugins>
  </build>
</project>

在插件部分,我们执行了插件的compile目标,并且将其绑定在generate-sources阶段。

为什么要绑定在这个阶段?而不是compile阶段呢?因为我们thrift插件的作用是生成java文件,而在maven执行compile阶段时,java文件必须生成完毕才能进行编译,因此,该插件的执行必须在compile之前,所以放在generate-sources阶段比较合适。

我们可以为插件做一些配置:

thriftExecutable,指的是thrift编译器的位置,如果我们配置了环境变量,可以不指定。验证环境变量可以使用thrift --version命令。

thriftSourceRoot,thrift源文件的目录,默认会从src/main/thrift下读取。

outputDirectory,生成java文件的目录。其实这个一般不需要配置,因为java文件的包名是在.thrift文件以namespace的形式定义的。

所以上面的pom文件没有做任何配置。

接着执行:

mvn clean install 命令即可生成一个jar包。

转载于:https://my.oschina.net/u/2000675/blog/3022056

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值