I want to use Gradle, but I do not want to install it. It is possible to compile a project using its Gradle wrapper. If you already have a Gradle project, you can initialize a new project using the existing Gradle wrapper: /path/to/existing/project/gradlew init
.
But to create your first project, you must, according to the Gradle docs, install Gradle, and never use it since then.
How do I get (download/create/initialize) a Gradle wrapper without installing Gradle?
I wrote a script to get a Gradle wrapper without installing Gradle.
File createwrapper:
#!/bin/bash
if [ -z "$1" ]; then
version='6.2.1'
else
version=$1
fi
rm -rf $version
mkdir -p $version
mkdir -p gradle/wrapper
pushd gradle/wrapper
wget https://raw.githubusercontent.com/gradle/gradle/master/gradle/wrapper/gradle-wrapper.jar
cat >gradle-wrapper.properties <<EOL
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https://services.gradle.org/distributions/gradle-${version}-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
EOL
popd
pushd $version
echo -e "1n1nn" | java -cp ../gradle/wrapper/gradle-wrapper.jar org.gradle.wrapper.GradleWrapperMain init
popd
After running e.g. createwrapper 8.9
(the parameter is a Gradle version), you get a subdirectory, 8.9
in this case, with a Gradle project in it. What’s important, that project has a Gradle wrapper that you can use to init
a new project.
What does the script do: it downloads the latest gradle-wrapper.jar
, gives it gradle-wrapper.properties
with the desired version, and invokes the init
task from that jar, choosing the 1st option (2 times) and accepting the default project name (see the echo -e
parameter). It is possible that in the future option numbers will change their meaning and a longer sequence of options will be required.
What you get:
$ tree
.
├── 6.2.1
│ ├── build.gradle
│ ├── gradle
│ │ └── wrapper
│ │ ├── gradle-wrapper.jar
│ │ └── gradle-wrapper.properties
│ ├── gradlew
│ ├── gradlew.bat
│ └── settings.gradle
├── 8.9
│ ├── build.gradle.kts
│ ├── gradle
│ │ ├── libs.versions.toml
│ │ └── wrapper
│ │ ├── gradle-wrapper.jar
│ │ └── gradle-wrapper.properties
│ ├── gradlew
│ ├── gradlew.bat
│ └── settings.gradle.kts
├── createwrapper
└── gradle
└── wrapper
├── gradle-wrapper.jar
├── gradle-wrapper.jar.1
└── gradle-wrapper.properties
(As you see, in one project there is build.gradle
, in the other there is build.gradle.kts
, this must be because the same numbers have different meanings in different versions.)
Useful link: http://blog.vorona.ca/init-gradle-wrapper-without-gradle.html (does not work for Gradle 8.9)