需要的资源文件,都放在根目录下:
1. versionInfo.js
const fs = require('fs')
const path = require('path')
const mkdirp = require('mkdirp')
const spawn = require('child_process').spawn
const packageObj = require('./package.json')
const versionNo = packageObj.version
const projectName = packageObj.name
const outputPath = 'dist/public'
const gitCommitId = spawn('git', ['rev-parse', 'HEAD']) // 获取commitId
function delDir (path) {
let files = []
if (fs.existsSync(path)) {
files = fs.readdirSync(path)
files.forEach((file) => {
const curPath = path + '/' + file
if (fs.statSync(curPath).isDirectory()) {
delDir(curPath) // 递归删除文件夹
} else {
fs.unlinkSync(curPath) // 删除文件
}
})
fs.rmdirSync(path)
}
}
delDir(outputPath)
mkdirp.sync(outputPath)
const versionInfo = fs.createWriteStream(path.join(outputPath, 'version.txt'), 'utf-8')
const nowDate = new Date()
const nYear = nowDate.getFullYear()
let nMonth = nowDate.getMonth() + 1
nMonth < 10 && (nMonth = `0${nMonth}`)
let nDay = nowDate.getDate()
nDay < 10 && (nDay = `0${nDay}`)
const dateString = `${nYear}${nMonth}${nDay}`
// 读取git当前的commitId
gitCommitId.stdout.on('data', function (data) {
versionInfo.write(`version = V${versionNo}_${dateString}_${data.toString().substring(0, 10)}\r\n`)
versionInfo.write(`projectName = ${projectName}\r\n`)
versionInfo.write(`git commit id = ${data}\r\n`)
console.log('版本文件生成成功')
})
gitCommitId.stderr.on('data', function (error) {
versionInfo.write(`${error}\n`)
versionInfo.end()
console.error('版本文件生成失败')
})
2. gitInfo.js
const fs = require('fs')
const path = require('path')
const mkdirp = require('mkdirp')
const spawn = require('child_process').spawn
const outputPath = 'dist/public/static'
const gitCommitId = spawn('git', ['rev-parse', 'HEAD']) // 获取commitId
const gitStatus = spawn('git', ['status', '-s']) // 获取文件变动信息
mkdirp.sync(outputPath)
const gitInfo = fs.createWriteStream(path.join(outputPath, 'gitInfo.txt'), 'utf-8')
// 读取git当前的commitId
gitCommitId.stdout.on('data', function (data) {
gitInfo.write(`Git's commitId: ${data}\n`)
gitStatus.stdout.on('data', function (data) {
gitInfo.write(`Files' status:\n${data}`)
gitInfo.end()
})
})
gitCommitId.stderr.on('data', function (error) {
gitInfo.write(`${error}\n`)
gitInfo.end()
})
gitStatus.stderr.on('data', function (error) {
gitInfo.write(`${error}\n`)
gitInfo.end()
})
3. compress.js
const exec = require('child_process').exec
const packageName = require('./package.json').name || 'dist'
const nowDate = new Date()
const nYear = nowDate.getFullYear()
let nMonth = nowDate.getMonth() + 1
nMonth < 10 && (nMonth = `0${nMonth}`)
let nDay = nowDate.getDate()
nDay < 10 && (nDay = `0${nDay}`)
let hourStr = nowDate.getHours()
hourStr < 10 && (hourStr = `0${hourStr}`)
let minuteStr = nowDate.getMinutes()
minuteStr < 10 && (minuteStr = `0${minuteStr}`)
let secondStr = nowDate.getSeconds()
secondStr < 10 && (secondStr = `0${secondStr}`)
const dateString = `${nYear}${nMonth}${nDay}${hourStr}${minuteStr}${secondStr}`
console.log('开始压缩生成jar包...')
const cmd = `node gitInfo.js && jar -cvf ${packageName}-${dateString}.jar -C dist/ .`
exec(cmd, function (error, stdout, stderr) {
// console.log(stdout)
if (error) {
console.error(error)
} else {
console.log(`打包成功,生成${packageName}-${dateString}.jar`)
}
})
4. pom.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hzcominfo.sinopec</groupId>
<artifactId>order-bonus-front</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
</project>
5. deployJarToMaven.js
const exec = require('child_process').exec
const packageName = require('./package.json').name || 'dist'
const packageVersion = require('./package.json').version || ''
const nowDate = new Date()
const nYear = nowDate.getFullYear()
let nMonth = nowDate.getMonth() + 1
nMonth < 10 && (nMonth = `0${nMonth}`)
let nDay = nowDate.getDate()
nDay < 10 && (nDay = `0${nDay}`)
let hourStr = nowDate.getHours()
hourStr < 10 && (hourStr = `0${hourStr}`)
let minuteStr = nowDate.getMinutes()
minuteStr < 10 && (minuteStr = `0${minuteStr}`)
let secondStr = nowDate.getSeconds()
secondStr < 10 && (secondStr = `0${secondStr}`)
const dateString = `${nYear}${nMonth}${nDay}${hourStr}${minuteStr}${secondStr}`
console.log('开始压缩生成jar包...')
const targetFileName = `${packageName}-${dateString}.jar`
const cmd = `node gitInfo.js && jar -cvf ${targetFileName} -C dist/ .`
exec(cmd, function (error, stdout, stderr) {
if (error) {
console.error(error)
} else {
console.log(`打包成功,生成${packageName}-${dateString}.jar`)
const xml2js = require('xml2js')
const fs = require('fs')
const MAVEN_REP_URL = 'http://10.183.188.124:8081/nexus/content/repositories/snapshots/'
const MAVEN_REPOSITORY_ID = 'bp-snapshot'
const POM_FILE_NAME = 'pom.xml'
fs.readFile('pom.xml', 'utf-8', (err, data) => {
if (err) {
throw err
}
// convert XML data to JSON object
xml2js.parseString(data, (err, result) => {
if (err) {
throw err
}
console.log(result.project)
result.project.artifactId = packageName
result.project.version = packageVersion + '-SNAPSHOT'
// convert JSON objec to XML
const builder = new xml2js.Builder()
const xml = builder.buildObject(result)
// write updated XML string to a file
fs.writeFile('pom.xml', xml, (err) => {
if (err) {
throw err
}
console.log('开始部署...')
const mavenCmd = `mvn deploy:deploy-file -Dfile=${targetFileName} -Durl=${MAVEN_REP_URL} -DrepositoryId=${MAVEN_REPOSITORY_ID} -DpomFile=${POM_FILE_NAME}`
exec(mavenCmd, function (error, stdout, stderr) {
console.log(stdout)
if (error) {
console.error(error)
} else {
console.log(`部署成功,当前版本${result.project.version}`)
}
})
})
})
})
}
})
6. package.json中的打包、上传maven指令
"build": "node versionInfo.js && vue-cli-service build --no-clean && node compress.js",
"build:deploy": "node versionInfo.js && vue-cli-service build --no-clean && node deployJarToMaven.js",
说明:在运行打包、上传指令时,会报缺少三方依赖的错(如xml2js、mkdirp),安装上即可。