有什么区别:
InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileName)
Run Code Online (Sandbox Code Playgroud)
和
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName)
Run Code Online (Sandbox Code Playgroud)
和
InputStream is = this.getClass().getResourceAsStream(fileName)
Run Code Online (Sandbox Code Playgroud)
每个人何时比其他人更适合使用?
我想要读取的文件在类路径中作为我的类来读取文件.我的类和文件位于同一个jar中,并打包在EAR文件中,并部署在WebSphere 6.1中.
我有一个企业应用程序,我正在从Ant构建转换为Maven.它几乎完全转换; 这是我需要解决的最后一件事.应用程序打包为一个EAR文件,其中包含两个WAR,并具有一个JAR模块,该模块提供应用程序的所有核心功能.
我正在使用Freemarker模板库来生成应用程序发送的自动电子邮件的消息正文.Freemarker需要将*.ftl模板文件放在类路径上,因为这是一个不是特定于一个WAR或另一个WAR的核心应用程序功能,所以它需要在JAR中.
定义JAR的Maven模块具有以下POM:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<relativePath>../../pom.xml</relativePath>
<groupId>com.company.project</groupId>
<artifactId>projectName</artifactId>
<version>1.8.0</version>
</parent>
<artifactId>core</artifactId>
<packaging>jar</packaging>
<name>Core Application</name>
<profiles>
<!-- snip -->
</profiles>
<dependencies>
<!-- snip -->
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.ftl</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<!-- snip -->
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
*.ftl文件位于src/main/resources/template /,其中一些位于template /中的子目录中.src/main/resources中还有其他文件 - 一些.properties和一些.xml,一些位于根目录,一些位于目录结构下.
当我在这个模块(或父模块)上运行包阶段时,作为构建过程的一部分创建的目标/ classes目录包含模板目录,该模板目录又包含所有*.ftl,*.xml和*具有适当目录结构的.properties文件.如果我手动JAR这个目录,一切都很完美.
这里变得奇怪而且我迷路了:当maven-jar-plugin创建JAR时,它包含XML和属性文件,但是JAR中完全没有模板目录,并且其内容无处可寻.
如您所见,我尝试明确地包括**/*.ftl.它没有任何区别; 我可以排除整个"包含"标记,我得到完全相同的行为.
我正在使用Maven 3.0.5和maven-jar-plugin 2.4.