利用Gradle Task自动创建项目结构

将下面代码加入到build.gradle中:

apply plugin: 'java'

task "create-dirs" << {
    sourceSets*.java.srcDirs*.each {it.mkdirs()}
    sourceSets*.resources.srcDirs*.each {it.mkdirs()}
}

用gradle运行此任务:

$ gradle create-dirs

生成的目录结构:

.
├── build.gradle
└── src
    ├── main
    │   ├── java
    │   └── resources
    └── test
        ├── java
        └── resources

如果是使用idea的话,只要在build.gradle中添加idea插件就行了,如下:

apply plugin: 'java'
apply plugin: 'idea'

然后执行如下命令:

$ gradle idea

以上命令会生成out: file.imlfile.iprfile.iws这三个文件,最后就可以打开intellij idea愉快的玩耍了。

原文地址:http://www.tuicool.com/articles/eyeeEf

Mark:我的构建脚本

import org.gradle.plugins.ide.eclipse.model.Facet

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'maven'
apply plugin: 'eclipse'
apply plugin: 'eclipse-wtp'

apply plugin: 'jetty'

//设置 JDK 版本
sourceCompatibility = 1.8
targetCompatibility = 1.8
//设置 WebApp 根目录
webAppDirName = 'webapp'
//设置 Java 源码所在目录
//sourceSets.main.java.srcDir 'src/main/java'
sourceSets {
    main {
        java {
            srcDir 'src/main/java'
        }
        resources {
            srcDir 'src/main/resources'
        }
    }
}


//设置 maven 库地址
repositories {
    mavenCentral() // 中央库
    maven { url 'http://maven.oschina.net/content/groups/public/' } // 自定义库地址
}


// 设置依赖
dependencies {
    def SpringVersion = '4.1.4.RELEASE'

    runtime group:'org.springframework', name:'spring-context', version:'4.1.4.RELEASE'
    runtime group:'org.springframework', name:'spring-webmvc', version:'4.1.4.RELEASE'
    runtime group:'org.aspectj', name:'aspectjrt', version:'1.8.1'
    runtime group:'org.slf4j', name:'slf4j-api', version:'1.6.1'
    runtime group:'org.slf4j', name:'jcl-over-slf4j', version:'1.6.1'
    runtime group:'org.slf4j', name:'slf4j-log4j12', version:'1.6.1'
    runtime group:'log4j', name:'log4j', version:'1.2.6'
    runtime group:'javax.inject', name:'javax.inject', version:'1'
    runtime group:'com.fasterxml.jackson.core', name:'jackson-databind', version:'2.4.1'

    testRuntime group:'org.springframework', name:'spring-test', version:'4.1.4.RELEASE'
    testRuntime group:'junit', name:'junit', version:'4.11'
//  providedRuntime 'org.springframework:spring-context:4.1.4.RELEASE'
//  providedCompile 'javax.servlet:servlet-api:2.5'
}


// 设置 Project Facets
eclipse {
    wtp {
        facet {
            facet name: 'jst.web', type: Facet.FacetType.fixed
            facet name: 'wst.jsdt.web', type: Facet.FacetType.fixed
            facet name: 'jst.java', type: Facet.FacetType.fixed
            facet name: 'jst.web', version: '3.0'
            facet name: 'jst.java', version: '1.8'
            facet name: 'wst.jsdt.web', version: '1.0'
        }
    }
}

task "create-dirs" << {
    sourceSets*.java.srcDirs*.each {it.mkdirs()}
    sourceSets*.resources.srcDirs*.each {it.mkdirs()}
}

你可能感兴趣的:(gradle)