This directory provides a Gradle example.
Dependency
The library JLine is required to build the project and has been integrated within this Git repository as a submodule. The following lines are an alternative to git-submodule:
cd your-github-directory
git clone https://github.com/jline/jline3
git -C jline3 checkout jline-3.1.2
cd CodingGoodPractices/Gradle/example
ln -s your-github-directory/jline3 .
Start scripts
The Gradle plugin βapplicationβ offers tasks to start the Java application:
gradle :gui:run # To start the GUI application
gradle :cli:run # To enter in interactive shell
Moreover, the task startScripts
can produce start scripts:
$ rm build/scripts -r
$ gradle startScripts
[...]
:cli:startScripts
== Start script build/scripts/cli
:core:cmdLineAdminStartScript
== Start script build/scripts/admin_station
:core:cmdLineIgwStartScript
== Start script build/scripts/igw
:core:startScripts
== Start script build/scripts/core
BUILD SUCCESSFUL
Total time: 0.863 secs
The scripts are all located together:
build/scripts/cli
build/scripts/cli.bat
build/scripts/gui
build/scripts/gui.bat
Limitations
The task β:cli:run
β is not currently usable because of missing input/output streams from application process.
The issue has been reported on JLine project.
The workaround is to use the generated scripts.
However the generated scripts are buggy.
Instead of fixing the generated scripts or hacking the input Gradle script, a simple workaround is to create some symlinks:
mkdir build/lib
( cd build/lib && ln -s ../*/libs/* ../../lib/* . )
Usage
The rest of this chapter focus only on script build/scripts/cli
.
cli
cli -h|--help
cli [-e|--expression "function arg1 arg2..."] [-i|--interactive]
Without argument, the application enters by default in interactive shell.
The option --expression
is similar to interactive shell:
the provided βfunction arg1 arg2...
β is processed as in interactive shell.
Available expressions
An expression is based on a function name (keyword) and arguments in a similar way as Robot Framework scenario.
There is only two expressions currently implemented: <set|get> myField
IDE
In order to maintain only one single build process, the developer generates the project files from the Gradle script.
cd Card/cardmgr gradle generateJava eclipse
The Gradle task βgenerateJavaβ produces the Version.java and JNI swig-generated files required by the task βeclipseβ in order to provide a clean Eclipse project files.
The Gradle script also supports the IntelliJ IDEA:
cd Card/cardmgr
gradle generateJava idea
The Gradle-based project can also be imported to NetBeans.
Check
The Gradle script offers coding-style checking, static code analysis and unit test:
$ cd Card/cardmgr
$ gradle check
[...]
:base:checkstyleMain
== Checkstyle reports available in 'build/base/reports'
:base:checkstyleTest
:base:findbugsMain
Scanning archives (42 / 42)
2 analysis passes to perform
Pass 1: Analyzing classes (465 / 465) - 100% complete
Pass 2: Analyzing classes (41 / 41) - 100% complete
Done with analysis
FindBugs rule violations were found. See the report at: build/base/reports/findbugs/main.html
:base:findbugsTest
:base:jdependMain
== JDepend reports available in 'build/base/reports'
:base:jdependTest
:base:pmdMain
7 PMD rule violations were found. See the report at: build/core/reports/pmd/main.html
:base:pmdTest
:base:test
[...]
The current Gradle script integrates the following checks:
- Checkstyle (coding rules)
- FindBugs (bytecode static analysis)
- JDepend (circular dependency)
- PMD (detect inefficient Java source code)
- JUnit (Unit test)
The reports are produced in the following directories:
build
βββ base
β βββ reports
β βββ checkstyle
β β βββ main.html # For humans
β β βββ main.xml # For Jenkins
β βββ findbugs
β β βββ main.html # FindBugs produce one format at a time: HTML or XML
β βββ jdepend
β β βββ main.txt # JDepends only produce TXT output
β βββ pmd
β βββ main.html # For humans
[...] βββ main.xml # For Jenkins
β
βββ core
βββ reports
β βββ checkstyle
β β βββ main.html
β β βββ main.xml
β β βββ test.html # Unit-Test code is checked but in a separated section
β β βββ test.xml
β βββ findbugs
β β βββ main.html
β β βββ test.html
β βββ jdepend
β β βββ main.txt
β β βββ test.txt
β βββ pmd
β β βββ main.html
β β βββ main.xml
β β βββ test.html
β β βββ test.xml
β βββ tests
β βββ test
β βββ index.html # JUnit result for humans in /reports/tests/test
βββ test-results # JUnit result for Jenkins in /test-results/test
βββ test
βββ TEST-com.company.appname.core.TrucTest.xml
βββ TEST-com.company.appname.core.MachinTest.xml
Tips
gradle tasks --all # List all available tasks
gradle help --task test # Basic information on the task "test"
gradle :cli:build :cli:taskTree --no-repeat # Display dependency graph (ASCII) of task ":cli:build"