简体   繁体   English

我应该在maven项目的路径中放置不被视为资源的配置文件

[英]Where in maven project's path should I put configuration files that are not considered resources

I have a simple java maven project. 我有一个简单的java maven项目。 One of my classes when executing needs to load an xml configuration file from the classpath. 执行时我的一个类需要从类路径加载xml配置文件。 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. 我不想在生成jar时打包这样的xml文件,但我想在conf子文件夹下的zip程序集中包含一个默认的xml文件,我也希望在单元测试中可以使用这个默认的xml来测试它。

As I see it there are 2 possible places of this default xml: 在我看来,这个默认的xml有两个可能的位置:

  1. src/main/resources/conf/default.xml
  2. 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. 在解决方案1中,我在构建期间将自动复制到目标文件夹,这意味着它在测试中可用,但我也在生产的jar中得到它,我不想要它。

  • 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. 在解决方案2中,我得到了我想要的jar(没有xml)但我手动必须将xml复制到目标文件夹以供测试。 (I don't want to add src's subfolders in test classpath. I think it is bad practice). (我不想在测试类路径中添加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? - 如果正确为2,将其复制到目标文件夹的最佳方法是什么?
- Is there any other solution better and more common than those two? - 有没有比这两个更好,更常见的其他解决方案?

(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插件,我想知道我是否可以完成它们。)

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? 如果正确为2,那么将其复制到目标文件夹的最佳方法是什么? Is there any other solution better and more common than those two? 有没有比这两个更好,更常见的其他解决方案?

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). 由于您希望将该文件复制到target/classes文件夹,因此它必须以某种方式被视为资源(因此要么放在src/main/resources下面,要么将src/main/conf声明为资源目录)。 And if you don't want it in the final jar, configure the Maven JAR Plugin to exclude it: 如果您不想在最终的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. 我建议使用最简单的设置。

My solution was to use two profiles: Development (default) and Packaging 我的解决方案是使用两个配置文件:开发(默认)和打包

My default / section contains both src/main/resources and src/main/conf. 我的默认/部分包含src / main / resources和src / main / conf。 I call this my Development profile, which is an implicit profile. 我将此称为我的开发配置文件,这是一个隐式配置文件。

My packaging profile is an explicit profile which is defined under section. 我的包装资料是一个明确的资料,在章节下定义。 There under / I only mentioned src/main/resources. / /我只提到了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. 当我运行我的打包脚本时(我们现在将这个外部设置为maven,因为它构建了我们的WAR),我正在运行'mvn install -Drpm'来激活我的包装配置文件(rpm是包装的id轮廓。

If this wasn't clear enough, feel free to ask more questions. 如果这还不够清楚,请随时提出更多问题。

You could place it in src/test/conf/default.xml. 您可以将它放在src / test / conf / default.xml中。 Your testclasses can find it, but it wont be packaged using the standard method. 您的测试类可以找到它,但不会使用标准方法打包。

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. 一个不同的解决方案可能是创建一个单独的maven模块并将其放在/ src / main / resources / conf / ...中。然后使这个jar成为测试依赖。 You do not need to do any special plugin configuration, but I think it is overkill for a single file. 您不需要执行任何特殊的插件配置,但我认为对于单个文件来说它是过度的。

If your packaging is war , you can use the packagingExcudes configuration option of the maven-war-plugin : 如果您的包装是战争 ,您可以使用maven-war-pluginpackagingExcudes配置选项:

<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. 对于正则表达式,它使用%regex[YOUR_REGEX_HERE]语法。 Check the documentation for more details. 查看文档以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该将IDL文件和生成的代码放在Maven项目的哪里? - Where should I put IDL files and generated code in Maven project? 我应该在哪里将配置文件放在Web服务中 - where should i put the configuration files in a webservice 我应该将主要实现放在多模块Maven项目中的哪个位置? - Where should I put the main implementation in a multi module Maven project? 我应该在Maven中的Web(WAR)项目中添加Java文件? - Where should I add Java files in web (WAR) project in Maven? 我应该将 SQL 文件放在我的 Java 项目中的什么位置? - Where should I put the SQL files in my Java project? Eclipse动态Web项目:我应该在哪里放置资源文件 - Eclipse dynamic web project : where should I put resource files 我应该将我的Maven jar依赖项之一所需的配置文件放在哪里? - Where should I put a configuration file needed by one of my maven jar dependency? Maven项目不会将文件放在classpath的src / main / resources中 - Maven project doesn't put files in src/main/resources on classpath 我应该在Maven项目的n层体系结构中将Spring MVC配置类编写在哪里? - Where should I write my Spring MVC configuration class in a n-tiers architecture in a Maven project? 我应该在web应用程序(maven项目)中将java.util.logging的logging.properties文件放在哪里? - Where should I put logging.properties file for java.util.logging in web application (maven project)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM