一 java 多版本管理
使用 jenv, 地址:https://github.com/jenv/jenv
mac 操作
brew install jenv
# Shell: zsh
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(jenv init -)"' >> ~/.zshrc
source ~/.zshrc
需要把JAVA_HOME 注释,搜索
grep -E 'JAVA_HOME|PATH' ~/.bash_profile ~/.profile ~/.bashrc ~/.zshrc
jenv 检测
jenv doctor
检测之前是否安装java
/usr/libexec/java_home -V
如果有会出现
Matching Java Virtual Machines (4):
20.0.1 (arm64) "Oracle Corporation" - "Java SE 20.0.1" /Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home
17.0.8 (arm64) "Amazon.com Inc." - "Amazon Corretto 17" /Users/admin/Library/Java/JavaVirtualMachines/corretto-17.0.8.1/Contents/Home
11.0.20 (arm64) "Amazon.com Inc." - "Amazon Corretto 11" /Users/admin/Library/Java/JavaVirtualMachines/corretto-11.0.20.1/Contents/Home
1.8.0_382 (arm64) "Amazon" - "Amazon Corretto 8" /Users/admin/Library/Java/JavaVirtualMachines/corretto-1.8.0_382/Contents/Home
使用 jenv add 添加java 版本
例如:
jenv add /Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home
jenv add /Users/admin/Library/Java/JavaVirtualMachines/corretto-17.0.8.1/Contents/Ho
检查
jenv doctor
jenv versions
jenv which java
设置全局版本:
jenv global 1.8 # system 这个就是使用全局的
当前目录使用该版本jenv local 命令在当前目录中创建了一个 .java-version 文件
jenv local 1.8
Maven 安装
地址: https://maven.apache.org/install.html
解压,然后复制到
sudo mv apache-maven-3.9.8 /usr/local/
然后vim ~/.zshrc
export M2_HOME=/usr/local/apache-maven-3.9.8
export PATH=$PATH:$M2_HOME/bin
最后 source ~/.zshrc
阿里镜像
找到 /usr/local/apache-maven-3.9.8/conf/settings.xml
修改部分
<mirrors>
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
基础命令 mvn clean install ,mvn install ,mvn package
第三方依赖库
部署服务器
服务器安装
java 地址: https://www.oracle.com/java/technologies/downloads/archive/
解压 JDK 安装包:
tar -xzf jdk-11.0.x_linux-x64_bin.tar.gz
设置环境变量,编辑 /etc/profile 或你的 shell 配置文件,比如 ~/.bashrc,添
export JAVA_HOME=/path/to/extracted/jdk-11.0.x
export PATH=$JAVA_HOME/bin:$PATH
mvn clean package
cd /etc/systemd/system
vim xxx.service
which java 查看位置
[Unit]
Description=duanju
After=network.target
[Service]
Type=simple
WorkingDirectory=/data/duanju
ExecStart=/data/java/jdk1.8.0_401/bin/java -jar /data/duanju/duanju.jar --spring.config.location=./conf/application.yml
ExecStop=/bin/kill -SIGTERM $MAINPID
TimeoutStopSec=20s
Restart=always
RestartSec=2s
StandardOutput=append:/data/duanju/output.log
StandardError=inherit
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl restart xxx.service
systemctl status xxx.service
systemctl stop xxx.service
java jenkins
pipeline {
agent any
environment {
GIT_URL = "https://codeup.aliyun.com/64xxxxx35b049235/duanju/java_duanju.git"
}
//运行选项
options {
disableConcurrentBuilds() //禁止并发构建
buildDiscarder logRotator(artifactDaysToKeepStr: '',
artifactNumToKeepStr: '',
daysToKeepStr: '7',
numToKeepStr: '10') //历史构建
}
//启动参数
parameters {
choice choices: ['build:test'], description: '选择部署到什么环境', name: 'DEPLOY_TO'
gitParameter branchFilter: '.*',
defaultValue: 'master',
description: '选择需要发布的代码分支,默认为master',
name: 'BranchName',
quickFilterEnabled: true,
selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'PT_BRANCH'
}
stages {
stage('pull dev') {
steps {
checkout changelog: true, poll: false, scm: [
$class: 'GitSCM',
branches: [[name: "$BranchName"]],
doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CleanCheckout']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'codeup', url: "${GIT_URL}"]]
]
}
}
stage("deploy"){
steps{
script{
if (params.DEPLOY_TO == 'build:test') {
echo "DEPLOY_TO is build:test"
sh '''
export JAVA_HOME=/data/jdk1.8.0_401
export PATH=$JAVA_HOME/bin:$PATH
export M2_HOME=/data/apache-maven-3.9.8
export PATH=$PATH:$M2_HOME/bin
mvn clean package
mv target/duanju.jar /data/duanju/test
/data/test_java_build.sh
'''
// sh "./docker_build.sh ${params.DEPLOY_TO}"
// 这里可以添加你需要在 build:test 情况下执行的步骤
} else {
echo "DEPLOY_TO is not build:test"
}
}
}
}
stage('DEPLOY TO') {
steps {
script {
echo "DEPLOY TO: ${DEPLOY_TO}"
}
}
}
}
post {
success {
echo 'Build was successful!'
script {
currentBuild.description = "构建成功!"
}
}
failure {
echo 'Build failed!'
script {
currentBuild.description = "构建失败!"
}
}
aborted {
echo '构建取消拉aborted!'
script {
currentBuild.description = "构建取消拉!"
}
}
}
}
杀死
ps aux | grep java