Maven 多套环境配置

时间:2023-03-09 22:26:23
Maven 多套环境配置

在Java开发中会配置不同环境,可通过Maven的profile指定不同的环境配置,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.sdu.mobile.innovation</groupId>
<artifactId>esclient</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>esclient Maven Webapp</name>
<url>http://maven.apache.org</url> <properties>
<enviroment>src/main/resources</enviroment>
</properties> <build>
<finalName>esclient</finalName> <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>./target/classes/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<excludes>
<exclude>*.xls</exclude>
</excludes>
</configuration>
</execution>
</executions>
<version>2.3.2</version>
</plugin> <plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass></mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins> <resources>
<resource>
<directory>${enviroment}.${deploy.type}</directory>
</resource>
<resource>
<directory>${enviroment}</directory>
</resource>
</resources>
</build> <!-- 环境部署 -->
<profiles>
<profile>
<id>dev</id>
<properties>
<deploy.type>dev</deploy.type>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<deploy.type>prod</deploy.type>
</properties>
</profile>
</profiles> </project>

  工程的目录结构如下所示:

Maven 多套环境配置

编译时指定环境:

mvn clean package -Pdev