-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
160 lines (150 loc) · 6.87 KB
/
index.js
File metadata and controls
160 lines (150 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
var shell = require('shelljs');
var path = require('path');
var fs = require("fs");
var os = require("os");
var config = require('./config');
// 仓库路径
repoBasePath = config.repoBasePath
if(!repoBasePath){
repoBasePath = path.join(os.homedir(), ".m2/repository")
}
// 上传指定文件夹下的
let filePath = config.filePath
if(!filePath){
console.error('filePath 不能为空');
return
}
// maven deploy 的时候不支持在本地仓库执行,因此新建一个临时使用的文件夹
let workPath = path.join(os.homedir(), "maven-temp-" + Date.now())
let uploadPackaging = config.uploadPackaging
if (!fs.existsSync(workPath)) {
fs.mkdirSync(workPath)
}
fileDisplay(filePath)
let uploadList = []
function fileDisplay(filePath) {
fs.readdir(filePath, function (err, files) {
if (err) {
console.error(err);
return console.error(err);
}
// filename 示例 ehr-0.0.1-SNAPSHOT.jar
files.forEach(function (filename) {
// 全路径
var filedir = path.join(filePath, filename);
//根据文件路径获取文件信息,返回一个fs.Stats对象
fs.stat(filedir, function (eror, stats) {
if (eror) {
console.error('获取文件stats失败');
} else {
if (stats.isDirectory()) {
fileDisplay(filedir);
}
if (stats.isFile()) {
if (!(filename.endsWith('.jar') || filename.endsWith('.pom'))) {
return
}
if (!(uploadPackaging == 'all' || filename.endsWith(uploadPackaging))) {
return
}
// 排除 SNAPSHOT 包干扰项
if (filedir.includes('SNAPSHOT') && !filename.includes('SNAPSHOT')) {
return
}
let pathArraystrs = filedir.split("/");
// jar 包完整名称
let jarFn = pathArraystrs.pop()
let version = pathArraystrs.pop()
let artifactId = pathArraystrs.pop()
let groupId = pathArraystrs.join('.').substring(repoBasePath.length + 1)
let currentWorkPath = workPath + '/' + artifactId + '-' + version
if (!fs.existsSync(currentWorkPath)) {
fs.mkdirSync(currentWorkPath)
}
let currentFilePath = `${currentWorkPath}/${filename}`
if (shell.exec(`cp ${filedir} ${currentFilePath}`).code !== 0) {
console.error("cp error " + currentFilePath);
} else {
console.log("复制文件到 " + currentFilePath);
}
let gav = {
groupId,
artifactId,
version,
file: currentFilePath
}
if (filename.endsWith('.jar')) {
var prefix = filedir.substring(0, filedir.length - 4)
var pomFileNameDir = prefix + ".pom"
if (fs.existsSync(pomFileNameDir)) {
var pomXmlFileNameDir = currentFilePath.replace(filename, "pom.xml")
if (!fs.existsSync(pomXmlFileNameDir)) {
if (shell.exec(`cp ${pomFileNameDir} ${pomXmlFileNameDir}`).code !== 0) {
console.error("cp error " + pomFileNameDir);
} else {
console.log("生成 " + pomXmlFileNameDir);
}
}
gav.packaging = 'jar'
gav.pomFile = pomXmlFileNameDir
if ((gav.artifactId + '-' + gav.version + '.jar') != jarFn) {
console.error("gav 不规范:" + gav);
} else {
uploadList.push(gav)
}
} else {
console.error("pom 不存在:" + pomFileNameDir);
}
} else {
const str = fs.readFileSync(filedir, 'utf-8');
if (str.includes('<packaging>pom</packaging>')) {
gav.packaging = 'pom'
uploadList.push(gav)
}
}
}
}
})
});
});
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
async function execute() {
console.error('sleep start');
await sleep(3000)
console.error('sleep end');
console.error('需要上传的数量: ' + uploadList.length);
let errorGav = []
uploadList.forEach(gav => {
if (!(uploadPackaging == 'all' || uploadPackaging == gav.packaging)) {
return
}
let pomFileShell = ''
if (gav.packaging == 'jar') {
pomFileShell = `-DpomFile=${gav.pomFile}`
}
let currentRepositoryId = config.repositoryId
let currentRepositoryUrl = config.repositoryUrl
if (gav.version.includes('SNAPSHOT') && config.snapshotRepositoryId && config.snapshotRepositoryUrl) {
currentRepositoryId = config.snapshotRepositoryId
currentRepositoryUrl = config.snapshotRepositoryUrl
}
let mavenshell = `mvn deploy:deploy-file -Dmaven.test.skip=true -DgroupId=${gav.groupId} -DartifactId=${gav.artifactId} -Dversion=${gav.version} -Dpackaging=${gav.packaging} -Dfile=${gav.file} ${pomFileShell} -DrepositoryId=${currentRepositoryId} -Durl=${currentRepositoryUrl}`
console.log(`start maven upload, packaging=${gav.packaging},groupId=${gav.groupId},${gav.artifactId}-${gav.version}`);
// console.log(mavenshell);
if (shell.exec(mavenshell).code !== 0) {
errorGav.push(`groupId=${gav.groupId},${gav.artifactId}-${gav.version}`)
} else {
console.log(`maven upload success, packaging=${gav.packaging},groupId=${gav.groupId},${gav.artifactId}-${gav.version}`);
}
})
console.log(`上传成功数量: ${uploadList.length - errorGav.length} , 失败数量: ${errorGav.length}`);
if(errorGav.length > 0){
console.log(`error gav: ${errorGav}`);
}
// 删除掉临时的文件夹
shell.exec(`rm -rf ${workPath}`)
}
execute()