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

时间:2014-09-16 10:49:37

标签: java ant build.xml

我正在使用ant构建我的网络应用程序。我试图从源文件夹中包含WEB-INF文件夹中的属性文件。我把它包含在war / WEB-APP / classes文件夹中。但应用程序并没有阅读它。因此,我想直接将它包含在WEB-INF文件夹中,以便在应用程序中读取它。 我尝试过以下但似乎没有任何效果。我的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>

我试图使用:

  1. &#34; include name =&#34; $ {war.dir} /GroupQuoteUI.properties"在&#34; webinf&#34;标签,但确实有效。

  2. 还包括=&#34; $ {war.dir} /GroupQuoteUI.properties"在标签内。

  3. 此内部&#34; webinf&#34;文件夹:

    &#34; zipfileset dir =&#34; $ {basedir} / war /&#34;包括=&#34; GroupQuoteUI.properties&#34; FULLPATH =&#34; $ {webinf.dir} /GroupQuoteUI.properties"

  4. 但是在构建声明期间出现错误&#34;不能同时使用src dir&#34;。

    那么我应该怎么做才能将这个文件包含在战争的WEB-INF目录中。包含所有其他目录和web.xml文件。

1 个答案:

答案 0 :(得分:0)

您无法通过

访问包含在战争或jar中的文件
new FileInputStream()

相反,您可以执行以下操作:

this.getClass().getResourceAsStream(filename)

这将从类路径加载资源(您的属性文件),因此它将读取war-archive中的文件。 它通过使用相同的ClasseLoader来实现这一点,ClasseLoader用于加载属于 this.getClass()的类

在这里你可以找到一个例子: How to really read text file from classpath in Java