简体   繁体   English

如何在战争的WEB-INF文件夹中包含属性文件

[英]How to include a property file in WEB-INF folder of war

I am using ant to build my web-app. 我正在使用ant构建我的网络应用。 I am trying to include a property file in the WEB-INF folder from a source folder. 我试图在源文件夹的WEB-INF文件夹中包含一个属性文件。 I have included it in the war/WEB-APP/classes folder. 我已将其包含在war / WEB-APP / classes文件夹中。 But the application is not reading it. 但是应用程序没有读取它。 Hence, i want to include it in the WEB-INF folder directly to read it in the application. 因此,我想将其直接包含在WEB-INF文件夹中以在应用程序中读取它。 I have tried the following but nothing seems to work. 我尝试了以下方法,但似乎无济于事。 my build.xml looks like this : 我的build.xml看起来像这样:

    <target name="build-dev" description="Package GWT app to web archive and deploy to web server">
   <echo message="Package GWT app to web archive" />
   <copy toDir="${basedir}/war/WEB-INF/lib">
      <fileset dir="${basedir}/lib" includes="*.jar" />
      <fileset dir="${gwt.home}" includes="*.jar" />
   </copy>
   <copy todir="${basedir}/war" file="${basedir}/src/etc/dev/GroupQuoteUI.properties" />
   <war basedir="${war.dir}" destfile="${deploy.dir}/${app.name}.war" webxml="${webinf.dir}/web.xml">
      <webinf dir="${webinf.dir}/">
         <include name="*." />

         <exclude name="**/web.xml" />
      </webinf>

      <classes dir="${basedir}/src/etc/dev/" includes="*.properties" />
   </war>
</target>

i have tried to use : 我试图使用:

  1. "include name="${war.dir}/GroupQuoteUI.properties" in "webinf" tag but it did'nt worked. “ webinf”标记中的“ include name =“ $ {war.dir} /GroupQuoteUI.properties”,但没有用。

  2. Also includes="${war.dir}/GroupQuoteUI.properties" inside the tag. 标记内还包含=“ $ {war.dir} /GroupQuoteUI.properties”。

  3. Also this inside "webinf" folder again : 同样在“ webinf”文件夹中:

    "zipfileset dir="${basedir}/war/" includes="GroupQuoteUI.properties" fullpath="${webinf.dir}/GroupQuoteUI.properties" “ zipfileset dir =” $ {basedir} / war /“ includes =” GroupQuoteUI.properties“ fullpath =” $ {webinf.dir} /GroupQuoteUI.properties“

but this is giving an error during build stating "cannot have src dir together". 但这在构建过程中给出了一个错误,指出“不能一起拥有src目录”。

So what should i do to include this file in the WEB-INF directory of the war. 因此,我应该怎么做才能将此文件包含在战争的WEB-INF目录中。 All other directories and web.xml file is included. 包括所有其他目录和web.xml文件。

You cannot read a file that is packed into a war, or jar by accessing it with 您无法通过以下方式访问打包为war或jar的文件:

new FileInputStream()

Instead you can do the following: 相反,您可以执行以下操作:

this.getClass().getResourceAsStream(filename)

this will load a resource (your properties-file) from the classpath, so it will read the file in a war-archive. 这将从类路径中加载资源(您的属性文件),因此它将在war归档中读取该文件。 It does this by using the same ClasseLoader which has been used to load the class that belongs to this.getClass() 它使用与加载属于this.getClass()的类相同的ClasseLoader来实现此目的。

Here you can find an example: How to really read text file from classpath in Java 在这里,您可以找到一个示例: 如何从Java中的类路径真正读取文本文件

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

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