You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins环境下Spring Boot项目Gradle打包传参与远程部署咨询

Hey Chloe, let's work through your Jenkins + Spring Boot + Gradle requirements together. Below is a tailored Gradle script that addresses all three of your needs, plus breakdowns of how each part works:


1. Custom Jar Naming (project-{$profile}-{$date}.jar)

We'll dynamically set the jar filename using the active profile and current date. We'll grab the profile from a command-line parameter (more on that next) and generate a formatted date string to ensure uniqueness.

2. Pass Profile Parameter to Gradle

We'll accept a profile parameter via the Gradle command line (e.g., -Pprofile=prod) and use it to configure both the Spring Boot build runtime and our jar naming convention.

3. Deploy & Run the Jar on Remote Server

We'll add tasks to copy the built jar to your remote server via scp, then execute a command to start the Spring Boot application. The example uses nohup to keep the app running in the background, but you can adjust it to fit your server's process management setup (like systemd) if needed.


Complete build.gradle Script

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.2.0'
    id 'io.spring.dependency-management' version '1.1.4'
}

// Define dynamic variables for profile and build date
ext {
    // Default to 'dev' if no profile parameter is passed
    activeProfile = project.hasProperty('profile') ? project.profile : 'dev'
    // Format date as yyyyMMddHHmm (adjust the format string to your preference)
    buildDate = new Date().format('yyyyMMddHHmm')
}

// Configure Spring Boot to use the active profile at runtime
springBoot {
    mainClass = 'com.yourpackage.YourApplication'
    args = ["--spring.profiles.active=${activeProfile}"]
}

// Customize the output jar filename
jar {
    archiveFileName = "project-${activeProfile}-${buildDate}.jar"
    // Optional: Set a specific output directory for the jar
    destinationDirectory = file("${project.buildDir}/libs")
}

// Task to copy the built jar to your remote server via scp
task deployToRemote(type: Exec) {
    // Replace these values with your actual server details
    def remoteUser = 'your-server-username'
    def remoteHost = 'your-server-ip-or-domain'
    def remoteDeployDir = '/opt/apps/your-project'
    def localJarPath = jar.archiveFile.get().asFile.absolutePath
    def jarFileName = jar.archiveFileName.get()

    // Execute scp command to transfer the jar
    commandLine 'scp', localJarPath, "${remoteUser}@${remoteHost}:${remoteDeployDir}/${jarFileName}"
}

// Task to start the jar on the remote server (depends on successful deployment)
task runOnRemote(type: Exec, dependsOn: deployToRemote) {
    def remoteUser = 'your-server-username'
    def remoteHost = 'your-server-ip-or-domain'
    def remoteDeployDir = '/opt/apps/your-project'
    def jarFileName = jar.archiveFileName.get()

    // Command to run the jar in the background (adjust for your server setup)
    // Using nohup to keep the app running after SSH session ends
    commandLine 'ssh', "${remoteUser}@${remoteHost}", 
        "cd ${remoteDeployDir} && nohup java -jar ${jarFileName} > app.log 2>&1 &"
}

// Optional: Automatically run deployment after build completes
build.dependsOn runOnRemote

Key Details & Jenkins Setup Tips:

  • Profile Handling: The activeProfile variable checks if a profile parameter is passed to Gradle (e.g., -Pprofile=prod in your Jenkins build step). If no parameter is provided, it defaults to dev.
  • Jar Naming: The jar task uses our dynamic variables to generate the exact filename format you requested.
  • Deployment:
    • deployToRemote relies on scp to transfer the jar—make sure your Jenkins server has passwordless SSH access to the remote server (set up SSH keys for this).
    • runOnRemote executes an SSH command to start the app. If you use systemd for process management, replace the nohup command with something like sudo systemctl restart your-project.service.
  • Jenkins Build Step: Configure your Jenkins job to run Gradle with the profile parameter as a build argument:
    clean build -Pprofile=${PROFILE}
    
    You can set PROFILE as a Jenkins job parameter (dropdown or text input) to let users select dev/prod/staging environments easily.

Let me know if you need tweaks for your specific server setup or Spring Boot version!

内容的提问来源于stack exchange,提问作者Chloe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:24:33