简体   繁体   English

Maven配置文件,使用休眠配置文件进行资源过滤

[英]Maven Profiles, Resource Filtering with hibernate configuration file

I am trying to instantiate hibernate configuration file using maven profiles. 我正在尝试使用Maven配置文件实例化休眠配置文件。 My config file resides under src/main/resources and in the pom file I marked that folder to be used in resource filtering. 我的配置文件位于src/main/resources ,在pom文件中,我将该文件夹标记为要在资源过滤中使用。 Interestingly I can load the same properties from properties file whereas hibernate throws exception while parsing config file. 有趣的是,我可以从属性文件中加载相同的属性,而hibernate在解析配置文件时会抛出异常。 Here is the code samples I am using. 这是我正在使用的代码示例。

POM - Resource Filtering POM-资源过滤

<resources>
    <resource>
         <directory>src/main/resources</directory>
         <filtering>true</filtering>
    </resource>
</resources>

File structure 档案结构

src/main/resources/hibernate.cfg.xml

src/main/resources/hibernate.properties

Maven dev profile Maven开发人员资料

<profile>
    <id>dev</id>
    <properties>
            <hibernate.connection.url>jdbc:mysql://localhost:3306/example_schema?zeroDateTimeBehavior=convertToNull&amp;UseUnicode=true&amp;characterEncoding=utf8</hibernate.connection.url>
    </properties>
</profile>

Hibernate config file sample Hibernate配置文件示例

<property name="hibernate.connection.url">${hibernate.connection.url}</property>

Properties file sample 属性文件样本

hibernate.config.file = ${hibernate.config.file}

When I run my application I see that the properties file is filled with correct values; 当我运行我的应用程序时,我看到属性文件中填充了正确的值。 but hibernate config file throws following parse exceptions. 但是休眠配置文件会引发以下解析异常。

Caused by: org.dom4j.DocumentException: Error on line 1 of document : Content is not allowed in prolog. Nested exception: Content is not allowed in prolog. at org.dom4j.io.SAXReader.read(SAXReader.java:482) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2155)

and from same stack trace 并从同一堆栈跟踪

The reference to entity "UseUnicode" must end with the ';' delimiter. Nested exception: The reference to entity "UseUnicode" must end with the ';' delimiter.

But in the pom I already escape & with &amp; 但在POM我已经逃避&&amp; . When I remove query params from url it works as I expect, how can I escape ampersand from pom file? 当我从url中删除查询参数时,它可以按预期运行,如何从pom文件中删除&符号?

I am not really sure its going to solve your problem. 我不确定它会解决您的问题。 But, you can look into the following URLs:- http://maven.apache.org/guides/introduction/introduction-to-profiles.html 但是,您可以查看以下URL:-http: //maven.apache.org/guides/introduction/introduction-to-profiles.html

OR this:- 或这个:-

http://www.manydesigns.com/en/portofino/portofino3/tutorials/using-maven-profiles-and-resource-filtering http://www.manydesigns.com/zh-CN/portofino/portofino3/tutorials/using-maven-profiles-and-resource-filtering

its been explained beautifully. 它被很好地解释了。 I don't have access to comment that is the reason i am answering. 我无权发表评论,这就是我要回答的原因。

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

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