2013-03-16 25 views
2

我有一个Maven/Java应用程序。部分应用程序允许您下载一些文件。这是项目设置。Maven资源筛选打破下载

+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内部,无法从那里打开文件(它们已经损坏)。

回答

1

更新:从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> 
+0

当我做的第一个,有一个'NullPointerException'我'InputStream'进行下载。它被填充为'getClass()。getClassLoader()。getResourceAsStream(“/ downloads/MyDocument.docx”);'。 – 2013-03-17 23:33:01

+0

第二个选项,文件仍被报告为损坏。 – 2013-03-17 23:35:39

+0

我也尝试在第二个选项中加入' false'。还重新排序''标签,但没有运气。总是说它是腐败的。 – 2013-03-17 23:40:01