1. 程式人生 > 實用技巧 >maven 使用shell命令 批量上傳jar包到遠端maven庫 python 程式碼

maven 使用shell命令 批量上傳jar包到遠端maven庫 python 程式碼

背景:

需要批量匯入jar包到遠端庫,有遠端庫的網頁登陸名和密碼(admin許可權),準備寫程式批量匯入。

1、前提條件

1、本地安裝好了maven

2、有程式的執行環境(我用的python編寫的)

2、核心程式碼:

核心程式碼:

#獲取根路徑的所有jar包
def getAllJar(rootpath):
   lists=[]
for root ,dir,files in os.walk(rootpath):
for each in files:
if each.endswith('jar'):
          lists.append(each)
  return lists

#傳入一個根路徑以及一個目標jar包路徑,可以返回一個jar上傳的shell語句
#rootpath='D:\jarpathdir'
#filepath='D:\jarpathdir\org\apache\logging\..\*.jar'
def getShell(rootpath,filepath):
delRootFilePath=filepath.repace(rootpath,'')[1:]

repoId='myrepo'
repoUrl='http://.../repository/myrepo'

templist=delRootFilePath.split('\\')
jarVersion=templist[-2]
jarArtifactid=templist[-3]
jarGroupid='.'.join(templist[:-3])

jarShell='mvn deploy:deploy-file'\
+'-DrepositoryId='+repoId\
+'-Durl='+repoUrl\
+'-Dfile='+filepath\
+'-Dgroupid='+jarGroupid\
+'-DartifactId='+jarArtifactid\
+'-Dversion'+jarVersion\
+'-Dpackgaing=jar'
return jarShell


#執行shell的方法:
os.system(jarshell)

  執行此語句就可以上傳一個jar包到遠端庫,多個語句依次執行,就可以批量上傳了。


流程:

1) 設定maven的conf資料夾中的setting檔案,把使用者名稱密碼寫進去,並註釋掉所有遠端映象庫

2)把responsary庫拷貝到其他位置(否則上傳不成功),並以此作為程式碼中的rootpath

3)結合以上程式碼原理,構建每一個jar的shell語句依次執行