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

标签 java maven pom.xml context.xml

我的 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/

相关文章:

java - 如何在回合制游戏中存储游戏棋子的路径?

java - 删除 Android Studio 中的调试日志记录

java - 出于商业目的在哪里发布可用的 maven 存储库中的 Artifact ?

android - 无法解析配置 ':flutter_local_notifications:classpath'的所有 Artifact

java - eclipse 之外的 maven java 构建错误

java - 默认情况下,自签名证书和 CN 中带有通配符的证书是否在 Java 中被阻止?

java - 所有类的类声明、构造函数和 toString 方法

maven - 从 Maven 原型(prototype)生成模块时访问父属性

java - tools.jar问题无法执行目标org.apache.maven.plugins :maven-compiler-plugin

angular - 如何使用 maven-exec-plugin 进行 npm clean?