如何快速搭建一个JAVA持续交付环境

20 阅读1分钟
  1. JDK 2.
  2. maven
https://downloads.apache.org/maven/maven-3/3.9.12/

sudo wget https://archive.apache.org/dist/maven/maven-3/3.9.12/binaries/apache-maven-3.9.12-bin.tar.gz -P /opt
sudo tar -zxvf apache-maven-3.8.8-bin.tar.gz -C /usr/local
sudo mv /usr/local/apache-maven-3.8.8 /usr/local/maven3.8

# 4. 配置环境变量
echo "export MAVEN_HOME=/usr/local/maven3.8" | sudo tee -a /etc/profile
echo "export PATH=\$PATH:\$MAVEN_HOME/bin" | sudo tee -a /etc/profile
source /etc/profile

# 5. 验证安装
mvn -v
  1. 调整设置私有仓库
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  
  <!-- 本地仓库路径(可选) -->
  <localRepository>${user.home}/.m2/repository</localRepository>
  
  <!-- 交互模式设置 -->
  <interactiveMode>true</interactiveMode>
  
  <!-- 离线模式设置 -->
  <offline>false</offline>
  
  <!-- 插件组设置 -->
  <pluginGroups></pluginGroups>
  
  <!-- 代理设置(如果使用代理) -->
  <proxies></proxies>
  
  <!-- 镜像配置 -->
  <mirrors>
    <mirror>
      <id>zgf-mirror</id>
      <name>zgf Public Repository Mirror</name>
      <url>http://nexus.zgf.com:8081/nexus/content/groups/public/</url>
      <mirrorOf>*</mirrorOf>
    </mirror>
  </mirrors>
  
  <!-- Profile 配置 -->
  <profiles>
    <profile>
      <id>zgf-repo</id>
      <!-- 普通依赖仓库 -->
      <repositories>
        <repository>
          <id>zgf-public</id>
          <name>zgf Public Repository</name>
          <url>http://nexus.zgf.com:8081/nexus/content/groups/public/</url>
          <layout>default</layout>
          <releases>
            <enabled>true</enabled>
            <updatePolicy>daily</updatePolicy>
            <checksumPolicy>ignore</checksumPolicy>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
            <checksumPolicy>ignore</checksumPolicy>
          </snapshots>
        </repository>
      </repositories>
      <!-- 插件仓库 -->
      <pluginRepositories>
        <pluginRepository>
          <id>zgf-public</id>
          <name>zgf Public Plugin Repository</name>
          <url>http://nexus.zgf.com:8081/nexus/content/groups/public/</url>
          <layout>default</layout>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>
  
  <!-- 激活的 Profile -->
  <activeProfiles>
    <activeProfile>zgf-repo</activeProfile>
  </activeProfiles>
  
  <!-- 服务器认证信息(如果需要) -->
  <servers>
    <!-- 如果需要认证,添加以下配置 -->
    <!--
             <server>
      <id>juzifenqi-public</id>
      <username>your-username</username>
      <password>your-password</password>
    </server>
    -->
  </servers>
  
</settings>
  1. git
# 1. 安装Git
sudo yum install -y git

# 2. 验证安装
git --version
  1. 配置公钥到GIT
### 1. 生成SSH密钥

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
### 2. 添加SSH密钥到ssh-agent
eval "$(ssh-agent -s)"

ssh-add ~/.ssh/id_rsa

cat ~/.ssh/id_rsa.pub
  1. 发布脚本

#!/bin/bash
cd /data/zgf
echo "开始克隆代码"
if [ ! -d "cmmmmmmm" ]; then
  git clone -b test-uat-251227 git@git.zgf.cn:background/cmmmmmmm.git
  cd cmmmmmmm
  git branch --set-upstream-to=origin/test-uat-251227 test-uat-251227
else
  echo "代码已存在,开始更新"
  cd cmmmmmmm
  git checkout test-uat-251227
  git fetch --all
  git pull origin test-uat-251227 || { echo "拉取失败,尝试重置"; git reset --hard origin/test-uat-251227; }
fi
cd cmmmmmmm-backend
pwd
# 打包
echo "开始打包"
mvn clean package -DskipTests
echo "打包完成"
# 备份旧包
TIMESTAMP=$(date +%Y%m%d%H%M%S)
echo "开始备份旧包到 bak/cmmmmmmm-backend.jar.$TIMESTAMP"
mv /data/App/cmmmmmmm-backend/cmmmmmmm-backend.jar /home/ops/zgfjar/cmmmmmmm-backend.jar.bak.$TIMESTAMP
# 替换新包
echo "替换新包到部署目录"
cp target/cmmmmmmm-backend.jar /data/App/cmmmmmmm-backend/
# 重启服务

cd /data/App/cmmmmmmm-backend/bin
echo "停止服务"
sh stop.sh
echo "启动服务"
sh start.sh
echo "部署完成"