Gradle执行cmd命令 Jacoco生成报告 FTP上传文件

让gradle执行cmd 或者其他exe命令的方法

  • 执行cmd命令的办法
    如下是执行cmd.exe下的某个命令。
task  testBatTask(type:Exec,description:'just for test bat  task'){
    workingDir 'nboxSoFile'
    commandLine 'cmd',"/c","executeEnhanceSoFileTask"
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 执行其他Exe命令的办法
    如下是执行一个 exe带参数的命令。
task  justEnhanceSoFileTask(type: Exec, description: '执行加固so File 操作'){
    workingDir  'nboxSoFile'
    def myCommond = [
                      'nagaptbox_client.exe', 'so','tdog.out',
                      '-P',  '--fake-pt-dynamic-offset', '-E', '11FFCBAA',
                      '-l1./tdog_loader_dis_v2_arm.so', '-lx./tdog_loader_dis_v2_arm.xml',
                      '-lc./tdog_cipher_v1.so', 'libLuluyouSecurity.so'
                    ]
    commandLine  myCommond
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
task stopTomcat(type:Exec) {
   workingDir '../tomcat/bin'

   //on windows:
   commandLine 'cmd', '/c', 'stop.bat'

   //on linux
   commandLine './stop.sh'

   //store the output instead of printing to the console:
   standardOutput = new ByteArrayOutputStream()

   //extension method stopTomcat.output() can be used to obtain the output:
   ext.output = {
     return standardOutput.toString()
   }
 }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

Gradle中调用cmd

需要在Gradle编译时,调用某些脚本进行文件操作,比如:头文件更新,或者动态链接库文件的更新等,需要借助脚本文件,并且不需要手动运行,那么如何使用Gradle呢?

如下代码可以实现在执行Gradle编译时连续运行两条命令:

 exec {
            workingDir '../'
            commandLine 'cmd', '/c', 'cmd_01.cmd && ' +  'cmd_02.cmd '+ Parameter
        }

 
 
  • 1
  • 2
  • 3
  • 4
  • 5

其中:

Parameter为cmd_02.cmd的参数,连续多条命令使用 && 连接;

workingDir为基于当前脚本的相对运行路径;

AS中编写CMD

在实际开发中,时常需要编写脚本进行文件操作,现在以一个文件拷贝替换的例cmd_02.cmd子进行说明:

@echo off
rem SET YOUR LOCAL VAR:
rem  0 : debug_ver_1 \ 1 : debug_ver_2 \ 2 : release_ver_1 \ 3 : release_ver_2
set VERSION_TYPE=%1%

if %VERSION_TYPE% equ 0 (
echo 835_debug version!
goto debug_ver_1
)

if %VERSION_TYPE% equ 1 (
echo 835_release version!
goto debug_ver_2
)

if %VERSION_TYPE% equ 2 (
echo 845_debug version!
goto release_ver_1
)

if %VERSION_TYPE% equ 3 (
echo 845_release version!
goto release_ver_2
)

:debug_ver_1
xcopy %CD%\app\src\main\libs_debug\libtest_01.so %CD%\app\src\main\cpp\testJNI\armeabi-v7a /y || goto ERROR_COPY
xcopy %CD%\app\src\main\libs_debug\libtest_02.so %CD%\app\src\main\cpp\testJNI\armeabi-v7a /y || goto ERROR_COPY
echo all libs updated
exit /b 0

:debug_ver_2
xcopy %CD%\app\src\main\libs_debug\libtest_03.so %CD%\app\src\main\cpp\testJNI\armeabi-v7a /y || goto ERROR_COPY
xcopy %CD%\app\src\main\libs_debug\libtest_04.so %CD%\app\src\main\cpp\testJNI\armeabi-v7a /y || goto ERROR_COPY
echo all libs updated
exit /b 0

:release_ver_1
xcopy %CD%\app\src\main\libs_release\libtest_01.so %CD%\app\src\main\cpp\testJNI\armeabi-v7a /y || goto ERROR_COPY
xcopy %CD%\app\src\main\libs_release\libtest_02.so %CD%\app\src\main\cpp\testJNI\armeabi-v7a /y || goto ERROR_COPY
echo all libs updated
exit /b 0

:release_ver_2
xcopy %CD%\app\src\main\libs_release\libtest_01.so %CD%\app\src\main\cpp\testJNI\armeabi-v7a /y || goto ERROR_COPY
xcopy %CD%\app\src\main\libs_release\libtest_02.so %CD%\app\src\main\cpp\testJNI\armeabi-v7a /y || goto ERROR_COPY
echo all libs updated
exit /b 0

:ERROR_COPY
echo failed to update libs
exit /b 0
 
 
  • 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
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

其中:

set VERSION_TYPE=%1%
 
 
  • 1

即表示在运行时会接受一个外部参数作为命令内的条件分支。

Jacoco配置

apply plugin: 'jacoco'
jacoco {
    toolVersion = "0.7.9"
}

//拉取文件
task pullReport() {
    exec {
        workingDir('C:\\')//执行目录
        commandLine 'cmd', '/c', 'md pregnancy && adb pull /mnt/sdcard/coverage C:\\pregnancy'//执行指令,注意需打开adb.exe
    }
}

//首先先删除旧的merge结果文件
task removeOldMergeEc(type: Delete, dependsOn: pullReport) {
    delete "$buildDir/mergedcoverage.ec"
}

//.ec存放目录
def ec_dir = ['/mnt/sdcard/coverage/']

task mergeReport(type: JacocoMerge, dependsOn: removeOldMergeEc) {
    group = "Reporting"
    description = "merge jacoco report."
    destinationFile = file("$buildDir/mergedcoverage.ec")//目标文件
    //这里的ec_dir是存储ec文件的文件夹
    FileTree tree = fileTree("$ec_dir") {
//        include '**/*.ec'
        include "*.ec"
    }
    executionData = tree
}

//合并后分析
task jacocoTestReport(type: JacocoReport, dependsOn: mergeReport) {
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
    reports {
        xml.enabled = false
        html.enabled = true
        csv.enabled = false
        //        html.destination file("${buildDir}/jacocoHtml")//报告生成路径
    }
    classDirectories = fileTree(
            dir: './build/intermediates/classes/debug',
            excludes: ['**/R*.class',
                       '**/*$InjectAdapter.class',
                       '**/*$ModuleAdapter.class',
                       '**/*$ViewInjector*.class'
            ])
    def coverageSourceDirs = ['../app/src/main/java']//针对此文件夹进行分析
    sourceDirectories = files(coverageSourceDirs)
    executionData = files("$buildDir/mergedcoverage.ec")

    doFirst {
        new File("$buildDir/intermediates/classes/").eachFileRecurse { file ->
            if (file.name.contains('$$')) {
                file.renameTo(file.path.replace('$$', '$'))
            }
        }
    }
}


FTP上传文件

configurations {
    ftpAntTask
}

def remotedir = "/usr/xj"  //ftp目录
def hostname = "192.168.0.1"
def username = "root"
def password = "root"

dependencies {
    ftpAntTask("org.apache.ant:ant-commons-net:1.9.2") {
        module("commons-net:commons-net:3.3")
    }
}

task uploadFTP {
    //上传至FTP
    ant {
        taskdef(name: 'ftp',
                classname: 'org.apache.tools.ant.taskdefs.optional.net.FTP',
                classpath: configurations.ftpAntTask.asPath)
                //创建文件目录
        ftp(server: hostname,
                userid: username,
                password: password,
                remotedir: remotedir,
                action: 'mkdir')
                //上传
        ftp(server: hostname,
                userid: username,
                password: password,
                passive: true,//被动模式上传,排除客户端防火墙高位端口干扰
//                action: 'put',
                remotedir: remotedir) {
            fileset(dir: "$buildDir/reports/jacoco/jacocoTestReport/html") {
            }
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值