Skip to content

Instantly share code, notes, and snippets.

@bahmanm bahmanm/gradle-init.sh
Last active Jan 30, 2019

Embed
What would you like to do?
Groovy and Spock (with Gradle)
#!/usr/bin/env bash
###
# Initialises a Gradle project with Groovy and Spock.
#
# How To Use?
#
# 1. Save the file, e.g. `gradle-init.sh`
# 2. Make it executable: `$ chmod +x gradle-init.sh`
# 3. Initialise the project: `$ ./gradle-init.sh YOUR_PROJECT_NAME`
# 4. Run your project: `$ cd YOUR_PROJECT_NAME && gradle run`
###
pushd () {
command pushd "$@" > /dev/null
}
popd () {
command popd "$@" > /dev/null
}
PROJECT_NAME=$1
if [ -z $PROJECT_NAME ]; then
echo You need to pass the project name as the argument.
exit
fi
if [ -d $PROJECT_NAME ]; then
echo The directory already exists.
fi
PACKAGE_NAME=`echo $PROJECT_NAME | \
tr '[:upper:]' '[:lower:]' | \
tr '[:punct:][:blank:]\-' '_' \
`
if [ -z $PACKAGE_NAME ]; then
echo Invalid project name. Cannot figure out a decent package name.
exit
fi
mkdir $PROJECT_NAME && pushd $PROJECT_NAME
#########################################################################
echo Setting up Gradle...
sed -e "s/\$PROJECT_NAME/$PROJECT_NAME/" > build.gradle <<EOF
// Author: Bahman Movaqar <Bahman@BahmanM.com>
plugins {
id 'groovy'
id 'application'
}
repositories {
jcenter()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.5.5:indy'
compile 'org.slf4j:slf4j-api:1.7.25'
compile 'ch.qos.logback:logback-classic:1.2.3'
testCompile 'org.spockframework:spock-core:1.2-groovy-2.5'
}
mainClassName = '$PROJECT_NAME.Main'
EOF
sed -e "s/\$PROJECT_NAME/$PROJECT_NAME/" > settings.gradle <<EOF
rootProject.name = '$PROJECT_NAME'
EOF
#########################################################################
echo Setting up source directories...
DIR="src/main/groovy/$PACKAGE_NAME/"
mkdir -p $DIR && pushd $DIR
sed -e "s/\$PACKAGE_NAME/$PACKAGE_NAME/" > Main.groovy <<EOF
package $PACKAGE_NAME
class Main {
static void main(String[] args) {
println('Hello, world')
}
}
EOF
popd
#########################################################################
echo Setting up test directories...
DIR="src/test/groovy/$PACKAGE_NAME/"
mkdir -p $DIR && pushd $DIR
sed -e "s/\$PACKAGE_NAME/$PACKAGE_NAME/" > MainSpec.groovy <<EOF
package $PACKAGE_NAME
import spock.lang.*
class MainSpec extends Specification {
def 'Dummy test'() {
assert true
}
}
EOF
popd
#########################################################################
echo Setting up git...
if [ -z `which git` ]
then
echo git does not appear to be installed.
echo I will not initialise the repository.
else
git init
fi
cat > .gitignore <<EOF
.gradle
/build/
# Ignore Gradle GUI config
gradle-app.setting
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar
# Cache of project
.gradletasknamecache
# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898
# gradle/wrapper/gradle-wrapper.properties
.projectile
*\#*
*\~*
EOF
#########################################################################
echo Setting up Emacs...
touch .projectile
#########################################################################
echo Done.
echo Happy coding.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.