GRADLE下运行main函数/执行测试用例

 1 group 'gongsibao.ged'
 2 version '1.0-SNAPSHOT'
 3 
 4 apply plugin: 'java'
 5 apply plugin: 'idea'
 6 
 7 sourceCompatibility = 1.8
 8 
 9 repositories {
10     mavenLocal()
11 
12     maven {
13         credentials {
14             username 'xxxx'
15             password 'xxxx'
16         }
17         url "xxxxx"
18     }
19 
20     //maven { url 'http://maven.oschina.net/content/groups/public/' }
21     mavenCentral()
22 }
23 
24 dependencies {
25     compile group: 'commons-io', name: 'commons-io', version: '2.0.1'
26     compile group: 'commons-logging', name: 'commons-logging', version: '1.1.1'
27     compile group: 'commons-lang', name: 'commons-lang', version: '2.6'
28     compile group: 'log4j', name: 'log4j', version: '1.2.17'
29     compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.18'
30 
31     testCompile group: 'junit', name: 'junit', version: '4.12'
32 }
33 
34 //---------------------------------
35 // gradle命令生成jar包
36 //---------------------------------
37 def libPath = "lib"
38 task release(type: Copy,dependsOn: [build,copyJar]) {
39 }
40 
41 jar {
42     manifest {
43         attributes(
44             "Main-Class": "org.netsharp.pigeon.startup.Startup",
45             "Implementation-Title": "Gradle",
46             "Class-Path": configurations.compile.collect { "$libPath/"+it.getName() }.join(' ')
47         )
48     }
49 }
50 
51 task copyJar(type:Copy){
52     from configurations.runtime
53     into ('build/libs/lib')
54 }
55 
56 //---------------------------------
57 // 在gradle运行时环境下执行main函数
58 //---------------------------------
59 task startserver(type: JavaExec, dependsOn: 'jar') {
60     description '启动服务器监听'
61     classpath = sourceSets.main.runtimeClasspath
62     main = 'org.netsharp.pigeon.startup.Startup'
63 }
64 
65 sourceSets {
66     main {
67         java {
68             srcDirs = ['src/main/java','src/test/java']
69         }
70         resources {
71             srcDirs = ['src/main/resources']
72         }
73     }
74     test {
75         java {
76             srcDirs = ['src/test/java']
77         }
78         resources {
79             srcDirs = ['src/test/resources']
80         }
81     }
82 }

 二,执行测试用例

打开文件/netsharp-test/build.gradle,添加如下任务

//同步数据库元数据
task syncMetaData(type: Test) {

    include '**/AllTests.class'
    reports.junitXml.destination = "$buildDir/test-results/SuiteTwo"
    reports.html.destination = test_reports_base_location
    //reports.html.destination = "$buildDir/test-results/SuiteTwo"
}

执行方法:gradle syncMetaData

添加一个其他的备注,gradle也支持把jar包放到代码本地路径,然后添加引用,格式如下:

compile files('src/lib/xxxxx.jar')

作者    :秋时

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

原文地址:https://www.cnblogs.com/Netsharp/p/8157566.html