【问题标题】:How to stop Apache maven <outputDirectory> to overwrite the folder如何停止 Apache maven <outputDirectory> 覆盖文件夹
【发布时间】:2011-10-24 20:57:14
【问题描述】:

我有以下目录层次结构:

generated 

   |    
   | -->java

Java目录有以下包:com.model

包含我在编译应用程序之前从某处复制/粘贴的 java 模型。

我使用协议缓冲区的问题,我告诉 maven 将生成的文件输出到相同的先前目录但通过一个新包:

结果:Protocol buffer 生成新包并删除旧包。
我不知道为什么它会这样做,虽然包名不同?

这是我用来从协议缓冲区生成 java 的部分 pom:

        <plugin>
            <groupId>com.google.protobuf.tools</groupId>
            <artifactId>maven-protoc-plugin</artifactId>
            <configuration>
                <protocExecutable>C:\protoc.exe</protocExecutable>
                <protoSourceRoot>./src/proto</protoSourceRoot>
                <outputDirectory>./src/generated/java</outputDirectory>

            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

【问题讨论】:

    标签: maven-2 maven-plugin maven-3


    【解决方案1】:

    如果您查看插件的代码,您会发现代码已被硬编码以清理目录:

    https://github.com/dtrott/maven-protoc-plugin/blob/master/src/main/java/com/google/protobuf/maven/AbstractProtocMojo.java#L154

    // Quick fix to fix issues with two mvn installs in a row (ie no clean)
    cleanDirectory(outputDirectory);
    

    有两种方法可以解决这个问题。将输出目录设置为临时目录,然后使用 maven copy 插件或 maven build 插件将文件复制到您选择的目录中,或者修改 maven 插件以删除该行(或者更好的是使其可配置)。

    汤米

    【讨论】:

    • 实际上你的第一种方法是我不敢提及的,因为我一直在尝试,但还不够努力,将生成的目录移动到首选位置。但是到目前为止还没有那么有希望.....你能不能给我一些提示,你可以与我分享,让我使用 maven 将目录剪切/粘贴到新位置!
    • 查看复制资源目标的使用情况:maven.apache.org/plugins/maven-resources-plugin/examples/… 在这种情况下确保关闭过滤
    • 我使用了“maven-antrun-plugin”,但我得到“An Ant BuildException has occurred: only one tofile and todir may be set”...请在下面查看我的答案。我想知道你是否可以帮助我!
    【解决方案2】:

    我通过以下方式解决了我的问题:

    <plugin>
                  <artifactId>maven-antrun-plugin</artifactId>
                  <executions>
                      <execution>
                          <phase>generate-resources</phase>
                          <goals>
                              <goal>run</goal>
                          </goals>
                          <configuration>
                              <tasks>
                                  <delete dir="./destination"/>
                                  <copy todir="./destination">
                                        <fileset dir="./source"/>
                                  </copy>
                                  <delete dir="./source"/>
                              </tasks>
                          </configuration>
                      </execution>
                  </executions>
            </plugin>
    

    但是,我收到此错误“An Ant BuildException has occurred: only one of tofile and todir may be set”

    【讨论】:

    • 其实我是在源码中找到的: if (destFile != null && destDir != null) { throw new BuildException("只能设置tofile和todir之一" + "。 ");但是,我已经创建了两个目标目录!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 2021-08-20
    相关资源
    最近更新 更多