使用Gradle创建Web Project

日积月累,滴水穿石。

学学怎么用gradle创建web项目。

原来的做法

我原来是这样做的:
新建gradle项目,然后修改Project Facets中的Dynamic web Module,但是实际情况是,只要一刷新gradle项目就会导致Dynamic web Module丢失,后面如果再修改Project Facets还是不行,就算是卸载Dynamic web Module,也会报NullPointerException。

现在的做法

1.新建Dynamic Web Project,指定Web module中的Content directory为src/main/webapp。
2.新建src/main/java、src/main/resources、src/test/java、src/test/resources
3.在项目根目录下创建build.gradle文件,这个文件可以创建一个gradle项目生成后,再复制到这里,注意应该打包为war包。

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'eclipse-wtp' // 这个非常重要!
apply plugin: 'war'

sourceCompatibility = 1.7
version = '1.0'
jar {
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart',
                   'Implementation-Version': version
    }
}

repositories {
    mavenCentral()
}

dependencies {
    // https://mvnrepository.com/artifact/org.springframework/spring-core
    compile group: 'org.springframework', name: 'spring-core', version: '4.3.7.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework/spring-context
    compile group: 'org.springframework', name: 'spring-context', version: '4.3.7.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework/spring-web
    compile group: 'org.springframework', name: 'spring-web', version: '4.3.7.RELEASE'
    // https://mvnrepository.com/artifact/org.springframework/spring-webmvc
    compile group: 'org.springframework', name: 'spring-webmvc', version: '4.3.7.RELEASE'
    // https://mvnrepository.com/artifact/javax.validation/validation-api
    compile group: 'javax.validation', name: 'validation-api', version: '2.0.0.Alpha1'
    // https://mvnrepository.com/artifact/org.hibernate/hibernate-validator
    compile group: 'org.hibernate', name: 'hibernate-validator', version: '5.4.1.Final'
    // https://mvnrepository.com/artifact/org.json/json
    compile group: 'org.json', name: 'json', version: '20160810'
    // https://mvnrepository.com/artifact/jstl/jstl
    compile group: 'jstl', name: 'jstl', version: '1.2'
    // https://mvnrepository.com/artifact/org.aspectj/aspectjrt
    compile group: 'org.aspectj', name: 'aspectjrt', version: '1.8.10'
    // https://mvnrepository.com/artifact/aopalliance/aopalliance
    compile group: 'aopalliance', name: 'aopalliance', version: '1.0'
    // https://mvnrepository.com/artifact/org.aspectj/aspectjweaver
    compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.8.10'

    testCompile group: 'junit', name: 'junit', version: '4.+'
}

test {
    systemProperties 'property': 'value'
}

uploadArchives {
    repositories {
       flatDir {
           dirs 'repos'
       }
    }
}

apply plugin: ‘eclipse-wtp’ 是Web Tools Platform,用于构建Web项目的Eclipse插件。必须使用此插件,要不然刷新gradle后,该项目就不是web项目了。

4.右键项目,选择Configure,Convert to Gradle Project。
5.刷新gradle,完成。

参考

https://my.oschina.net/u/1177710/blog/270406

猜你喜欢

转载自blog.csdn.net/u012383839/article/details/72875132