Java Maven - 如何包含空目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2605747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:47:30  来源:igfitidea点击:

Maven - how to include empty directories

javamavendirectory

提问by mickthompson

By default during the build process maven is removing the empty directories.
Do you know if a parameter can be specified in the pom to instruct maven to include empty directories in the generated target/test-classes folder?

默认情况下,maven 在构建过程中删除空目录。
你知道是否可以在pom中指定一个参数来指示maven在生成的target/test-classes文件夹中包含空目录?

采纳答案by VonC

According to this ticket MRESOURCES-36, there should be a <includeEmptyDirs>element, but only for Maven Resources Plugin 2.3.

根据这张票 MRESOURCES-36,应该有一个<includeEmptyDirs>元素,但仅适用于Maven Resources Plugin 2.3

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <version>2.3</version>
  <configuration>
    <includeEmptyDirs>true</includeEmptyDirs>
  </configuration>
</plugin>


For Maven versions which included an older version of the Resources plugin:

对于包含旧版本资源插件的 Maven 版本:

Until this issue is fixed, here is a workaround I've been using successfully.
Add this plugin element into project/build/pluginsin your pom.xml, and change the dir in the mkdir task.

You can have multiple <mkdir>elements for multiple directories. The mkdirtask does nothing if the directory has already been copied by the resources plugin.

在解决此问题之前,这是我一直在成功使用的解决方法。
将此插件元素添加到project/build/plugins您的 中pom.xml,并更改 mkdir 任务中的目录。

<mkdir>多个目录可以有多个元素。mkdir如果资源插件已经复制了目录,则该任务不执行任何操作。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <executions>
    <execution>
      <id>create-empty-directory</id>
      <phase>process-classes</phase>
      <goals>
        <goal>run</goal>
      </goals>
      <configuration>
        <tasks>
          <mkdir dir="${basedir}/target/classes/empty" />
        </tasks>
      </configuration>
    </execution>
  </executions>
</plugin>

This originally came from the openejb-standalone pom.xmlin the openejb project.

这最初来自openejb 项目中的openejb-standalonepom.xml

回答by Simon Nickerson

We've usually got around this problem by including an empty placeholder file in any directories that we need to create but which have no useful content at build time.

我们通常通过在我们需要创建但在构建时没有有用内容的任何目录中包含一个空的占位符文件来解决这个问题。

This also has the advantage that file formats (e.g. zip files) which don't allow the concept of empty directories will still create the right directory structure.

这也有一个优点,即不允许空目录概念的文件格式(例如 zip 文件)仍将创建正确的目录结构。

回答by khmarbaise

Why do you need empty folders under target/test-classes?

为什么在 target/test-classes 下需要空文件夹?

On the other hand you can use the assembly plugin to create empty folders in zip/tar.gz files.

另一方面,您可以使用程序集插件在 zip/tar.gz 文件中创建空文件夹。

Just create an entry in your assembly descriptor which references an existing folder (in this case src/main/resources/bin...

只需在您的程序集描述符中创建一个引用现有文件夹的条目(在本例中为 src/main/resources/bin ...

<fileSet>
  <directory>src/main/resources/bin</directory>
  <outputDirectory>/logs</outputDirectory>
  <directoryMode>0755</directoryMode>
  <excludes>
    <exclude>*</exclude>
  </excludes>
</fileSet>

The above works for .tar.gz and zip files as well. The directoryMode above is only needed if you create .tar.gz files.

以上也适用于 .tar.gz 和 zip 文件。仅当您创建 .tar.gz 文件时才需要上述 directoryMode。

The second possibility is to create an empty folder in your folder structure which is included with the assembly plugin (like zip, tar.gz etc.)...BTW: zip, tar.gz allow empty folders.

第二种可能性是在您的文件夹结构中创建一个空文件夹,该文件夹包含在程序集插件中(如 zip、tar.gz 等)......顺便说一句:zip、tar.gz 允许空文件夹。

回答by jgibson

I used the Assembly Plugin like khmarbaise suggested, but to get it to work I needed to use an Ant-style exclude to make sure that no files or directories crept into the archive:

我使用了 khmarbaise 建议的 Assembly Plugin,但为了让它工作,我需要使用 Ant 风格的 exclude 来确保没有文件或目录进入存档:

<excludes>
    <exclude>**/*</exclude>
</excludes>

回答by Belkasmi

If it's not working for you can use Maven Resources Plugin 2.7.

如果它不起作用,您可以使用Maven Resources Plugin 2.7

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.7</version>
            <configuration>
                <includeEmptyDirs>true</includeEmptyDirs>
            </configuration>
        </plugin>