thrift maven

maven-thrift-plugin该插件能够让咱们在maven中使用 编译.thrift文件java

<?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阶段。web

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

咱们能够为插件作一些配置:maven

thriftExecutable,指的是thrift编译器的位置,若是咱们配置了环境变量,能够不指定。验证环境变量能够使用thrift --version命令。ui

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

outputDirectory,生成java文件的目录。其实这个通常不须要配置,由于java文件的包名是在.thrift文件以namespace的形式定义的。spa

因此上面的pom文件没有作任何配置。插件

接着执行:code

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

相关文章
相关标签/搜索