使用 maven 为每个环境定制 context.xml

Dan*_*ier 1 java context.xml pom.xml maven

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

context.xml.development context.xml.production

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

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

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

Tom*_*ain 5

另一种方法(如果您没有考虑)是使用一个带有占位符的 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>
Run Code Online (Sandbox Code Playgroud)

然后,将 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>
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)