java 如何将包含属性文件的目录添加到 Ant build.xml?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12007868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:14:28  来源:igfitidea点击:

How to add a directory containing properties files to an Ant build.xml?

javaantbuild

提问by espeed

I'm trying to build an "uberjar" with ant, and the project has a config directory with several properties files.

我正在尝试使用 ant 构建一个“uberjar”,并且该项目有一个包含多个属性文件的 config 目录。

How do I add the config dir to build.xml?

如何将配置目录添加到 build.xml?

Here's an example of how it's referenced:

这是如何引用它的示例:

static private String CONFIG_DIR = "config/";
static public String CONFIG_FILE = "proj.properties";

File configFile = new File(CONFIG_DIR, CONFIG_FILE);

There are multiple properties files in the config directory. Normally I would just add it to the classpath:

config 目录中有多个属性文件。通常我会把它添加到类路径中:

java -classpath bin:lib/*:config some.project.Example

Here's my build.xml target, which isn't quite right:

这是我的 build.xml 目标,不太正确:

<target name="uberjar" depends="compile">
  <jar jarfile="${babelnet.jar}">
    <fileset dir="${bin.dir}">
      <include name="**/*.class"/>
    </fileset>
    <zipgroupfileset dir="lib" includes="*.jar"/>
  </jar>
  <copy todir="config">
    <fileset dir="config">
      <include name="**/*.properties"/>
    </fileset>
      </copy>
</target>

回答by Christopher Peisert

To include the properties files within the new jar file, you could add another nested <fileset>to the <jar>task.

要将属性文件包含在新 jar 文件中,您可以<fileset><jar>任务添加另一个嵌套文件。

<target name="uberjar" depends="compile">
  <jar jarfile="${babelnet.jar}">
    <fileset dir="${bin.dir}">
      <include name="**/*.class"/>
    </fileset>
    <zipgroupfileset dir="lib" includes="*.jar"/>
    <fileset dir="${basedir}">
      <include name="config/*.properties"/>
    </fileset>
  </jar>
</target>

Also, see stackoverflow question: How to read a file from a jar file?

另外,请参阅 stackoverflow 问题:How to read a file from a jar file?