Maven部署多个环境

现实中一个项目往往需要部署到多个环境,如生产环境,测试环境等,但是各个环境的配置会有所不同.如果项目用到了maven可以使用properties-maven-plugin插件解决这个问题.

1.目录

Maven部署多个环境_第1张图片

2.配置

在pom.xml里

添加profiles

<profiles>
    <!-- 默认使用dev配置打包 命令 mvn package -->
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <environment>dev</environment>
        </properties>
    </profile>

    <!--生产环境 mvn package -Pproduction-->
    <profile>
        <id>production</id>
        <properties>
            <environment>production</environment>
        </properties>
    </profile>
</profiles>

添加plugin

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-2</version>
    <executions>
        <execution>
            <id>default-cli</id>
            <phase>initialize</phase>
            <goals>
                <goal>read-project-properties</goal>
            </goals>
            <configuration>
                <files>
                    <file>${basedir}/profiles/${environment}.properties</file>
                </files>
            </configuration>
        </execution>
    </executions>
</plugin>

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.test</groupId>
    <artifactId>test</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>com.test.properties</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <profiles>
        <!-- 默认使用dev配置打包 命令 mvn package -->
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <environment>dev</environment>
            </properties>
        </profile>

        <!--生产环境 mvn package -Pproduction-->
        <profile>
            <id>production</id>
            <properties>
                <environment>production</environment>
            </properties>
        </profile>
    </profiles>

    <build>
        <finalName>${project.artifactId}-${project.version}</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.0-alpha-2</version>
                <executions>
                    <execution>
                        <id>default-cli</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <files>
                                <file>${basedir}/profiles/${environment}.properties</file>
                            </files>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

dev.properties

log.level=DEBUG

production.properties

log.level=ERROR

log.properties

level=${log.level}

3.测试

使用以下命令:

mvn package

生成的war文件log.properties的内容为level=DEBUG

mvn package -Pproduction

生成的war文件log.properties的内容为level=ERROR

你可能感兴趣的:(Maven部署多个环境)