【问题标题】:How to create empty folders with maven archetype?如何使用 Maven 原型创建空文件夹?
【发布时间】:2010-05-07 07:38:46
【问题描述】:

这种方法存在一个问题,位于Codehaus JIRA #ARCHETYPE-57,但这张票中列出的所有说明对我来说都失败了。 marekdec How to get maven archetype to generate empty directories 的博客文章对我来说也失败了。

archetype.xml 中带有尾随 / 的技巧对我不起作用:

<resources>
  <resource>src/main/webapp/</resource>

Unable to find resource 'archetype-resources/src/main/webapp/'

archetype-metadata.xml 中的 fileSet 目录也不适合我:

<fileSet filtered="true" encoding="UTF-8">
 <directory>src/main/webapp/</directory>
</fileSet>

我使用以下 maven-archetype-plugin 来创建我的自定义原型。

mvn org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-5:create

还有其他解决办法吗?还是我错过了什么?谢谢

【问题讨论】:

  • 你使用什么版本的 Maven Archetype?
  • @Pascal: maven-archetype-plugin:2.0-alpha-5

标签: maven-2 maven-archetype


【解决方案1】:

我做了一个快速测试……它对我有用。首先,我创建了一个原型:

$ mvn archetype:generate -B -DarchetypeArtifactId=maven-archetype-archetype \
                            -DgroupId=com.stackoverflow \
                            -DartifactId=Q2786966 \
                            -Dversion=1.0-SNAPSHOT \

我将 archetype.xml 重命名为 archetype-metadata.xml(前者用于 Archetype 1.0.X,后者用于 Archetype 2.0.X),因此项目如下所示:

$树。 . ├── pom.xml └── 源 └── 主要 └── 资源 ├── 原型资源 │   ├── pom.xml │   └── src │   ├── 主要 │   │   └── java │   │   └── App.java │   └── 测试 │   └── java │   └── AppTest.java └── META-INF └── 行家 └── 原型元数据.xml

而archetype-metadata.xml 包含:

<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor name="Q2786966">
  <fileSets>
    <fileSet filtered="true" encoding="UTF-8">
      <directory>src/main/webapp</directory>
    </fileSet>
    <fileSet filtered="true" packaged="true">
      <directory>src/main/java</directory>
      <includes>
        <include>**/*.java</include>
      </includes>
    </fileSet>
  </fileSets>
</archetype-descriptor>

然后我安装了原型并用它来创建一个项目:

$ mvn install
$ cd ~/tmp
$ mvn archetype:generate -B -DarchetypeGroupId=com.stackoverflow \
                            -DarchetypeArtifactId=Q2786966 \
                            -DarchetypeVersion=1.0-SNAPSHOT \
                            -DgroupId=my.group \
                            -DartifactId=my-artifact \
                            -Dversion=1.0-SNAPSHOT

生成的项目如下所示:

$树我的神器/ 我的神器/ ├── pom.xml └── 源 └── 主要 ├── java │   └── 我的组 │   └── App.java └── 网页应用

空的 webapp 目录在那里。

【讨论】:

  • 谢谢Pascal,我的错是我认为archetype.xml 和archetype-metadata.xml 这两个文件都是必要的,感谢您澄清archetype.xml 是针对旧的原型插件版本的,这就是我遇到的原因旧机制。
【解决方案2】:

我通过将以下配置添加到原型 pom.xml 构建配置来解决了这个问题。

假设你的 archetype-metadata.xml 配置文件的文件集如下:

<fileSet encoding="UTF-8">
 <directory>src</directory>
  <includes>
   <include>**/**</include>
  </includes>
</fileSet>

将此添加到您的 pom.xml,而不是原型中包含的那个,实际项目 pom.xml

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

发挥神奇作用的xml配置行是

<includeEmptyDirs>true</includeEmptyDirs>

完整的配置如下所示

<project ...>
...

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

        </plugins>
    </build>

</project>

【讨论】:

  • 这是最佳答案!
  • 为什么这没有更多的选票?这比我见过的任何其他解决方案都更清晰、更简单,并且大大减少了 archetype-metadata.xml 中的配置膨胀
【解决方案3】:

我在question 的第一个答案中遇到了同样的问题。重新配置 maven-resources-plugin。

根据这张票 MRESOURCES-36,应该有一个 includeEmptyDirs> 元素,但仅适用于 Maven 资源插件 2.3。

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

【讨论】:

    猜你喜欢
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多