Maven环境隔离

Posted by Chenyawei on 2020-03-16
Words 309 and Reading Time 1 Minutes
Viewed Times
  1. 解决的实际问题

    避免人工修改的弊端,即容易犯错;

    轻松分环境编译、打包、部署;

  2. 配置及原理

    pom.xml 中build节点内增加resources节点

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <!--build中添加Maven环境隔离配置-->
    <resources>
    <resource>
    <directory>src/main/resources.${deploy.type}</directory>
    <excludes>
    <exclude>*.jsp</exclude>
    </excludes>
    </resource>
    <resource>
    <directory>src/main/resources</directory>
    </resource>
    </resources>

    pom.xml 中增加profiles节点

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    <!--Maven环境隔离配置,与build节点同级的节点-->
    <profiles>
    <profile>
    <id>dev</id>
    <activation>
    <!--默认激活的节点-->
    <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
    <deploy.type>dev</deploy.type>
    </properties>
    </profile>
    <profile>
    <id>beta</id>
    <properties>
    <deploy.type>beta</deploy.type>
    </properties>
    </profile>
    <profile>
    <id>prod</id>
    <properties>
    <deploy.type>prod</deploy.type>
    </properties>
    </profile>
    </profiles>
  3. Maven环境隔离编译打包命令

    1
    2
    3
    mvn clean package -Dmaven.test.skip=true -Pdev
    mvn clean package -Dmaven.test.skip=true -Pbeta
    mvn clean package -Dmaven.test.skip=true -Pprod
  4. 新建对应的文件夹,并把要隔离的文件分开,公共的留下


notice

欢迎访问 chenyawei 的博客, 若有问题或者有好的建议欢迎留言,笔者看到之后会及时回复。 评论点赞需要github账号登录,如果没有账号的话请点击 github 注册, 谢谢 !

If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !