Gradle创建项目目录结构

如何使用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

Gradle创建项目目录结构

另外一种方法,我们可以使用三方插件templates。

1. 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项目的项目目录结构。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/psyxd.html