从 sonatype nexus 迁移到 bintray

Migrate from sonatype nexus to bintray

Bintray 似乎是一项不错的服务,并且有很多文章介绍它们如何比使用 Nexus 更好,但由于某些原因,没有迁移脚本或工具可以将工件从 Nexus 实际移动到 Bintray。我正在寻找一种将 Nexus 存储库(私有或 public)轻松迁移到 Bintray 的解决方案。

我知道 Bintray 有 REST API 似乎你可以编写一个脚本来创建项目版本,然后从 Nexus 一个一个地上传工件,但出于某种原因我不断从这个 REST 中收到错误 API 并且无法提供实际有效的东西。

目前没有专门用于此类迁移的工具。 REST API 确实是这样做的方法。

@JBaruch 已经提到没有工具可以做到这一点,所以我不得不编写脚本来做到这一点。脚本可能非常脆弱、缓慢且难以维护,因此请谨慎使用。另请注意,脚本非常基础,之后您可能需要手动修复一些东西。

# run inside of your nexus repo directory, like /opt/nexus/sonatype_work/my_maven_repository

BINTRAY_USER=""
BINTRAY_TOKEN=""
BINTRAY_API="https://api.bintray.com"
BINTRAY_REPO="your_org/maven"

for META_FILE in `find . -type f -name 'maven-metadata.xml' ! -path "*/.nexus/*"  ! -path "*/.meta/*" ! -path "*/.index/*"`;
do
    echo "Meta-file: $META_FILE"
    PRODUCT_DIR=`dirname ${META_FILE#./}`
    PRODUCT_ID=`basename $PRODUCT_DIR`
    PACKAGE_DIR=`dirname $PRODUCT_DIR`
    PACKAGE=`echo $PACKAGE_DIR | tr '/' '.'`
    echo "Found product $PRODUCT_ID in $PACKAGE at path $PRODUCT_DIR"

    # make project on bintray
    PROJECT_ID="$PACKAGE:$PRODUCT_ID"
    PROJ_TMPL='{
        "name": "%s",
        "desc": "%s",
        "licenses": ["Apache-2.0"],
        "vcs_url": "https://github.com/your_org/",
        "public_download_numbers": false,
        "public_stats": true
    }'
    PROJ_DEF=`printf "$PROJ_TMPL" $PROJECT_ID $PACKAGE.$PRODUCT_ID`
    PROJ_URL="$BINTRAY_API/packages/$BINTRAY_REPO"

    echo "Posting $PROJ_DEF to $PROJ_URL"
    curl -H "Content-Type: application/json" -d "$PROJ_DEF" -u$BINTRAY_USER:$BINTRAY_TOKEN $PROJ_URL
    echo ""

    for VERSION_DIR in `find $PRODUCT_DIR/* -maxdepth 1 -type d ! -name . ! -name .. ! -name '.meta' ! -name '.index' ! -name '.nexus'`;
    do
        VERSION=`basename $VERSION_DIR`
        echo "Found version $VERSION of product $PACKAGE.$PRODUCT_ID"

        # make version on bintray
        VERSION_TMPL='{
            "name": "%s",
            "desc": "%s"
        }'
        VERSION_DEF=`printf "$VERSION_TMPL" $VERSION "$VERSION release of $PACKAGE:$PRODUCT_ID"`
        VERSION_URL="$BINTRAY_API/packages/$BINTRAY_REPO/$PROJECT_ID/versions"
        echo "Posting $VERSION_DEF to $VERSION_URL"

        curl -H "Content-Type: application/json" -d "$VERSION_DEF" -u$BINTRAY_USER:$BINTRAY_TOKEN $VERSION_URL
        echo ""

        for FILE in `find $VERSION_DIR -type f`;
        do
            URI="$BINTRAY_API/content/$BINTRAY_REPO/$PROJECT_ID/$VERSION/$FILE;publish=1"
            echo "Uploading file $FILE to $URI"
            curl -T $FILE -u$BINTRAY_USER:$BINTRAY_TOKEN $URI
            echo ""
        done
    done
done
echo "+--------------------------------+"
echo "| Migration script finished! \o/ |"
echo "+================================+"