gpt4 book ai didi

java - 使用 Maven 为每个环境定制 context.xml

转载 作者:行者123 更新时间:2023-11-29 08:09:37 24 4
gpt4 key购买 nike

我的 Java web 项目中有两个 context.xml 文件:

context.xml.development 上下文.xml.生产

我使用 maven war 插件来构建它。

当我构建项目时,我希望 maven 将正确的 context.xml 复制到 META-INF 目录。

我该怎么做?我已经在我的 pom.xml 中使用配置文件

最佳答案

另一种方法(以防您没有考虑过)是使用一个带占位符的 context.xml 文件。例如:

<Context>
<Resource name="jdbc/syncDB" auth="Container" type="javax.sql.DataSource"
maxTotal="100" maxIdle="30" maxWaitMillis="10000"
username="${database.username}" password="${database.password}" driverClassName="oracle.jdbc.OracleDriver"
url="${database.url}"/>

</Context>

然后,将 war 插件添加到您的 maven pom.xml 文件中,该文件具有 META-INF 文件夹作为过滤资源:

    <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>src/main/webapp/META-INF</directory>
<filtering>true</filtering>
<targetPath>META-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>

这样,您就可以将这些占位符值定义为可以针对特定配置文件覆盖的默认值:

<properties>
<database.password>default_user</database.password>
<database.username>default_password</database.username>
<database.url>jdbc:oracle:thin:@oracle.host:1521:defaultsid</database.url>
</properties>

<profiles>
<profile>
<id>dev</id>
<properties>
<database.url>jdbc:oracle:thin:@oracle.host:1521:DEVELOPMENTsid</database.url>
</properties>
</profile>
</profiles>

关于java - 使用 Maven 为每个环境定制 context.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8931585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com