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

本文介绍了如何利用Maven的profile特性在dev、sit、uat等不同环境中配置不同的设置文件。通过命令行参数 `-P` 指定激活的profile,跳过测试并执行构建。在遇到401错误时,需要在Maven设置中配置服务器凭证。同时,使用maven-compiler-plugin和maven-war-plugin插件分别设定编码与打war包。最后,讲解了如何查看生效的Maven配置以及将项目部署到远程私服。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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

<think>我们正在讨论Jenkins配置多个Maven版本的问题。根据引用内容,我们可以知道Jenkins支持配置多个Maven版本,并且可以通过全局工具配置来实现。引用[2]提到“Jenkins的jdkmaven配置”,说明Jenkins可以配置多个工具(包括JDKMaven)的版本。引用[4]则展示了如何配置多个Maven仓库,虽然仓库版本不同,但配置方式类似。因此,配置多个Maven版本的步骤应该是:1.在Jenkins服务器上安装多个版本的Maven(确保路径不同)。2.在Jenkins的“全局工具配置”中分别添加这些Maven版本,并指定各自的安装路径。3.在具体的Jenkins任务中,可以选择使用哪个Maven版本。下面详细说明步骤:</think>### Jenkins配置多个Maven版本的方法 Jenkins支持在同一实例中配置多个Maven版本,具体实现步骤如下: #### 1. **在Jenkins服务器安装多个Maven版本** - 登录Jenkins服务器,下载不同版本的Maven压缩包(如3.9.63.8.8) - 解压到不同目录: ```bash /opt/maven/maven-3.9.6 /opt/maven/maven-3.8.8 ``` #### 2. **添加Jenkins全局工具配置** - 进入Jenkins控制台:`系统管理 > 全局工具配置` - 在"Maven"区域添加多个配置: ```markdown Name: Maven-3.9.6 MAVEN_HOME: /opt/maven/maven-3.9.6 Name: Maven-3.8.8 MAVEN_HOME: /opt/maven/maven-3.8.8 ``` - 勾选"自动安装"可让Jenkins自动下载指定版本[^2] #### 3. **为不同项目指定Maven版本** - 在项目配置页面的`Build`环节: - 选择`Invoke top-level Maven targets` - 在"Maven Version"下拉菜单中选择需要的版本: ```markdown [x] Invoke top-level Maven targets Maven Version: Maven-3.9.6 // 选择版本 Goals: clean package // 执行命令 ``` #### 4. **多仓库支持配置(可选)** - 若不同版本需对应不同仓库,修改`settings.xml`: ```xml <!-- 引用[4]示例 --> <profile> <id>maven-3.9</id> <repositories> <repository> <url>http://repoA</url> </repository> </repositories> </profile> <profile> <id>maven-3.8</id> <repositories> <repository> <url>http://repoB</url> </repository> </repositories> </profile> ``` - 在Jenkins项目中使用`-P`参数激活配置: ```bash mvn clean install -P maven-3.9 # 激活3.9对应的仓库配置 ``` #### 5. **验证配置** - 在项目构建日志中检查Maven版本: ```log [INFO] Maven home: /opt/maven/maven-3.9.6 [INFO] Java version: 17, vendor: Eclipse Adoptium ``` ### 关键注意事项 1. **环境变量冲突**: - 避免在系统环境变量设置全局`MAVEN_HOME` - 通过Jenkins工具配置覆盖系统路径[^2] 2. **JDK版本兼容性**: - Maven 3.9+需JDK 11+,旧项目需匹配JDK版本 - 在`全局工具配置`中同步配置多版本JDK[^2] 3. **容器化部署**: - Docker环境需在镜像中预装多版本Maven - 通过volume挂载不同版本的`settings.xml`[^3] ### 配置效果示例 ```mermaid graph LR A[Jenkins项目A] --> B[Maven 3.9.6] A --> C[仓库A] D[Jenkins项目B] --> E[Maven 3.8.8] D --> F[仓库B] ``` 通过上述步骤,可实现不同项目使用特定Maven版本,解决多项目版本兼容问题[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值