【问题标题】:create directory when needed in maven在 Maven 中需要时创建目录
【发布时间】:2011-05-04 09:54:30
【问题描述】:

我正在使用 maven-exec-plugin 来生成 Thrift 的 java 源代码。它调用外部 Thrift 编译器并使用 -o 指定输出目录“target/generated-sources/thrift”。

问题是maven-exec-plugin和Thrift编译器都没有自动创建输出目录,我必须手动创建。 是否有一种体面/便携的方式在需要时使用创建缺少的目录?我不想在 pom.xml 中定义 mkdir 命令,因为我的项目需要独立于系统。

【问题讨论】:

    标签: maven-2 directory


    【解决方案1】:

    如果您想在项目中的某处准备这样的文件夹结构,然后复制到您想要的位置,请使用 maven-resource 插件来做到这一点:

    <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>
    

    【讨论】:

    • 无法使用 Git 跟踪空文件夹。你最好使用 maven-assembly-plugin
    【解决方案2】:

    您可以定义一个 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>
    

    【讨论】:

      【解决方案3】:

      代替exec插件,使用antrun插件首先创建目录,然后调用thrift编译器。

      <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。

      【讨论】:

      • 很好的解决方案,但有人知道没有antrun吗?有时我必须构建多次,因为这不起作用(也许是 Maven 3.1.1?)
      • &lt;id&gt;generate-sources&lt;/id&gt; 不需要。附言我唯一喜欢 Maven 的就是依赖管理。
      • @Adrian 查看这个无蚂蚁解决方案:stackoverflow.com/a/40674535/363573
      • tasks 已从 maven-antrun-plugin 中删除。请改用target。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      • 2019-10-12
      • 2019-03-13
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      相关资源
      最近更新 更多