在maven中需要时创建目录

发布于 2024-09-30 02:24:56 字数 242 浏览 0 评论 0原文

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

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

I am using maven-exec-plugin to generate java sources of Thrift. It invokes the external Thrift compiler and using -o to specify the output directory, "target/generated-sources/thrift".

The problem is neither maven-exec-plugin nor Thrift compiler automatically create the output directory, I have to manually create it.
Is there a decent/portable way use create missing directories when needed? I don't want to define a mkdir command in the pom.xml, since my project need to be system independent.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

メ斷腸人バ 2024-10-07 02:24:56

使用 antrun 代替 exec 插件插件首先创建目录,然后调用 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

Instead of the exec plugin, use the antrun plugin to first create the directory and then invoke the thrift compiler.

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

You may also want to take a look at the maven-thrift-plugin.

悲凉≈ 2024-10-07 02:24:56

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

You can define an ant task to do the job. Put the plugin declaration into your project's pom.xml. This will keep your project system-independent:

        <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>
谜兔 2024-10-07 02:24:56

如果您想在项目中的某个位置准备此类文件夹结构,然后复制到您想要的位置,请使用 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>

If you would like to prepare such folder structure somewhere in your project and then copy to place you want, use maven-resource plugin to do that:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文