如何使用 Maven 配置文件创建引用质量和生产系统数据源的 war 文件?

How to create war file which refers quality and production system datasource,Using maven profiles?

这是一个简单的服务应用程序,它创建 war 文件并将其部署在 CAT 应用程序中。 1.Using maven 配置文件,war 文件是使用代码的生成器:

 -1.1.Development environment @ my laptop, mvn <package> -P development 
 -1.2.Quality environment @ my laptop, mvn <package> -P quality 
 -1.3.Production environment @ production server, mvn <package> -P production

pom.xml:

<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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.bosch.bgn</groupId>
    <artifactId>newsbox</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>newsbox</name>
    <build>
        <finalName>newsbox</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <inherited>true</inherited>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <!-- Run the application using "mvn embedded-glassfish:run" -->
            <plugin>
                <groupId>org.glassfish</groupId>
                <artifactId>maven-embedded-glassfish-plugin</artifactId>
                <version>${glassfish.version}</version>
                <configuration>
                    <goalPrefix>embedded-glassfish</goalPrefix>
                    <app>D:\Java\workspace/target/${project.build.finalName}.war</app>
                    <autoDelete>true</autoDelete>
                    <port>8080</port>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>com.sun.jersey</groupId>
                        <artifactId>jersey-servlet</artifactId>
                        <version>${project.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>javax.servlet</groupId>
                        <artifactId>javax.servlet-api</artifactId>
                        <version>3.0.1</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
        <!-- Instructing the resources plugin to filter certain directories -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-servlet</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>${jersey.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>
    <properties>
        <jersey.version>1.17.1</jersey.version>
        <glassfish.version>3.1.1</glassfish.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <!-- Profiles start here -->
    <profiles>
        <!-- Development environment @ my laptop, mvn <package> -P development -->
        <profile>
            <id>development</id>
            <properties>
                <db.driverClass>oracle.jdbc.driver.OracleDriver</db.driverClass>
                <db.connectionURL>.....</db.connectionURL>
                <db.username>wf</db.username>
                <db.password>....</db.password>
            </properties>
        </profile>
        <!-- Development environment @ my laptop, mvn <package> -P quality -->
        <profile>
            <id>quality</id>
            <properties>
                <db.driverClass>oracle.jdbc.driver.OracleDriver</db.driverClass>
                <db.connectionURL>......</db.connectionURL>
                <db.username>webaccess</db.username>
                <db.password>....</db.password>
            </properties>
        </profile>
        <!-- Production environment @ production server, mvn <package> -P production -->
        <profile>
            <id>production</id>
            <properties>
                <db.driverClass>oracle.jdbc.driver.OracleDriver</db.driverClass>
                <db.connectionURL>.....</db.connectionURL>
                <db.username>webaccess</db.username>
                <db.password>....</db.password>
            </properties>
        </profile>
    </profiles>
</project>

谢谢。

这将为开发配置文件创建 war 文件:

mvn clean install -Pdevelopment

同样适用于:

mvn clean install -Pquality

mvn clean install -Pproduction

感谢您的回复。

  • 我曾经使用像-mvn package -P quality 这样的命令构建war 文件。
  • 它将创建一个 war 文件,其中包含质量系统的数据库详细信息 详细信息已复制到 .properties 文件扩展名。
  • 现在我将通过上传 WAR 通过一些工具启用 Q 部署 文件进入系统。
  • 部署完成后状态为“Q 部署完成 启动 P”。(这是一个可点击的按钮)
  • 现在如果我点击按钮 war 文件将部署在 P 系统中。 但是DB是Quality pointing one for P.
  • 现在,如果我访问该应用程序,系统将无法工作。(Q DB 详细信息在 P war 文件 db.properties 中)

如何解决这个问题???