Java 我应该在 Maven 项目的路径中的哪个位置放置不被视为资源的配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3001713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where in maven project's path should I put configuration files that are not considered resources
提问by Paralife
I have a simple java maven project. One of my classes when executing needs to load an xml configuration file from the classpath. I don't want to package such xml file when producing the jar but I want to include a default xml file in a zip assembly under a conf subfolder and I also want this default xml to be available in the unit tests to test against it.
我有一个简单的 java maven 项目。我的一个类在执行时需要从类路径加载一个 xml 配置文件。我不想在生成 jar 时打包这样的 xml 文件,但我想在 conf 子文件夹下的 zip 程序集中包含一个默认的 xml 文件,我还希望此默认 xml 在单元测试中可用以对其进行测试。
As I see it there are 2 possible places of this default xml:
正如我所见,这个默认 xml 有 2 个可能的位置:
src/main/resources/conf/default.xml
src/main/conf/default.xml
src/main/resources/conf/default.xml
src/main/conf/default.xml
Both solutions demand special pom actions:
这两种解决方案都需要特殊的 pom 操作:
In solution 1, I get the auto copy to target folder during build which means it is available in testing but I also get it in the produced jar which i don't want.
In solution 2, I get the jar as I want it(free of the xml) but I manually have to copy the xml to the target folder to be available for testing. (I don't want to add src's subfolders in test classpath. I think it is bad practice).
在解决方案 1 中,我在构建过程中自动复制到目标文件夹,这意味着它可以在测试中使用,但我也在生成的 jar 中获取它,但我不想要它。
在解决方案 2 中,我得到了我想要的 jar(不含 xml),但我必须手动将 xml 复制到目标文件夹以供测试。(我不想在测试类路径中添加 src 的子文件夹。我认为这是不好的做法)。
Question: what is the best solution of the two?
- If the correct is 2, what is the best way to copy it to target folder?
- Is there any other solution better and more common than those two?
问题:两者的最佳解决方案是什么?
- 如果正确的是 2,将它复制到目标文件夹的最佳方法是什么?
- 有没有比这两个更好、更常见的其他解决方案?
(I also read Where should I put application configuration files for a Maven project?but I would like to know the most "correct solution" from the "convention over configuration" point of view and this link provides some configuration type solutions but not any convention oriented. Maybe there isn't one but I ask anyway. Also the solutions provided include AntRun plugin and appAssembler plugin and I wonder if I could do it with out them.)
(我还阅读了我应该将 Maven 项目的应用程序配置文件放在哪里?但我想从“约定优于配置”的角度了解最“正确的解决方案”,此链接提供了一些配置类型的解决方案,但没有任何约定面向。也许没有,但我还是问了。另外提供的解决方案包括 AntRun 插件和 appAssembler 插件,我想知道我是否可以不用它们。)
采纳答案by Pascal Thivent
The question is what is the best solution of the two? If the correct is 2, what is the best way to copy it to target folder? Is there any other solution better and more common than those two?
问题是两者的最佳解决方案是什么?如果正确的是 2,将它复制到目标文件夹的最佳方法是什么?有没有比这两个更好、更常见的其他解决方案?
Since you want that file to be copied to the target/classes
folder, it has somehow to be considered as a resource (so either put in under src/main/resources
or declare src/main/conf
as resource directory). And if you don't want it in the final jar, configure the Maven JAR Pluginto exclude it:
由于您希望将该文件复制到target/classes
文件夹中,因此必须以某种方式将其视为资源(因此要么放在资源目录下,src/main/resources
要么声明src/main/conf
为资源目录)。如果您不希望在最终 jar 中使用它,请配置Maven JAR 插件以排除它:
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<excludes>
<exclude>**/conf/*</exclude>
</excludes>
</configuration>
</plugin>
...
</plugins>
</build>
...
</project>
For the assembly part, assembly descriptors are pretty flexible so it should be possible to achieve what you want regardless of the choice. I'd suggest using the easiest setup though.
对于装配部件,装配描述符非常灵活,因此无论选择如何,都应该可以实现您想要的。不过,我建议使用最简单的设置。
回答by Peter Tillemans
You could place it in src/test/conf/default.xml. Your testclasses can find it, but it wont be packaged using the standard method.
你可以把它放在 src/test/conf/default.xml 中。您的测试类可以找到它,但不会使用标准方法对其进行打包。
With an additional assembly you can package it from there. That step is always necessary.
使用额外的组件,您可以从那里打包。这一步总是必要的。
A different solution could be to create a separate maven module and place it in /src/main/resources/conf/... .Then make this jar a test dependency. You do not need to do any special plugin configuration, but I think it is overkill for a single file.
一个不同的解决方案可能是创建一个单独的 maven 模块并将其放在 /src/main/resources/conf/... 中。然后将此 jar 设为测试依赖项。你不需要做任何特殊的插件配置,但我认为这对于单个文件来说是多余的。
回答by Asaf Mesika
My solution was to use two profiles: Development (default) and Packaging
我的解决方案是使用两个配置文件:开发(默认)和打包
My default / section contains both src/main/resources and src/main/conf. I call this my Development profile, which is an implicit profile.
我的默认 / 部分包含 src/main/resources 和 src/main/conf。我称之为我的开发配置文件,这是一个隐式配置文件。
My packaging profile is an explicit profile which is defined under section. There under / I only mentioned src/main/resources. When I'm running my packaging script (we currently have this external to maven since its building an RPM out of our WAR), I'm running 'mvn install -Drpm' to activate my Packaging profile (rpm is the id for the Packaging profile.
我的包装配置文件是在部分下定义的显式配置文件。在/下我只提到了src/main/resources。当我运行我的打包脚本时(我们目前有这个外部的 maven,因为它从我们的 WAR 中构建了一个 RPM),我正在运行“mvn install -Drpm”来激活我的打包配置文件(rpm 是打包的 id轮廓。
If this wasn't clear enough, feel free to ask more questions.
如果这还不够清楚,请随时提出更多问题。
回答by Voicu
If your packaging is war, you can use the packagingExcudesconfiguration option of the maven-war-plugin:
如果您的包装是war,您可以使用maven-war-plugin的PackagingExcudes配置选项:
<project>
...
<build>
<plugins>
...
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<!-- Exclude abc.properties found in src/main/resources/ (ends up getting packaged in WEB-INF/classes/) -->
<packagingExcludes>
WEB-INF/classes/abc.properties
</packagingExcludes>
</configuration>
</plugin>
...
</plugins>
</build>
...
</project>
Use commas to separate between multiple resources you want to exclude. Also, you can use wildcards and regex in your excluded paths. For regex, it's in the %regex[YOUR_REGEX_HERE]
syntax. Check the documentationfor more details.
使用逗号分隔要排除的多个资源。此外,您可以在排除的路径中使用通配符和正则表达式。对于正则表达式,它在%regex[YOUR_REGEX_HERE]
语法中。查看文档以获取更多详细信息。