2010-11-03 132 views
31

我使用maven-exec-plugin來生成Thrift的java源代碼。它調用外部Thrift編譯器並使用-o指定輸出目錄「target/generated-sources/thrift」。在maven中需要時創建目錄

問題既不是maven-exec-plugin也不是Thrift編譯器自動創建輸出目錄,我不得不手動創建它。 有沒有一個體面的/便攜的方式使用創建缺少需要的目錄?我不想在pom.xml中定義mkdir命令,因爲我的項目需要獨立於系統。

回答

26

相反Exec插件的,使用antrun插件先創建目錄,然後調用節儉的編譯器。

<plugin> 
    <artifactId>maven-antrun-plugin</artifactId> 
    <executions> 
    <execution> 
     <id>generate-sources</id> 
     <phase>generate-sources</phase> 
     <configuration> 
     <tasks> 
      <mkdir dir="target/generated-sources/thrift"/> 
      <exec executable="${thrift.executable}"> 
      <arg value="--gen"/> 
      <arg value="java:beans"/> 
      <arg value="-o"/> 
      <arg value="target/generated-sources/thrift"/> 
      <arg value="src/main/resources/MyThriftMessages.thrift"/> 
      </exec> 
     </tasks> 
     </configuration> 
     <goals> 
     <goal>run</goal> 
     </goals> 
    </execution> 
    </executions> 
</plugin> 

您可能還想看看maven-thrift-plugin

+0

好的解決辦法,但沒有人知道一個沒有antrun ?有時我必須建立多次,因爲這不起作用(也許Maven 3.1.1?) – Adrian 2013-11-14 06:58:52

+0

' generate-sources'不需要。附:我在Maven中唯一喜歡的是依賴管理。 – 2015-08-16 11:45:06

+0

@Adrian看到這個antrun-less解決方案:https://stackoverflow.com/a/40674535/363573 – Stephan 2017-07-16 11:59:25

15

您可以定義一個ant任務來完成這項工作。將plugin聲明放入項目的pom.xml中。這將讓你的項目與系統無關:

 <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-antrun-plugin</artifactId> 
      <executions> 
       <execution> 
        <id>createThriftDir</id> 
        <phase>process-resources</phase> 
        <configuration> 
         <tasks> 
          <delete dir="${thrift.dir}"/> 
          <mkdir dir="${thrift.dir}"/> 
         </tasks> 
        </configuration> 
        <goals> 
         <goal>run</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
4

如果你想在項目中的某處準備這樣的文件夾結構,然後複製到地方你想,使用Maven的資源插件來做到這一點:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-resources-plugin</artifactId> 
    <executions> 
     <execution> 
      <id>copy-folder</id> 
      <phase>package</phase> 
      <goals> 
       <goal>copy-resources</goal> 
      </goals> 
      <configuration> 
       <outputDirectory>${project.build.directory}</outputDirectory> 
       <resources> 
        <resource> 
        <filtering>false</filtering> 
        <directory>${project.basedir}/src/main/resources/folders</directory> 
        </resource> 
       </resources> 
      </configuration> 
    </execution> 
    </executions> 
</plugin>