Gradle入门(2) -- 创建模板工程

本文介绍如何使用Gradle插件创建标准的Java项目模板。通过下载特定插件并配置build.gradle文件,用户能够轻松生成包含基本目录结构的Java项目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gradle的标准模板和Maven的是一样的:

[quote]APP_PROJECT_ROOT
|-- build.gradle
|-- build/
`-- src
|-- main
| |-- groovy/
| |-- java/
| `-- resources/
`-- test
|-- groovy/
|-- java/
`-- resources/[/quote]

但是目前Gradle不像Maven那样通过命令行就能生成模板工程。可以自己写脚本(Task)生成模板工程,也可以使用Plugin。

按照以下步骤安装插件:

(1)从https://launchpad.net/gradle-templates 下载templates-1.2.jar 后放入一个新建的文件夹$GRADLE_HOME\customPlugins中。

(2)编写一个build.gradle
[quote]buildscript{
repositories {
flatDir dirs: "${gradle.gradleHomeDir}/customPlugins"
}
dependencies {
classpath ':templates:1.2'
}
}

apply plugin: 'templates'[/quote]

(3)执行脚本
[quote]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[/quote]

(4)查看Task

[quote]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[/quote]

(5)创建Java模板工程
[quote]D:\>gradle createJavaProject
> Building > :createJavaProject
??> Project Name: TestRen

??> Group: [testren] testgroup

??> Version: [1.0] 1.0
:createJavaProject

BUILD SUCCESSFUL

Total time: 30.578 secs[/quote]

(6)生成的工程
[quote]TestRen
│ build.gradle
│ gradle.properties
│ LICENSE.txt

└─src
├─main
│ ├─java
│ └─resources
└─test
├─java
└─resources[/quote]

(7)使用STS(SpringSource Tool Suite)导入工程,以后的构建就很方便了。
[img]http://dl.iteye.com/upload/attachment/0071/3318/77e685b0-ced8-329f-af6a-8fa0d8399d34.gif[/img]
[img]http://dl.iteye.com/upload/attachment/0071/3320/2caaecc4-8484-3183-b371-36feec7689f5.gif[/img]
[img]http://dl.iteye.com/upload/attachment/0071/3322/489969bb-491b-36ca-8173-d19066e1f81b.gif[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值