zoukankan      html  css  js  c++  java
  • gradle获取shell输出并使用

    前言

    当我们的电脑在不同的局域网下会有不同的ip,而在gradle中又要配置ip的时候
    (说的就是你nexus配置),每次手动改总觉得麻烦,因此有了本篇文章介绍如何偷懒!

    1.shell获取本地ip

    #!/usr/bin/env bash
    ifconfig |grep 192.168|cut -d ' ' -f2
    

    "ifconfig |grep 192.168"会获得
    "inet 192.168.0.103 netmask 0xffffff00 broadcast 192.168.0.255"
    "cut -d ' ' -f2"表示以空格分开,然后获取第二个
    也就是我们的目标192.168.0.103
    ps:记得给shell 文件赋可执行权限:chmod +x getIp.sh

    2.gradle 获取到shell的输出

    gradle中定义一个方法来获取输出
    可以参考官网文档

    def getIp() {
        def out = new ByteArrayOutputStream()
        exec {
            workingDir 'xxx'//getIp.sh所在目录,可以是绝对路径或相对路径
            commandLine './getIp.sh'
            standardOutput = out
        }
        return out.toString().trim()//trim()是必要的
    }
    

    到这里我们就可以通过调用getIp()来获取本地ip了,如:

    ext {
        curIp = getIp()
    }
    //或者
    def curIp = getIp()
    

    更多文章,尽在个人博客
    fork me on github

  • 相关阅读:
    The Chinese Postman Problem HIT
    Chinese Postman Problem Aizu
    矩阵游戏 HYSBZ
    最大获利 HYSBZ
    asp.net+MVC--1
    -----IT男生涯————初始篇
    Permutation
    RMQ with Shifts
    Fast Matrix Operations
    "Ray, Pass me the dishes!"
  • 原文地址:https://www.cnblogs.com/bosh123/p/12172159.html
Copyright © 2011-2022 走看看