简体   繁体   English

从maven的资源目录中排除文件

[英]exclude file from maven's resource directory

I am using maven for my spring boot application(1.5 version). 我正在将Maven用于我的Spring Boot应用程序(1.5版本)。 There are some files in src/main/resources like abc.properties , app.json . src/main/resources有一些文件,例如abc.propertiesapp.json Below are some pointer what i want to achieve. 以下是我要实现的一些指针。

  1. Exclude these files getting into the jar. 排除这些文件进入jar的可能性。
  2. When i run my application through intellij these files should be available in classpath. 当我通过intellij运行我的应用程序时,这些文件应该在classpath中可用。

I looked at related answers on SO but none matches my case. 我在SO上查看了相关的答案,但没有一个与我的情况相符。 Any suggestion? 有什么建议吗?

you can use the resouce tag in maven pom file: 您可以在maven pom文件中使用resouce标签:

  <resources>
      <resource>
        <directory>[your directory]</directory>
        <excludes>
          <exclude>[non-resource file #1]</exclude>
          <exclude>[non-resource file #2]</exclude>
          <exclude>[non-resource file #3]</exclude>
          ...
          <exclude>[non-resource file #n]</exclude>
        </excludes>
      </resource>
      ...
    </resources>

For more informations see: https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html 有关更多信息,请参见: https : //maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html

My understanding is 我的理解是

  1. You want some config file, that is available in classpath during runtime 您需要一些配置文件,该文件在运行时可在类路径中使用
  2. Such config file will be changed based on environment 这样的配置文件将根据环境而改变

The way I used to do is: 我以前做的方法是:

  1. Create a separate directory for such kind of resources, eg src/main/appconfig/ 为此类资源创建一个单独的目录,例如src/main/appconfig/
  2. Do NOT include this in POM's resources (ie they are not included in resulting JAR) 请勿在POM的资源中包含此内容(即,它们不包含在结果JAR中)
  3. In IDE, add this directory manually as source folder (I usually put this in testResource in POM. When I use Eclipse + M2E, testResource s will be added as source folder. Not sure how IntelliJ though) 在IDE中,手动将此目录添加为源文件夹(我通常将其放在POM的testResource中。当我使用Eclipse + M2E时, testResource s将被添加为源文件夹。虽然不确定IntelliJ的方式)

For point 2, I used to do it slightly differently. 对于第2点,我过去所做的略有不同。 Instead of excluding, I will include in the result JAR but in a separate directory (eg appconfig-template), so that people using the application can take this as reference to configure their environment. 除了将其排除在外,我还将在结果JAR中包括一个单独的目录(例如,appconfig-template),以便使用该应用程序的人可以以此为参考来配置其环境。

An alternative of 3 is: create a separate profile which include appconfig as resource . 3的替代选择是:创建一个单独的配置文件,其中将appconfig作为resource Use this profile only for your IDE setup but not building your artifact. 仅将此概要文件用于IDE设置,而不能构建工件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM