如何使用Gradle創建如maven那樣的項目結構呢 ?
gradle不像maven那樣有固定的項目結構,gradle原聲API是不支持的,要想做到這一點,我們可以自定義一個task。
1. 創建一個createJavaProject文件夾
2. 在此目錄下創建gradle的構建腳本,build.gradle,內容如下:
apply plugin: 'java'
task "create-dirs" << {
sourceSets*.java.srcDirs*.each { it.mkdirs() }
sourceSets*.resources.srcDirs*.each { it.mkdirs() }
}
3. 命令行運行 gradle create-dirs
另外一種方法,我們可以使用三方插件templates。
1. gradle templates 插件可以輕松的創建項目結構,http://tellurianring.com/wiki/gradle/templates,但是最新版本gradle1.0好像安裝不上,不知道什麼問題,我們也可以使用其他方式用上這個插件。
2. 下載最新的templates-xx.jar,https://launchpad.net/gradle-templates/+download 放在gradle 的lib/plugins目錄下面。
3. 建個項目文件夾createJavaProject, 新建gradle構建腳本build.gradle,內容如下:
buildscript{
repositories {
flatDir dirs: "${gradle.gradleHomeDir}/lib/plugins"
}
dependencies {
classpath ':templates:1.2'
}
}
apply plugin: 'templates'
4. 執行gradle tasks, 就會看到如下tasks:
Template tasks
--------------
createGradlePlugin - Creates a new Gradle Plugin project in a new directory named after your project.
createGroovyClass - Creates a new Groovy class in the current project.
createGroovyProject - Creates a new Gradle Groovy project in a new directory named after your project.
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 project.
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.
5. gradle createJavaProject創建java項目的項目目錄結構。