【发布时间】:2015-03-17 08:10:41
【问题描述】:
我有一个包含 .xlsx 和 .docx 文件的文件夹 src/resource/templates。我正在使用 maven,如何将文件从该文件夹复制到 jar 文件中?
【问题讨论】:
-
将这些文件放入
src/main/resources,它们将被自动复制。
我有一个包含 .xlsx 和 .docx 文件的文件夹 src/resource/templates。我正在使用 maven,如何将文件从该文件夹复制到 jar 文件中?
【问题讨论】:
src/main/resources,它们将被自动复制。
您可以向 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 中
【讨论】:
<resource> <directory>${basedir}</directory> <includes> <include>*</include> </includes> </resource> 所以 * 应该包括所有内容,并且模板是一个子文件夹,但由于某种原因,也许你不能解释一下?文件类型有 xlsx、xls、doc、docx 和 txt @ikettu
<include>*</include>表示所有文件。您必须使用例如 <include>**/*.txt</include> 递归查找所有 txt 文件。
src/resource/templates 的整个子树应该递归地包含在内,因为它会在组装过程中产生 jar。
我使用 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>
【讨论】: