Maven 资源过滤破坏下载

标签 maven properties resources profiles

我有一个 Maven/Java 应用程序。该应用程序的一部分允许您下载一些文档。这是项目设置。

+src
    +main
        +resources
            +downloads
                MyDocument.docx
            jdbc.properties
pom.xml

jdbc.properties 时下载工作正常其中包含硬编码值。但是,我正在尝试更新应用程序以使用 Maven 配置文件并为不同的环境指定不同的数据库连接。我设法让它与pom.xml中的以下内容一起工作.

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

但是,即使jdbc.properties文件正确填充了正确的环境数据库信息,下载功能停止工作。该文件将被下载,但当您尝试打开它时,它显示 The file MyDocument.docx cannot be opened because there are problems with the contents. .

我尝试更改 <directory>src/main/resources/*.properties并添加额外的 <resource>我在哪里转动 <filtering>为 false src/main/resources/downloads 。但这两种方法都不起作用。如何防止 Maven 过滤损坏文件?

仅供引用 - 我查看了 WAR 内部,也无法从那里打开文档(它们已经损坏)。

最佳答案

更新:来自 https://stackoverflow.com/a/10025058/516167 的更好解决方案

<plugin>
  <artifactId>maven-resources-plugin</artifactId>
  <version>2.5</version>
  <configuration>
    <encoding>UTF-8</encoding>
    <nonFilteredFileExtensions>
      <nonFilteredFileExtension>xls</nonFilteredFileExtension>
    </nonFilteredFileExtensions>
  </configuration>
</plugin>

您应该从过滤中排除诸如 MyDocument.docx (*.docs) 之类的文件。

<build>
   <resources>
      <resource>
        <directory>src/main/resouces</directory>
        <filtering>true</filtering>
        <excludes>
          <exclude>**/*.docx</exclude>
        </excludes>
      </resource>
    </resources>
</build>

或定义备用下载目录,例如:

<build>
   <resources>
      <resource>
        <directory>src/main/downloads</directory>
      </resource>
    </resources>
</build>

关于Maven 资源过滤破坏下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15455594/

相关文章:

java - 源服务器没有找到目标资源的当前表示或不愿意透露存在的表示。关于部署到 tomcat

java - 使用 liquibase 降级数据库 - Spring

java - 无法通过命令行启动 Java 应用程序

java - maven 中 WebProject 的依赖

ios - 无法访问 parentviewcontroller 的属性

c# - 如何在控制台打印类属性?

c# - 仅通过构造函数设置类的属性

JavaScript 处理

c# - 从另一个项目/程序集访问 resx 文件

c# - 在 aspx 页面中使用 Asp.Net WebForms 上的属性资源本地化文本