---
title: "Gradle入门(2) -- 创建模板工程"
author: "rensanning"
date: "2012-07-26T10:25:02Z"
tags: ["gradle"]
categories: ["企业架构"]
slug: "gradle-2"
draft: false
lastmod: "2012-07-26T10:38:45Z"
id: 1607617
url: "https://rensanning.iteye.com/blog/1607617"
---

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:\\&gt;gradle --stacktrace :help Welcome to Gradle 1.0. To run a build, run gradle ... 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:\\&gt;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:\\&gt;gradle createJavaProject &gt; Building &gt; :createJavaProject ??&gt; Project Name: TestRen ??&gt; Group: \[testren] testgroup ??&gt; 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]