【问题标题】:Customized context.xml per environment using maven使用 maven 为每个环境定制 context.xml
【发布时间】:2012-01-19 18:49:20
【问题描述】:

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

context.xml.development context.xml.production

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

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

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

【问题讨论】:

    标签: java maven pom.xml context.xml


    【解决方案1】:

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

    【讨论】:

    • 我不清楚这里使用了哪些文件。属性(包括密码)是否应该进入 pom.xml 文件?
    【解决方案2】:

    maven build profiles 和 maven-war-plugin 的组合使用文件过滤来包含或排除正确的文件应该可以解决问题。

    例如,像这样:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <groupId>com.mycompany</groupId>
    <artifactId>myproject</artifactId>
    <profiles>
        <profile>
            <id>custom-profile</id>
            <activation>
                <property>
                    <name>environment</name>
                    <value>production</value>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>2.1.1</version>
                        <configuration>
                            <webResources>
                                <resource>
                                    <directory>resources</directory>
                                    <excludes>
                                        <exclude>context.xml.development</exclude>
                                    </excludes>
                                </resource>
                            </webResources>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>custom-profile2</id>
            <activation>
                <property>
                    <name>environment</name>
                    <value>development</value>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>2.1.1</version>
                        <configuration>
                            <webResources>
                                <resource>
                                    <directory>resources</directory>
                                    <excludes>
                                        <exclude>context.xml.production</exclude>
                                    </excludes>
                                </resource>
                            </webResources>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    

    那么当你运行 maven 时,一定要适当地发送“环境”属性:

    mvn clean install -Denvironment=production

    【讨论】:

    • 这将创建一个 context.xml 文件(不带 . 后缀)?
    • 不,我没有意识到这是必要的。最好的办法可能是将每个文件放在不同的目录中并将文件命名为“context.xml”,然后使用 maven 配置文件选择您想要的。
    猜你喜欢
    • 1970-01-01
    • 2022-01-07
    • 2021-11-17
    • 2013-01-17
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 2011-07-27
    • 2012-10-04
    相关资源
    最近更新 更多