欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

maven使用profile设置多个环节不同配置

程序员文章站 2022-06-14 19:29:31
...
在pom文件中设置profiles节点
	<profiles>
		<profile>
			<!-- 本地开发环境 -->
			<id>dev</id>
			<activation>
			<!-- <activeByDefault>true</activeByDefault>  -->
				<property>
					<name>env</name>
					<value>dev</value>
				</property>
			</activation>
			<properties>
				<profiles.active>dev</profiles.active>
			</properties>
		</profile>
		<profile>
			<id>sit</id>
			<activation>
				
				<property>
					<name>env</name>
					<value>sit</value>
				</property>
			</activation>
			<properties>
				<profiles.active>sit</profiles.active>
			</properties>
		</profile>

		<profile>
			<id>uat</id>
			<activation>
				<property>
					<name>env</name>
					<value>uat</value>
				</property>
			</activation>
			<properties>
				<profiles.active>uat</profiles.active>
			</properties>
		</profile>

		<profile>
			<!-- 生产环境 -->
			<id>prd</id>
			<properties>
				<profiles.active>prd</profiles.active>
			</properties>
		</profile>

	</profiles>
maven打包支持profile环境来替换不同环境的配置文件(xml配置文件和properties配置文件)
	<plugin>
				<artifactId>maven-resources-plugin</artifactId>
				<executions>
					<execution>
						<id>copy-active-profile-resources</id>
						<phase>process-resources</phase>
						<goals>
							<goal>copy-resources</goal>
						</goals>
						<configuration>
							<outputDirectory>${basedir}/target/classes</outputDirectory>
							<resources>
								<resource>
									<directory>${project.basedir}/profiles/${profiles.active}</directory>
									<filtering>false</filtering>
								</resource>
							</resources>
							<overwrite>true</overwrite>
						</configuration>
					</execution>	
				</executions>
			</plugin>

 

1:在项目中配置profile文件夹,此文件夹下再配置dev,sit,uat文件夹,分别放对应的配置文件

2:使用maven命令进行不同配置文件的,在eclipse中配置 clean install -Dmaven.test.skip=true  -P  dev,在maven中使用 mvn clean install -Dmaven.test.skip=true  -P  dev 

如果需要上传到私服nexus中,遇到401错误,并且在maven的安装路径下面的setting配置文件的server节点中需要配置id为Releases和Snapshots的用户名和密码。

	<distributionManagement>
		<repository>
			<id>Releases</id>
			<name>Team Nexus Release Repository</name>
			<url>http://xxxx:7001/nexus/content/repositories/releases/</url>
		</repository>
		<snapshotRepository>
			<id>Snapshots</id>
			<name>Team Nexus Snapshot Repository</name>
			<url>http://xxxx:7001/nexus/content/repositories/snapshots/</url>
			<uniqueVersion>true</uniqueVersion> 
		</snapshotRepository>
	</distributionManagement>

    <server>
           <id>Releases</id>
              <username>xxx</username>
            <password>xxx</password>
          </server>
<server>
			<id>Snapshots</id>
			<username>xxx</username>
            <password>xxxx</password>
</server>

 

3:使用maven插件maven-compiler-plugin设置编码的jdk版本,及编码格式

<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>${jdk.version}</source>
					<target>${jdk.version}</target>
					<encoding>${project.build.sourceEncoding}</encoding>
				</configuration>
			</plugin>

 4:也可设置maven的安装目录下面的setting.conf中设置JDK的版本

 

<profile>
			<id>jdk8</id>
			 <activation>
				  <activeByDefault>true</activeByDefault>
				  <jdk>1.8</jdk>
			 </activation>
			 <properties>
				  <maven.compiler.source>1.8</maven.compiler.source>
				  <maven.compiler.target>1.8</maven.compiler.target>
				  <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
			 </properties> 
		</profile>

5:使用maven插件maven-war-plugin打war包

<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<configuration>
					<!--本地的db配置与迁移不需要在test,sit,uat,prd环境的容器中部署 -->
					<packagingExcludes>WEB-INF/classes/config/**</packagingExcludes>
				</configuration>
				<version>2.4</version>
			</plugin>

 6:获取本地命令行生效的maven配置文件:

mvn help:effective-settings

7:maven命令行进行远程部署私服

mvn deploy:deploy-file -Dfile=.\target\xxxx.jar Dpackaging=jar -Durl=http://xxxx:7001/nexus/content/repositories/snapshots  -DgroupId=com.xxx -DartifactId=xxx -Dversion=0.0.1-SNAPSHOT

相关标签: maven