Install and Usage
The order should be:
-
Install Oracle JDK on system: downlaod, extract, define $JAVA_HOME and add $JAVA_HOME/bin folder to $PATH in ~/.zshrc;
-
Install Gradle on System: downlaod, extract, define $GRADLE_HOME and add $GRADLE_HOME/bin folder to $PATH in ~/.zshrc;
-
Install Eclipse: download, extract;
-
Install Gradle plugin for eclipse via "Market Place" in Eclipse, because it's inconvenient to run gradle tasks in console while edit files in Eclipse;
-
Configure Gradle plugin: set its working JDK;
-
Build a new Gradle project in Eclipse, add git version control;
-
Run Gradle tasks: right click "build.gradle" file, then select "Gradle -> Tasks Quick Launcher" (or Shift + Alt + Ctrl +R) to bring Gradle launcher, then input tasks name.
That's it.
Frequently Used Tasks
Some need adding apply plugin: 'application'
and
mainClassName = 'com.boco.gdep.Probe'
into build.gradle.
Read Gradle Application Plugin for details.
- clean
- check
- test
- compileJava
- run
- startScripts: create start scripts for your application;
- distZip: build final deploy package, find zip file under build/distributions folder;
- installApp: install package created in "distZip" into build/install folder;
Note that in development stage, testing codes with JUnit
(using F11 to launch last dubug) is better than using Gradle's test task
(using Shift + Ctrl + Alt + R, then input "test" and
All these tasks can be executed within Eclipse and in console. So it works good both in development stage and BVT stage. A sample project using these tools is my GDEP project.
Debug Gradle with println
Add the following scripts into build.gradle, then run "check" task.
println "--------"
println project.name
println "--------"
Customize project name
Add a new file "settings.gradle" at the root of the project, add a string "rootProject.name = 'myProj'" into the file. Now project name, jar file name, start script name will all be changed to myProject.jar/sh/zip, etc.
First install gradle, then run the following commands:
$ mkdir myproj
$ cd myproj
$ gradle init --type java-library
$ mkdir src/main/resources
$ mkdir src/test/resources
Now the project layout is ready. If you want to develop this project in Eclipse, add "apply plugin: 'eclipse'" into build.gradle, then run:
$ gradle eclipse
Then open this project with Eclipse.
The default project layout (file structure) of "java" plugin is described in "Project layout" section of Gradle User Guide:
src/main/java: Production Java source src/main/resources: Production resources src/test/java: Test Java source src/test/resources: Test resources src/sourceSet/java: Java source for the given source set src/sourceSet/resources: Resources for the given source set
Add git version control with:
$ cd myproj
$ git init
In Eclipse Luna, the git plugin is build-in, so you can manage versions in command line or in eclipse.
Gradle will create many help files and folders, which is not the contents of the project. So we need exclude them using .gitignore:
$ cat .gitignore
build
.gradle
gradle
...
Three Eclipse component ".settings", ".project" and ".classpath" should be added to git repository.
Merge Github Project and Eclipse Gradle Project
-
git clone https://github.com/leetschau/GDEP.git gdep2
-
In Eclipse, build a new Gradle project "GDEP-0811" with "Quick start" template;
-
Move all files and folders in gdep2 to GDEP-0811;
-
Refresh project in Eclipse;
-
Build project with [right click on project -> Gradle -> Tasks Quick Launcher], enter "build", and press
. If you encounter a "Cannot find System Java Compiler" error, set Gradle's Java compiler in [Window -> Preferences -> Gradle -> Arguments -> Java Home], select "Workspace JRE": jdk1.6.0_45 (which is the JDK of you system, you have to install it yourself).