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

相關文章
相關標籤/搜索