pom.xml配置,针对mvn clean install -P参数(环境参数)打包

Posted achengmu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pom.xml配置,针对mvn clean install -P参数(环境参数)打包相关的知识,希望对你有一定的参考价值。

pom.xml配置,针对mvn clean install -P参数(环境参数)打包

 

比如你有2个环境,一个dev,一个prod,

然后你在mvn打包的时候,可以通过-P来打包,是打dev包,还是prod包。

pom.xml可以这样配置

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>  //如果不带-P但是,默认是dev
            </activation>
        </profile>
        <profile>
            <id>devtest</id>
            <properties>
                <env>devtest</env>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <env>prod</env>
            </properties>
        </profile>
    </profiles>

  

-Pdev / -Pprod 的参数dev/prod,会根据传入的参数代入到具体的打包配置中

 <build>
        <finalName>xzair-rest</finalName>
        <scriptSourceDirectory>src/main/java</scriptSourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resources/${env}</directory>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>dev/*</exclude>
                    <exclude>devtest/*</exclude>
                    <exclude>prod/*</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <warSourceDirectory>src/main/webapp</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <webResources>
                        <resource>
                            <directory>src/main/resources/${env}</directory>
                            <targetPath>WEB-INF/classes</targetPath>
                            <filtering>true</filtering>
                        </resource>
                        <resource>
                            <directory>src/main/resources</directory>
                            <filtering>true</filtering>
                            <excludes>
                                <exclude>dev/*</exclude>
                                <exclude>devtest/*</exclude>
                                <exclude>prod/*</exclude>
                            </excludes>
                        </resource>
                        <resource>
                            <directory>${project.basedir}/src/main/resources/lib</directory>
                            <targetPath>WEB-INF/lib</targetPath>
                            <filtering>false</filtering>
                            <includes>
                                <include>**/*.jar</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <!-- 过滤后缀为pem、pfx的证书文件 -->
                    <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>pem</nonFilteredFileExtension>
                        <nonFilteredFileExtension>pfx</nonFilteredFileExtension>
                        <nonFilteredFileExtension>p12</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <path>/xzair-rest</path>
                    <charset>${project.build.sourceEncoding}</charset>
                    <port>${server.port}</port>
                    <uriEncoding>${project.build.sourceEncoding}</uriEncoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

  

cmd打包的时候可以这样打

mvn clean isntall -Pprod
或者
mvn clean install -Pdev

  

 

以上是关于pom.xml配置,针对mvn clean install -P参数(环境参数)打包的主要内容,如果未能解决你的问题,请参考以下文章

如何在eclipse中使用mvn clean install

记一次mvn clean install 报package com.rockysaas.shopguide.common.config.threadpool does not exist

为啥 Maven 命令“mvn sonar:sonar”在我的“pom.xml”中没有任何插件配置就可以工作?

缺包与maven

从npm 角度理解 mvn 的 pom.xml

mvn“No compiler is provided in this environment”报错解决方案