简体   繁体   English

Maven:如何将资源复制到WAR中?

[英]Maven: how to copy resources into WAR?

I have an Eclipse webapp project that I am trying to migrate from Ant build to Maven build. 我有一个Eclipse Webapp项目,我正在尝试从Ant构建迁移到Maven构建。 I have a lot of non-Java files in my src/ folder, for example log4j2.xml , ehcache.xml , some .properties localization files etc. 我的src/文件夹中有很多非Java文件,例如log4j2.xmlehcache.xml以及一些.properties本地化文件等。

When I run war:war target on this project, the resulting WAR file contains only the .class files in the WEB-INF/classes folder, all the non-Java files are missing. 当我在该项目上运行war:war目标时,生成的WAR文件仅包含WEB-INF/classes文件夹中的.class文件,所有非Java文件都丢失了。 In Ant I did this: 在Ant中,我这样做是:

<target name="copy-resources" depends="compile">
  <copy todir="${build}">
    <fileset dir="${src}">
      <exclude name="**/*.java"/>
    </fileset>
  </copy>
</target>

How do I achieve the same thing in Maven? 我如何在Maven中实现同一目标? I suspect I should be using the resources folder, but right now I am trying to migrate with as little changes to the original codebase as possible... 我怀疑我应该使用resources文件夹,但现在我正尝试以对原始代码库的尽可能少的更改进行迁移...

You can access these files from resources folder. 您可以从资源文件夹访问这些文件。 I hope this link helps. 希望此链接对您有所帮助。

You can also specify the folder, if it is not the default resources folder, in the pom.xml. 如果不是默认资源文件夹,也可以在pom.xml中指定该文件夹。 See Specifying resource directories . 请参阅指定资源目录

For example, if your folder has these files in src/my-resources, then you need to add this to the pom.xml, and then you would be able access it in code. 例如,如果您的文件夹在src / my-resources中包含这些文件,则需要将其添加到pom.xml中,然后就可以通过代码访问它了。

   <resources>
     <resource>
       <directory>src/my-resources</directory>
     </resource>
   </resources>

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

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