Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save davosian/e5db6f80c9bdc034cab84bea8fd17f67 to your computer and use it in GitHub Desktop.

Select an option

Save davosian/e5db6f80c9bdc034cab84bea8fd17f67 to your computer and use it in GitHub Desktop.

Revisions

  1. @karlvr karlvr revised this gist Nov 10, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion migrate-nexus-to-github-packages.md
    Original file line number Diff line number Diff line change
    @@ -18,4 +18,4 @@ and repeat the process.
    3. Create or edit your `~/.m2/settings.xml` to include a `<repository>` and `<server>` element as in the example file, substituting your username and personal access token.
    4. Run the script `migrate-nexus-to-github-packages.sh /srv/nexus/storage/releases <owner> maven-releases github`

    It is possible to paralellise the script, as GitHub is pretty slow at accepting each package (~30s / package). I recommend parallelising by artifact, as Maven isn't thread safe in the way it writes to its files. I achieved that quite simply by spawning individual scripts, one for each artifact, and running them in the background.
    It is possible to paralellise the script, as GitHub is pretty slow at accepting each package (~30s / package). I recommend parallelising by artifact, as Maven isn't thread safe in the way it writes to its files. I achieved that quite simply by spawning individual scripts, one for each artifact, and running them in the background. Beware that Maven actually uses a bit of memory and CPU, so doing too many artifacts in parallel might overload your system.
  2. @karlvr karlvr revised this gist Nov 10, 2020. 2 changed files with 4 additions and 2 deletions.
    4 changes: 3 additions & 1 deletion migrate-nexus-to-github-packages.md
    Original file line number Diff line number Diff line change
    @@ -16,4 +16,6 @@ and repeat the process.
    1. Create the `maven-releases` GitHub repository as a private (or public) repository.
    2. Create a personal access token for your GitHub account with permissions: `repo` and `write:packages`.
    3. Create or edit your `~/.m2/settings.xml` to include a `<repository>` and `<server>` element as in the example file, substituting your username and personal access token.
    4. Run the script `migrate-nexus-to-github-packages.sh /srv/nexus/storage/releases <owner> maven-releases github`
    4. Run the script `migrate-nexus-to-github-packages.sh /srv/nexus/storage/releases <owner> maven-releases github`

    It is possible to paralellise the script, as GitHub is pretty slow at accepting each package (~30s / package). I recommend parallelising by artifact, as Maven isn't thread safe in the way it writes to its files. I achieved that quite simply by spawning individual scripts, one for each artifact, and running them in the background.
    2 changes: 1 addition & 1 deletion migrate-nexus-to-github-packages.sh
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@ for pom in $(find "$basedir" \( -name '.nexus' \) -prune -false -o -name '*.pom'
    javadoc="$dir/$artifact-$version-javadoc.jar"
    pomfile="$dir/$artifact-$version.pom"

    command="mvn org.apache.maven.plugins:maven-deploy-plugin:3.0.0-M1:deploy-file \
    command="mvn -q org.apache.maven.plugins:maven-deploy-plugin:3.0.0-M1:deploy-file \
    -DrepositoryId=$serverid \
    -Durl=https://maven.pkg.github.com/$owner/$repository"

  3. @karlvr karlvr revised this gist Nov 10, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion migrate-nexus-to-github-packages.sh
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ ctrl_c() {
    }

    # Find poms in artifact and version order
    for pom in $(find "$basedir" -name '*.pom' | sort --version-sort) ; do
    for pom in $(find "$basedir" \( -name '.nexus' \) -prune -false -o -name '*.pom' | sort --version-sort) ; do
    dir=$(dirname $pom)
    dir=$(cd "$dir" && pwd)
    if [ -f "$dir/.migrated-github-packages" ]; then
  4. @karlvr karlvr renamed this gist Nov 10, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. @karlvr karlvr revised this gist Nov 10, 2020. 1 changed file with 9 additions and 4 deletions.
    13 changes: 9 additions & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,17 @@
    # Migrate Maven packages from Sonatype Nexus to GitHub Packages

    This gist describes the process we've used to migrate our Maven package repository from Sonatype Nexus
    to GitHub Packages.
    to GitHub Packages. The same process could be used for migrating any Maven package repo that is in
    the standard layout.

    We created a special repository on GitHub to hold all of our Maven packages. You might decide to migrate
    packages to different repositories, in which case invoke the script multiple times.

    The script uses `find` to look for all of the folders containing poms and uploading them. A hidden
    `.migrated-github-packages` file is created in each folder that's processed so you can interrupt
    and repeat.
    The script uses `find` to look for all of the folders containing poms and upload them. You specify the folder
    to start from, so you upload part of your repository.

    A hidden `.migrated-github-packages` file is created in each folder that's processed so you can interrupt
    and repeat the process.

    1. Create the `maven-releases` GitHub repository as a private (or public) repository.
    2. Create a personal access token for your GitHub account with permissions: `repo` and `write:packages`.
  6. @karlvr karlvr revised this gist Nov 10, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion migrate-nexus-to-github-packages.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #!/bin/bash -eu

    basedir="${1:-}" # The directory to start looking for poms from
    owner="${2:-}" # The owner usernae on GitHub
    owner="${2:-}" # The owner username on GitHub
    repository="${3:-}" # The repository name on GitHub
    serverid="${4:-}" # Matches to <server> in ~/.m2/settings.xml

  7. @karlvr karlvr created this gist Nov 10, 2020.
    14 changes: 14 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    This gist describes the process we've used to migrate our Maven package repository from Sonatype Nexus
    to GitHub Packages.

    We created a special repository on GitHub to hold all of our Maven packages. You might decide to migrate
    packages to different repositories, in which case invoke the script multiple times.

    The script uses `find` to look for all of the folders containing poms and uploading them. A hidden
    `.migrated-github-packages` file is created in each folder that's processed so you can interrupt
    and repeat.

    1. Create the `maven-releases` GitHub repository as a private (or public) repository.
    2. Create a personal access token for your GitHub account with permissions: `repo` and `write:packages`.
    3. Create or edit your `~/.m2/settings.xml` to include a `<repository>` and `<server>` element as in the example file, substituting your username and personal access token.
    4. Run the script `migrate-nexus-to-github-packages.sh /srv/nexus/storage/releases <owner> maven-releases github`
    58 changes: 58 additions & 0 deletions migrate-nexus-to-github-packages.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    #!/bin/bash -eu

    basedir="${1:-}" # The directory to start looking for poms from
    owner="${2:-}" # The owner usernae on GitHub
    repository="${3:-}" # The repository name on GitHub
    serverid="${4:-}" # Matches to <server> in ~/.m2/settings.xml

    if [ -z "$basedir" -o -z "$owner" -o -z "$repository" -o -z "$serverid" ]; then
    echo "usage: $0 <basedir> <owner> <repository> <serverid>" >&2
    exit 1
    fi

    trap ctrl_c INT

    ctrl_c() {
    echo "Interrupted" >&2
    exit 1
    }

    # Find poms in artifact and version order
    for pom in $(find "$basedir" -name '*.pom' | sort --version-sort) ; do
    dir=$(dirname $pom)
    dir=$(cd "$dir" && pwd)
    if [ -f "$dir/.migrated-github-packages" ]; then
    continue
    fi

    version=$(basename $dir)
    artifact=$(basename $(dirname $dir))
    jar="$dir/$artifact-$version.jar"
    sources="$dir/$artifact-$version-sources.jar"
    javadoc="$dir/$artifact-$version-javadoc.jar"
    pomfile="$dir/$artifact-$version.pom"

    command="mvn org.apache.maven.plugins:maven-deploy-plugin:3.0.0-M1:deploy-file \
    -DrepositoryId=$serverid \
    -Durl=https://maven.pkg.github.com/$owner/$repository"

    if [ -f "$jar" ]; then
    command="$command -Dfile=\"$jar\""
    elif [ -f "$pomfile" ]; then
    command="$command -Dfile=\"$pomfile\""
    fi
    if [ -f "$sources" ]; then
    command="$command -Dsources=\"$sources\""
    fi
    if [ -f "$javadoc" ]; then
    command="$command -Djavadoc=\"$javadoc\""
    fi
    if [ -f "$pomfile" ]; then
    command="$command -DpomFile=\"$pomfile\""
    fi

    echo "$pomfile"

    eval $command
    touch "$dir/.migrated-github-packages"
    done
    38 changes: 38 additions & 0 deletions settings.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
    http://maven.apache.org/xsd/settings-1.0.0.xsd">

    <activeProfiles>
    <activeProfile>github</activeProfile>
    </activeProfiles>

    <profiles>
    <profile>
    <id>github</id>
    <repositories>
    <repository>
    <id>central</id>
    <url>https://repo1.maven.org/maven2</url>
    <releases><enabled>true</enabled></releases>
    <snapshots><enabled>true</enabled></snapshots>
    </repository>
    <repository>
    <id>github-REPOSITORY</id>
    <name>GitHub OWNER Apache Maven Packages</name>
    <url>https://maven.pkg.github.com/OWNER/REPOSITORY</url>
    <releases><enabled>true</enabled></releases>
    <snapshots><enabled>false</enabled></snapshots>
    </repository>
    </repositories>
    </profile>
    </profiles>

    <servers>
    <server>
    <id>github</id>
    <username>USERNAME</username>
    <password>PERSONAL_ACCESS_TOKEN</password>
    </server>
    </servers>
    </settings>