【发布时间】:2012-10-12 00:00:26
【问题描述】:
我有一个 web.xml 文件,我想将属性文件中的多个值读入 web.xml。这可能吗? .如果是,如何?我可以在 java 类本身中访问这个属性文件吗?我专门使用 ant 来构建我的项目。对此的任何帮助表示赞赏。
【问题讨论】:
标签: xml tomcat ant gradle properties-file
我有一个 web.xml 文件,我想将属性文件中的多个值读入 web.xml。这可能吗? .如果是,如何?我可以在 java 类本身中访问这个属性文件吗?我专门使用 ant 来构建我的项目。对此的任何帮助表示赞赏。
【问题讨论】:
标签: xml tomcat ant gradle properties-file
您可以使用占位符来做到这一点。 例如(使用 Gradle):
gradle.properties:
myProp1=abc
myProp2=def
build.gradle:
war {
eachFile {
if (it.name == 'web.xml') {
it.filter {String line -> line.replaceAll('\\$\\{textToReplace1\\}', myProp1) }
it.filter {String line -> line.replaceAll('\\$\\{textToReplace2\\}', myProp2) }
}
}
}
【讨论】:
properties.load(Connector.class.getResourceAsStream("path/to/properties/file"));
this.DRIVER_CLASS = properties.getProperty("attribute/mentioned/in/the/property/file");
this.DATABASE_URL = properties.getProperty("another/attribute/mentioned/in/the/property/file");
this.databaseName = properties.getProperty("other/attribute/mentioned/in/the/property/file");
this.username = properties.getProperty("other/attribute/mentioned/in/the/property");
And so on...
属性文件的格式与上面“Cengiz”显示的格式相同。 这里 this.variables 是我的私有变量,可以是任何东西。 此外,属性文件需要位于类的顶层层次结构中。
这解决了我的疑问。 谢谢你的帮助。
【讨论】: