我有一个 Spring Web 应用程序,它是作为 Maven 工件 A.war 创建的。它有一个带有工件 B.jar 的 maven 依赖项。工件 B 的 src/main/resources 有一个文件 test.txt。我想从工件 A.war 中读取文件 test.txt。当我分解 B.jar 时,我看到 test.txt 位于 jar 的根目录中。B.jar 最终出现在爆炸的 A.war 的 WEB-INF/lib 中。在 A 中,我尝试使用 File f = new ClassPathResource("test.txt").getFile(); 读取文件。
我收到 java.io.FileNotFoundException: 类路径资源 [test.txt] 无法解析为绝对文件路径,因为它不在文件系统中:jar:B.jar!/test.txt。我也试过 File f = new File(getClass().getResource("test.txt").toURI());
有人可以帮忙吗?
我终于按照链接中提到的备用路径让它工作了
基本上在Artifact A的pom文件中,我使用了maven依赖插件和解包目标。使用它,我解压了工件 B 并将 test.txt 复制到项目 A 的 target/classes 目录中。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>generate-sources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.me</groupId>
<artifactId>artifactB</artifactId>
<version>1.0-SNAPSHOT</version>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<includes>*.txt,*.xml</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
在构建工件 A 时,会创建 war 文件 A.war 并且 test.txt 出现在分解的 war 文件的 WEB-INF/classes 目录中,因此它位于类路径中。
| 归档时间: |
|
| 查看次数: |
2403 次 |
| 最近记录: |