您的位置:首页 > 其它

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

2015-02-19 00:00 531 查看
摘要: 今天体验了下Gradle,总体感觉还不错,就是新建项目比较麻烦(可能是我不会用这个插件),随想通过Task实现项目结构的创建,果断网站一搜,还真有现成的,开心,收藏。

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

<pre class="brush:groovy">
apply plugin: 'java'

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

用gradle运行此任务:

<pre class="brush:shell">
$ gradle create-dirs
</pre>

生成的目录结构:

<pre>
.
├── build.gradle
└── src
├── main
│ ├── java
│ └── resources
└── test
├── java
└── resources
</pre>

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

<pre class="brush:groovy">
apply plugin: 'java'
apply plugin: 'idea'
</pre>

然后执行如下命令:

<pre class="brush:shell">
$ gradle idea
</pre>

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

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

Mark:我的构建脚本

<pre class="brush:groovy">
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()}
}
</pre>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Gradle