创建基于Gradle的Web项目

来源:互联网 发布:js新窗口打开url 编辑:程序博客网 时间:2024/06/05 10:41

使用的环境是: Eclipse IDE for Java EE Developers Kepler + JDK 1.7.

1.安装 Gradle 插件. 在 Eclipse Marketplace 中搜索 Gradle, 选择安装 Gradle Integration for Eclipse

2.新建一个 Dynamic Web Project, 在项目根目录下新建 build.gradle 文件, 并向其中写入如下内容:   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import org.gradle.plugins.ide.eclipse.model.Facet
  
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
  
sourceCompatibility = 1.7   // 设置 JDK 版本
webAppDirName = 'WebContent'    // 设置 WebApp 根目录
sourceSets.main.java.srcDir 'src'   // 设置 Java 源码所在目录
  
// 设置 maven 库地址
repositories { 
    mavenCentral() // 中央库
    // maven { url '<a href="http://maven.oschina.net/content/groups/public/' " target="_blank">http://maven.oschina.net/content/groups/public/' </a>} // 自定义库地址
}
  
// 设置依赖
dependencies {
    providedCompile 'javax.servlet:servlet-api:2.5' // 编译期
    providedRuntime 'javax.servlet:jstl:1.2'    // 运行时
}
  
// 设置 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.7'
            facet name: 'wst.jsdt.web', version: '1.0'
        }
    }
}

   

3.接下来在项目上右击 -> configure -> convert to Gradle project

4.再次在项目上右击 -> Gradle -> Refresh All

5.如果没有 web.xml 文件的话, 再右击项目 -> Java EE Tools -> Generate Deployment Descriptor Stub

6.最后看下效果如何, 在 WebContent 下新建 index.jsp 里面随便写点什么, 然后项目上右击 -> Run As -> Run on Server

0 0
原创粉丝点击