1

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

context.xml.development context.xml.production

我使用 Maven 战争插件来构建它。

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

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

4

2 回答 2

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>

然后,将 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>
于 2015-08-26T11:14:02.247 回答
1

maven 构建配置文件和 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

于 2012-01-19T20:13:17.783 回答