Skip to content

Instantly share code, notes, and snippets.

@alcarvalho
Last active May 3, 2016 16:28
Show Gist options
  • Save alcarvalho/26fb041de96f6cf30e91 to your computer and use it in GitHub Desktop.
Save alcarvalho/26fb041de96f6cf30e91 to your computer and use it in GitHub Desktop.
Script to set gradle's jvm to Java 8 inside IntelliJ installations on Mac OS X.
#!/bin/bash
# You can run it by copying and pasting the following on your terminal:
# curl "https://gist.githubusercontent.com/alcarvalho/26fb041de96f6cf30e91/raw/intellij_gradle_java8.sh" | bash
function die() {
echo $1
exit 1
}
function get_java8_home() {
LIBEXEC_JAVA_HOME=$(which java_home || echo /usr/libexec/java_home)
test -x $LIBEXEC_JAVA_HOME || die "Could not find java_home. It should live in /usr/libexec/java_home"
JAVA8_HOME=$($LIBEXEC_JAVA_HOME -v 1.8)
echo $JAVA8_HOME
}
function patch_idea_properties() {
prop_file=$1
echo "Patching $prop_file..."
if [ -z $(grep gradle.java.home $prop_file) ] ; then
echo "Backing up..."
cp $prop_file $prop_file~
echo "gradle.java.home=$JAVA8_HOME" >> $prop_file
echo "Done."
else
echo "Nothing to be done here."
fi
}
JAVA8_HOME=${JAVA8_HOME-$(get_java8_home)}
test -z "$JAVA8_HOME" && die "JAVA8_HOME is not set and I have failed to auto set it"
echo "Finding IntelliJ installations and patching them..."
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for f in /Applications/{Android\ Studio*,Intelli*}.app ; do
echo "Found $f"
idea_props=$(find "$f" -name idea.properties | head -n 1)
patch_idea_properties $idea_props
done
IFS=$SAVEIFS
echo "All done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment