引用
APP_PROJECT_ROOT
|-- build.gradle
|-- build/
`-- src
|-- main
| |-- groovy/
| |-- java/
| `-- resources/
`-- test
|-- groovy/
|-- java/
`-- resources/
|-- build.gradle
|-- build/
`-- src
|-- main
| |-- groovy/
| |-- java/
| `-- resources/
`-- test
|-- groovy/
|-- java/
`-- resources/
但是目前Gradle不像Maven那样通过命令行就能生成模板工程。可以自己写脚本(Task)生成模板工程,也可以使用Plugin。
按照以下步骤安装插件:
(1)从https://launchpad.net/gradle-templates 下载templates-1.2.jar 后放入一个新建的文件夹$GRADLE_HOME\customPlugins中。
(2)编写一个build.gradle
引用
buildscript{
repositories {
flatDir dirs: "${gradle.gradleHomeDir}/customPlugins"
}
dependencies {
classpath ':templates:1.2'
}
}
apply plugin: 'templates'
repositories {
flatDir dirs: "${gradle.gradleHomeDir}/customPlugins"
}
dependencies {
classpath ':templates:1.2'
}
}
apply plugin: 'templates'
(3)执行脚本
引用
D:\>gradle --stacktrace
:help
Welcome to Gradle 1.0.
To run a build, run gradle <task> ...
To see a list of available tasks, run gradle tasks
To see a list of command-line options, run gradle --help
BUILD SUCCESSFUL
Total time: 5.188 secs
:help
Welcome to Gradle 1.0.
To run a build, run gradle <task> ...
To see a list of available tasks, run gradle tasks
To see a list of command-line options, run gradle --help
BUILD SUCCESSFUL
Total time: 5.188 secs
(4)查看Task
引用
D:\>gradle task
:tasks
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Help tasks
----------
dependencies - Displays the dependencies of root project ''.
help - Displays a help message
projects - Displays the sub-projects of root project ''.
properties - Displays the properties of root project ''.
tasks - Displays the tasks runnable from root project '' (some of the displayed tasks may belong to
subprojects).
Template tasks
--------------
createGradlePlugin - Creates a new Gradle Plugin project in a new directory named after your projec
.
createGroovyClass - Creates a new Groovy class in the current project.
createGroovyProject - Creates a new Gradle Groovy project in a new directory named after your proje
t.
createJavaClass - Creates a new Java class in the current project.
createJavaProject - Creates a new Gradle Java project in a new directory named after your project.
createScalaClass - Creates a new Scala class in the current project.
createScalaObject - Creates a new Scala object in the current project.
createScalaProject - Creates a new Gradle Scala project in a new directory named after your project
createWebappProject - Creates a new Gradle Webapp project in a new directory named after your proje
t.
exportAllTemplates - Exports all the default template files into the current directory.
exportGroovyTemplates - Exports the default groovy template files into the current directory.
exportJavaTemplates - Exports the default java template files into the current directory.
exportPluginTemplates - Exports the default plugin template files into the current directory.
exportScalaTemplates - Exports the default scala template files into the current directory.
exportWebappTemplates - Exports the default webapp template files into the current directory.
initGradlePlugin - Initializes a new Gradle Plugin project in the current directory.
initGroovyProject - Initializes a new Gradle Groovy project in the current directory.
initJavaProject - Initializes a new Gradle Java project in the current directory.
initScalaProject - Initializes a new Gradle Scala project in the current directory.
initWebappProject - Initializes a new Gradle Webapp project in the current directory.
To see all tasks and more detail, run with --all.
BUILD SUCCESSFUL
Total time: 3.766 secs
:tasks
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Help tasks
----------
dependencies - Displays the dependencies of root project ''.
help - Displays a help message
projects - Displays the sub-projects of root project ''.
properties - Displays the properties of root project ''.
tasks - Displays the tasks runnable from root project '' (some of the displayed tasks may belong to
subprojects).
Template tasks
--------------
createGradlePlugin - Creates a new Gradle Plugin project in a new directory named after your projec
.
createGroovyClass - Creates a new Groovy class in the current project.
createGroovyProject - Creates a new Gradle Groovy project in a new directory named after your proje
t.
createJavaClass - Creates a new Java class in the current project.
createJavaProject - Creates a new Gradle Java project in a new directory named after your project.
createScalaClass - Creates a new Scala class in the current project.
createScalaObject - Creates a new Scala object in the current project.
createScalaProject - Creates a new Gradle Scala project in a new directory named after your project
createWebappProject - Creates a new Gradle Webapp project in a new directory named after your proje
t.
exportAllTemplates - Exports all the default template files into the current directory.
exportGroovyTemplates - Exports the default groovy template files into the current directory.
exportJavaTemplates - Exports the default java template files into the current directory.
exportPluginTemplates - Exports the default plugin template files into the current directory.
exportScalaTemplates - Exports the default scala template files into the current directory.
exportWebappTemplates - Exports the default webapp template files into the current directory.
initGradlePlugin - Initializes a new Gradle Plugin project in the current directory.
initGroovyProject - Initializes a new Gradle Groovy project in the current directory.
initJavaProject - Initializes a new Gradle Java project in the current directory.
initScalaProject - Initializes a new Gradle Scala project in the current directory.
initWebappProject - Initializes a new Gradle Webapp project in the current directory.
To see all tasks and more detail, run with --all.
BUILD SUCCESSFUL
Total time: 3.766 secs
(5)创建Java模板工程
引用
D:\>gradle createJavaProject
> Building > :createJavaProject
??> Project Name: TestRen
??> Group: [testren] testgroup
??> Version: [1.0] 1.0
:createJavaProject
BUILD SUCCESSFUL
Total time: 30.578 secs
> Building > :createJavaProject
??> Project Name: TestRen
??> Group: [testren] testgroup
??> Version: [1.0] 1.0
:createJavaProject
BUILD SUCCESSFUL
Total time: 30.578 secs
(6)生成的工程
引用
TestRen
│ build.gradle
│ gradle.properties
│ LICENSE.txt
│
└─src
├─main
│ ├─java
│ └─resources
└─test
├─java
└─resources
│ build.gradle
│ gradle.properties
│ LICENSE.txt
│
└─src
├─main
│ ├─java
│ └─resources
└─test
├─java
└─resources
(7)使用STS(SpringSource Tool Suite)导入工程,以后的构建就很方便了。