PowerShell 添加 Maven 到 PATH
当前用户(永久)
$addPath = "D:\soft\apache-maven-3.9.15\bin"
$currentPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($currentPath -notlike "*$addPath*") {
[Environment]::SetEnvironmentVariable("Path", "$currentPath;$addPath", "User")
Write-Host "已追加: $addPath"
} else {
Write-Host "PATH 已存在"
}
当前会话立即生效
$env:Path += ";D:\soft\apache-maven-3.9.15\bin"
重新打开终端才能读到写入注册表的值,不想重开就用这行临时追加。
系统级 PATH(需管理员)
$addPath = "D:\soft\apache-maven-3.9.15\bin"
$currentPath = [Environment]::GetEnvironmentVariable("Path", "Machine")
if ($currentPath -notlike "*$addPath*") {
[Environment]::SetEnvironmentVariable("Path", "$currentPath;$addPath", "Machine")
}
cmd 添加 Maven 到 PATH
:: 追加到当前用户 PATH(永久,写注册表)
setx PATH "%PATH%;D:\soft\apache-maven-3.9.15\bin"
:: 当前窗口立即可用
set PATH=%PATH%;D:\soft\apache-maven-3.9.15\bin
系统 PATH(需管理员):
setx /M PATH "%PATH%;D:\soft\apache-maven-3.9.15\bin"
setx 会写注册表,当前窗口不生效,重新打开 cmd 后验证:
mvn -v
用 mvn 运行 Spring Boot
在项目根目录(有 pom.xml 的那层)执行:
mvn spring-boot:run
常用参数:
# 跳过测试(开发时推荐,速度快)
mvn spring-boot:run -DskipTests
# 指定 profile
mvn spring-boot:run -Dspring-boot.run.profiles=dev
打包后运行
mvn clean package -DskipTests
java -jar target/xxx.jar
生产部署通常用这种方式,不依赖 Maven 运行时。
常见错误
mvn 不是内部或外部命令
PATH 没生效。执行 mvn -v 测试,不行就重新打开终端,或确认 Maven bin 目录路径是否正确。
No plugin found for prefix 'spring-boot'
pom.xml 缺少 Spring Boot Maven 插件,在 <build> 里加:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
端口 8080 被占用
netstat -ano | findstr 8080
taskkill /PID 进程ID /F
或在 application.yml 改端口:
server:
port: 8081
多模块项目依赖解析失败
${revision} 未替换说明父 pom 没有先 install,在项目根执行:
mvn clean install -N -DskipTests
-N 表示只安装父 pom,不递归子模块,之后再正常 mvn spring-boot:run。
