为什么';gradle exec是否使用脚本?

为什么';gradle exec是否使用脚本?,gradle,Gradle,有没有办法让gradle exec像shell exec一样工作?ie-了解路径中的可执行文件 我们有需要在windows和unix上运行的代码,还有许多在这两台机器上显然不同的脚本。虽然我可以做这样的黑客: exec { commandLine command("npm"), "install" } // // find a command with this name in the path // String command( String name ) { def o

有没有办法让gradle exec像shell exec一样工作?ie-了解路径中的可执行文件

我们有需要在windows和unix上运行的代码,还有许多在这两台机器上显然不同的脚本。虽然我可以做这样的黑客:

exec {
     commandLine command("npm"), "install"
}
//
// find a command with this name in the path
//
String command( String name )
{
    def onWindows = (System.env.PATH==null);
    def pathBits = onWindows ? System.env.Path.split(";") : System.env.PATH.split(":");
    def isMatch = onWindows ? {path -> 
                for (String extension : System.env.PATHEXT.split(";"))
                {
                    File theFile = new File( path, name + extension);
                    if (theFile.exists())
                        return theFile;
                }
                return null;
        } : {path -> def file = new File(path,name);if (file.exists() && file.canExecute()) return file;return null;}

    def foundLocal = isMatch(file("."));
    if (foundLocal)
        return foundLocal;
    for (String pathBit : pathBits)
    {
        def found = isMatch(pathBit);
        if (found)
            return found;
    }
    throw new RuntimeException("Failed to find " + name + " in the path")
}
npmCommand=Os.isFamily(Os.FAMILY\u WINDOWS)?'npm.cmd':'/usr/local/bin/npm'

然后运行这个命令-一些脚本的路径不一定是一成不变的-这只是可怕的代码


有没有办法解决这个问题-即扩展exec任务以在路径中查找可执行文件并运行它们?

我想要一个更好的方法-但是我做了一个简单的函数,我把它放在我们的公用程序中,您可以这样使用:

exec {
     commandLine command("npm"), "install"
}
//
// find a command with this name in the path
//
String command( String name )
{
    def onWindows = (System.env.PATH==null);
    def pathBits = onWindows ? System.env.Path.split(";") : System.env.PATH.split(":");
    def isMatch = onWindows ? {path -> 
                for (String extension : System.env.PATHEXT.split(";"))
                {
                    File theFile = new File( path, name + extension);
                    if (theFile.exists())
                        return theFile;
                }
                return null;
        } : {path -> def file = new File(path,name);if (file.exists() && file.canExecute()) return file;return null;}

    def foundLocal = isMatch(file("."));
    if (foundLocal)
        return foundLocal;
    for (String pathBit : pathBits)
    {
        def found = isMatch(pathBit);
        if (found)
            return found;
    }
    throw new RuntimeException("Failed to find " + name + " in the path")
}
函数如下所示:

exec {
     commandLine command("npm"), "install"
}
//
// find a command with this name in the path
//
String command( String name )
{
    def onWindows = (System.env.PATH==null);
    def pathBits = onWindows ? System.env.Path.split(";") : System.env.PATH.split(":");
    def isMatch = onWindows ? {path -> 
                for (String extension : System.env.PATHEXT.split(";"))
                {
                    File theFile = new File( path, name + extension);
                    if (theFile.exists())
                        return theFile;
                }
                return null;
        } : {path -> def file = new File(path,name);if (file.exists() && file.canExecute()) return file;return null;}

    def foundLocal = isMatch(file("."));
    if (foundLocal)
        return foundLocal;
    for (String pathBit : pathBits)
    {
        def found = isMatch(pathBit);
        if (found)
            return found;
    }
    throw new RuntimeException("Failed to find " + name + " in the path")
}

比如说:

if (System.getProperty('os.name').toLowerCase(Locale.ROOT).contains('windows'))
{
    commandLine 'cmd', '/c', 'commandGoesHere'
}
else
{
    commandLine 'sh', '-c', 'commandGoesHere'
}

当然,您可以获得标准的$PATH值并使用groovy进行处理。顺便说一句,有可能。