我应该在maven项目的路径中放置不被视为资源的配置文件 [英] Where in maven project's path should I put configuration files that are not considered resources

查看:157
本文介绍了我应该在maven项目的路径中放置不被视为资源的配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的java maven项目。执行时我的一个类需要从类路径加载xml配置文件。我不想在生成jar时打包这样的xml文件,但我想在conf子文件夹下的zip程序集中包含一个默认的xml文件,我也希望在单元测试中可以使用这个默认的xml来测试它。

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.

正如我所看到的,这个默认的xml有2个可能的位置:

As I see it there are 2 possible places of this default xml:


  1. src / main / resources / conf / default.xml

  2. src / main / conf / default。 xml

  1. src/main/resources/conf/default.xml
  2. src/main/conf/default.xml

两种解决方案都需要特殊的pom动作:

Both solutions demand special pom actions:


  • 在解决方案1中,我在构建期间获得了自动复制到目标文件夹,这意味着它可以在测试中使用,但我也可以在我不想要的生产的jar中获取它。

  • 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.

在解决方案2中,我得到了我想要的jar(没有xml)但我手动必须将xml复制到目标文件夹中可供测试。 (我不想在测试类路径中添加src的子文件夹。我认为这是不好的做法。)

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).

问题:两者的最佳解决方案是什么?

- 如果正确的是2,那么将它复制到目标文件夹的最佳方法是什么?

- 有没有比这两个更好更常见的解决方案?

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?

(我也读过我应该在哪里放置Maven项目的应用程序配置文件?但我想知道最正确的解决方案从约定优于配置的角度来看,这个链接提供了一些配置类型解决方案,但没有任何面向约定。也许没有一个,但我还是要求。还提供的解决方案包括AntRun插件和appAssembler插件,我想知道是否我可以完成它们。)

(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.)

推荐答案


问题是这两者的最佳解决方案是什么?如果正确为2,那么将其复制到目标文件夹的最佳方法是什么?有没有比这两个更好更常见的解决方案呢?

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?

因为你想把那个文件复制到目标/类文件夹,它以某种方式被视为资源(因此要么放在 src / main / resources 下面,要么声明 src / main / conf 作为资源目录)。如果您不想在最终的jar中使用它,请配置 Maven JAR插件排除它:

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 Plugin to exclude it:

<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.

这篇关于我应该在maven项目的路径中放置不被视为资源的配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆