【问题标题】:Adding Templates in Maven在 Maven 中添加模板
【发布时间】:2015-03-17 08:10:41
【问题描述】:

我有一个包含 .xlsx 和 .docx 文件的文件夹 src/resource/templates。我正在使用 maven,如何将文件从该文件夹复制到 jar 文件中?

【问题讨论】:

  • 将这些文件放入src/main/resources,它们将被自动复制。

标签: maven directory copy


【解决方案1】:

您可以向 maven 项目添加更多资源目录。见http://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html

例如:

<project>
 ...
 <build>
   ...
   <resources>
     <resource>
       <directory>src/resource/templates</directory>
     </resource>
   </resources>
   ...
 </build>
 ...
</project>

已编辑补充说明:

如果您现在有文件 src/resource/templates/example.xlsx,它应该在 jar 的根目录中。

文件 src/resource/templates/report/resource/Templates/example.xlsx 将作为文件 report/resource/Templates/example.xlsx 复制到 jar 中

【讨论】:

  • 所以这是资源的代码&lt;resource&gt; &lt;directory&gt;${basedir}&lt;/directory&gt; &lt;includes&gt; &lt;include&gt;*&lt;/include&gt; &lt;/includes&gt; &lt;/resource&gt; 所以 * 应该包括所有内容,并且模板是一个子文件夹,但由于某种原因,也许你不能解释一下?文件类型有 xlsx、xls、doc、docx 和 txt @ikettu
  • &lt;include&gt;*&lt;/include&gt;表示所有文件。您必须使用例如 &lt;include&gt;**/*.txt&lt;/include&gt; 递归查找所有 txt 文件。
  • 在建议的解决方案中,来自src/resource/templates 的整个子树应该递归地包含在内,因为它会在组装过程中产生 jar。
  • 我用过/,但是没用,这个不是说包罗万象吗?
【解决方案2】:

我使用 maven copy 找到了答案。

  <plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.4.3</version>
    <executions>
      <execution>
        <id>copy-resources</id>
        <!-- here the phase you need -->
        <phase>compile</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <outputDirectory>${basedir}/classes/report/resource/Templates</outputDirectory>
          <resources>          
            <resource>
              <directory>${basedir}/src/report/resource/Templates</directory>
              <filtering>true</filtering>
            </resource>
          </resources>              
        </configuration>            
      </execution>
    </executions>
  </plugin>

【讨论】:

    猜你喜欢
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 2019-11-23
    相关资源
    最近更新 更多