Maven profile整合Spring profile

在Maven的pom.xml和Spring框架中,都有profile这个概念。profile是用于区分各类环境的,例如开发环境、测试环境、正式环境等。Maven的profile常常用于在打包时根据指定环境打入不一样的配置文件配置,如数据库配置。Spring的Profile能够用于在不一样的环境下加载不一样的bean,例如@Profile注解。本文介绍如何将两者整合。java

Spring启用一个profile

Spring启用某个profile有多种方式,为了便于整合Maven,这里经过web.xml方式启用:web

<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>dev</param-value>
</context-param>

在默认状况下,会启用Spring的dev profile,即开发环境。spring

Maven profile配置

在pom.xml中,能够配置test和product两个profile,分别对应测试环境和正式环境。这里也能够根据具体状况自定义。数据库

<profiles>
  <profile>
    <id>test</id>
    ...
  </profile>
  <profile>
    <id>product</id>
    ...
  </profile>
</profiles>

此时,运行mvn clean package -Ptest就会使用id为test的profile内的配置打包,mvn clean package -Pproduct就是用来打正式环境包的命令。app

web.xml内容替换

在web.xml中<param-value>dev</param-value>是指定Spring的profile,接下里要实现这个内容的替换,让mvn clean package -Ptest打包出来的web.xml变成<param-value>test</param-value>。这里可使用maven-antrun-plugin来实现字符串替换:框架

<profiles>
  <profile>
    <id>test</id>
    <build>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.8</version>
          <executions>
            <execution>
              <phase>compile</phase>
              <configuration>
                <target>
                  <!-- web.xml中的<param-value>dev</param-value>替换为<param-value>test</param-value> -->
                  <replace file="${basedir}/src/main/webapp/WEB-INF/web.xml"
                           token="&lt;param-value&gt;dev&lt;/param-value&gt;"
                           value="&lt;param-value&gt;test&lt;/param-value&gt;" />
                </target>
              </configuration>
              <goals>
                <goal>run</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
  </profile>
  <profile>
    <id>product</id>
    <build>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.8</version>
          <executions>
            <execution>
              <phase>compile</phase>
              <configuration>
                <target>
                  <!-- web.xml中的<param-value>dev</param-value>替换为<param-value>product</param-value> -->
                  <replace file="${basedir}/src/main/webapp/WEB-INF/web.xml"
                           token="&lt;param-value&gt;dev&lt;/param-value&gt;"
                           value="&lt;param-value&gt;product&lt;/param-value&gt;" />
                </target>
              </configuration>
              <goals>
                <goal>run</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>


以上的<replace file="" token="" value="" />中分别用于指定文件、待替换字符串、替换后的字符串。这里为何不写成token="dev"?考虑到”dev”这个字符串过短,可能会在web.xml中别的地方出现,防止误伤,因此这里会用token="&lt;param-value&gt;dev&lt;/param-value&gt;"webapp

以上配置加入到pom.xml中后,再经过mvn clean package -Ptestmvn clean package -Pproduct命令打包,打包好的war包解压出来,能够发现<param-value>dev</param-value>被替换成了<param-value>test</param-value><param-value>product</param-value>。此时便完成了Maven和Spring的profile的整合。maven

优化:打包时不修改src目录下的web.xml

若是使用mvn clean package -Ptestmvn clean package -Pproduct打包,会形成项目中的web.xml修改替换,此时若是忘了改回来,会形成一些问题,如再次打不一样环境的包就找不到<param-value>dev</param-value>来replace了,还有若是此时在开发中调试会误启用其余的Spring profile。测试

为了解决这个问题,咱们能够经过maven-antrun-plugin插件在replace前将web.xml拷贝一份出来,在拷贝出的web.xml文件上执行replace操做,打包时将拷贝出的web.xml打入war包,从而不影响原有的web.xml文件内容。优化

<profiles>
  <profile>
    <id>test</id>
    <properties>
      <!-- 使用target/web-xml中的web.xml文件打入war包 -->
      <maven.war.webxml>${basedir}/target/web-xml/web.xml</maven.war.webxml>
    </properties>
    <build>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.8</version>
          <executions>
            <execution>
              <phase>compile</phase>
              <configuration>
                <target>
                  <!-- 将web.xml拷贝一份到target/web-xml目录下 -->
                  <copy file="${basedir}/src/main/webapp/WEB-INF/web.xml"
                        todir="${basedir}/target/web-xml" />
                  <!-- 拷贝后的web.xml中的<param-value>dev</param-value>替换为<param-value>test</param-value> -->
                  <replace file="${basedir}/target/web-xml/web.xml"
                           token="&lt;param-value&gt;dev&lt;/param-value&gt;"
                           value="&lt;param-value&gt;test&lt;/param-value&gt;" />
                </target>
              </configuration>
              <goals>
                <goal>run</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
  </profile>
  <profile>
    <id>product</id>
    <properties>
      <!-- 使用target/web-xml中的web.xml文件打入war包 -->
      <maven.war.webxml>${basedir}/target/web-xml/web.xml</maven.war.webxml>
    </properties>
    <build>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.8</version>
          <executions>
            <execution>
              <phase>compile</phase>
              <configuration>
                <target>
                  <!-- 将web.xml拷贝一份到target/web-xml目录下 -->
                  <copy file="${basedir}/src/main/webapp/WEB-INF/web.xml"
                        todir="${basedir}/target/web-xml" />
                  <!-- 拷贝后的web.xml中的<param-value>dev</param-value>替换为<param-value>product</param-value> -->
                  <replace file="${basedir}/target/web-xml/web.xml"
                           token="&lt;param-value&gt;dev&lt;/param-value&gt;"
                           value="&lt;param-value&gt;product&lt;/param-value&gt;" />
                </target>
              </configuration>
              <goals>
                <goal>run</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>


为何不使用多个web.xml文件

以上是使用了一种web.xml文件字符串替换的方式来实现Maven和Spring的profile整合。另外还有一种配置多个web.xml文件的方式,即为每一个profile拷贝一份web.xml,修改相应spring.profiles.active的值,pom.xml中每一个profile指定对应的maven.war.webxml将相应的web.xml打入war包。若是项目须要常常修改web.xml文件内容,每次都要修改好几份web.xml文件会很是麻烦,因此不推荐使用。

相关文章
相关标签/搜索