我正在构建一个依赖于一些配置文件的命令行工具。
在开发时,我将这些文件放在 下src/main/resources
,并由 访问getClass().getResourceAsStream("/config-file-name")
,效果很好。
我选择 maven-assembly-plugin 来构建具有所有依赖项的 jar。
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.myproject.Scheduler</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
配置文件都包装在jar中,但我想将它们分开。
所以,我添加以下配置,
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>conf.properties</include>
</includes>
<filtering>true</filtering>
<excludes>
<exclude></exclude>
</excludes>
<targetPath>..</targetPath>
</resource>
</resources>
完成此操作后,配置文件与 jar 一起出现,而不是包装在 jar 中。
但是我的代码现在无法访问这些文件。
我是否需要更改在代码中访问文件的方式?
还是我不需要将这些文件放入src/main/resources/
任何替代方案?
如果有人知道,请帮助。
谢谢。
更新:最后,我放弃使用 maven 资源文件,而是将文件路径默认设置为与 jar 相同的路径,一切正常,除了 Eclipse 抱怨。
由于我想将配置文件移出 jar,并且不希望它位于 target/classes 文件夹中,所以我使用<targetPath>..</targetPath>
将配置文件移动到与 jar 文件相同的目录。
当我重新生成 Eclipse 项目设置时,Eclipse 说:不能在输出文件夹“project/target”内嵌套输出文件夹“project/target/test-classes”。
有人帮我吗?