Skip to content

Instantly share code, notes, and snippets.

@leolopez89
Last active July 13, 2022 14:54
Show Gist options
  • Save leolopez89/2ad326dfc8e292ce4d67f0dbfee0da3e to your computer and use it in GitHub Desktop.
Save leolopez89/2ad326dfc8e292ce4d67f0dbfee0da3e to your computer and use it in GitHub Desktop.
How to convert a gradle repository into a maven repository
#!/bin/bash
#
# Usage:
#
# bash gradle2maven.sh
# Will be used as default directories
# your default gradle reprository: $HOME/.gradle/caches/modules-2/files-2.1/
# in your current path, a new maven directory: $PWD/maven-XXXXX`/repository
#
# bash gradle2maven.sh <gradle-files2.1-path>
# It will use your prefered gradle files2.1 path and your current path
#
# bash gradle2maven.sh <gradle-files2.1-path> <your-prefered-output-path>
# It will use your prefered gradle files2.1 and output paths
#
current=$PWD
path=$1
[ "${path}" == "" ] && path=$HOME/.gradle/caches/modules-2/files-2.1/
output=$2
[ "${output}" == "" ] && output=`mktemp -d $current/maven-XXXXX`/repository
mkdir -vp $output
cd ${path}
for file in `find . -regex '.*\(pom\|jar\|aar\|exe\)'`; do
echo "Processing $file"
folderCreate=`echo $file | awk -v file=$file -v output=$output -F/ '{
cad = $2;
gsub(/\./, "/", cad);
print "mkdir -p " output "/" cad "/" $3 "/" $4 "/ "
}'`
fileCopy=`echo $file | awk -v file=$file -v output=$output -F/ '{
cad = $2;
gsub(/\./, "/", cad);
print "cp " file " " output "/" cad "/" $3 "/" $4 "/" $6
}'`
$folderCreate
$fileCopy
done
cd $current
@leolopez89
Copy link
Author

Gradle To Maven Description

Usage

  • bash gradle2maven.sh
    Will be used as default directories

    • your default gradle reprository: $HOME/.gradle/caches/modules-2/files-2.1/
    • in your current path, a new maven directory: $PWD/maven-XXXXX`/repository
  • bash gradle2maven.sh {gradle-files2.1-path}
    It will use your prefered gradle files2.1 path and your current path

  • bash gradle2maven.sh {gradle-files2.1-path} {your-prefered-output-path}
    It will use your prefered gradle files2.1 and output paths

Description

This script is for creating a maven repository from your gradle repository. It will allow you to reuse your downloaded files on your own device and on other devices, because a Gradle repository is not portable to another host.

Example output

Having these gradle dependencies downloaded:
files-2.1/androidx.databinding/databinding-common/3.3.1/2e932d3219ea5f6d1078b24a8a3ce24b209c9309/databinding-common-3.3.1.pom
files-2.1/androidx.databinding/databinding-common/3.2.1/91c10c7ce255ab29ec4aea8a469d6c8ffa60bd53/databinding-common-3.2.1.jar

You will get:
maven-xxxxx/repository/androidx/databinding/databinding-common/3.2.1/databinding-common-3.2.1.pom
maven-xxxxx/repository/androidx/databinding/databinding-common/3.3.1/databinding-common-3.3.1.pom

You should create a directory $HOME/.m2/ and add your repository directory inside it, like so:
$HOME/.m2/repository

Now, in your Android project (for example), you can add to your build.gradle file:
mavenCentral()
as your first build repository. And gradle will look for the dependencies first on your computer, then on the internet

Notes

  • If you have problems with gradle dependencies, you can save your downloaded gradle dependencies with this script and safely delete your gradle directory and save many megabytes and download time.

  • If you want to start any android (flutter,react native, et) project in a new computer, you can convert your old gradle repository and reuse it in the new device, saving time.

@eliastg
Copy link

eliastg commented Jul 13, 2022

Very useful tool.
I have personally used it to fix some problems in React Native projects.
I am very happy that you have finally published.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment