将.properties文件中的值设置为web.xml

时间:2012-10-22 07:44:20

标签: xml tomcat ant gradle properties-file

我有一个web.xml文件,我想从属性文件中读取多个值到web.xml中。这可能吗? 。如果是的话,怎么样?。我可以在java类中访问这个属性文件吗?我特意使用ant来构建我的项目。对此有任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:3)

您可以使用占位符执行此操作。 例如(使用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) }
        }
    }
}

答案 1 :(得分:0)

    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是我的私有变量,可以是任何东西。 此外,属性文件需要位于层次结构的顶层。

这解决了我的查询。 谢谢你的帮助。