如何将本地jar包上传到私有maven库


一些项目的库只放在内网,有时候需要远程开发非常不方便,这时候就可以把相应的库传到自己私有的maven库里面,之后在项目配置文件里面加上自己的仓库就行了。

本地缓存的jar包上传有好几种方式

1. 通过Nexus后台的upload功能上传,这个需要填写一堆信息,效率相当低下。
2. 通过maven的发布功能
3. 通过curl

maven上传,自己填充信息

mvn deploy:deploy-file -Dmaven.test.skip=true -DgroupId=com.android.tools.build -DartifactId=hlw -Dversion=1.11-SNAPSHOT -Dpackaging=jar -Dfile=xxx-1.0.jar -Durl=http://localhost:8081/repository/maven-releases/ -DrepositoryId=releases

通过curl上传,可以直接把pom和jar文件上传上去

curl -v -u admin:admin123 --upload-file xxx-1.0.jar http://localhost:8081/repository/maven-release/com/xxx/xxx/xxx-1.0.jar

如果用命令行一个一个去执行还是很慢的,我用python写了一丁点代码来调用curl方式处理,只要把缓存下面的需要上传的jar包com.xxx.xxx目录复制到某个文件夹下就可以了

# encoding: utf-8
import os

# maven config start
user_name = 'admin'
password = 'admin123'
host = 'http://nexus.11000011.com:8081/repository/'
# snapshot不能上传至release,这里可定义配置
maven_releases = 'maven-releases'
maven_snapshots = 'maven-snapshots'
# maven config end

# upload command template
upload_template = 'curl -v -u %s:%s --upload-file %s %s'
# local upload path
local_upload = 'e:/temp/nexus'

# pom and jar file
pom_dict = {}
jar_dict = {}


def upload_file(file_path, maven_path):
    target_url = host + maven_path + get_abs_url(file_path)
    upload_cmd = upload_template % (user_name, password, file_path, target_url)
    os.system(upload_cmd)


def upload_all():
    # 只处理有jar文件的
    for key in jar_dict:
        jar_file = jar_dict[key]
        pom_file = pom_dict[key]
        maven_path = maven_releases
        if 'SNAPSHOT' in key:
            maven_path = maven_snapshots
        upload_file(pom_file, maven_path)
        upload_file(jar_file, maven_path)


# 获取上传相对路径
def get_abs_url(file_path):
    flag = len(local_upload)
    temp_path = file_path[flag + 1:]

    flag = temp_path.rindex('/')
    file_name = temp_path[flag:]
    temp_path = temp_path[0: flag]

    flag = temp_path.rindex('/')
    temp_path = temp_path[0: flag]

    # 处理一下包名的点号
    flag = temp_path.index('/')
    first_part = temp_path[0: flag]
    second_part = temp_path[flag:]
    first_part = first_part.replace('.', '/')

    abs_path = '/' + first_part + second_part + file_name

    return abs_path


# 遍历文件夹,提取pom和jar文件
def each_file(filepath):
    path_dir = os.listdir(filepath)
    for item in path_dir:
        child = os.path.join('%s/%s' % (filepath, item))
        if os.path.isfile(child):
            if '.pom' in item:
                end = item.rindex('.pom')
                key = item[0: end]
                pom_dict[key] = child
            elif '.jar' in item:
                end = item.rindex('.jar')
                key = item[0: end]
                jar_dict[key] = child
            # android aar
            elif '.aar' in item:
                end = item.rindex('.aar')
                key = item[0: end]
                jar_dict[key] = child
            continue
        each_file(child)


if __name__ == "__main__":
    each_file(local_upload)
    upload_all()


内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://11000011.com/?id=112

« 上一篇 下一篇 »

我要评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

主机推荐

  • 搬瓦工