比如你有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
原文地址:https://www.cnblogs.com/achengmu/p/10647662.html
时间: 2024-10-12 23:53:30