Gradle / groovy设置环境变量

时间:2014-10-29 00:29:07

标签: groovy gradle

我在Windows命令提示符下设置环境时遇到一些问题:

我在做什么:

  1. 设置环境(创建环境变量)
  2. 配置环境后运行bat
  3. 我尝试了两种不同的方法:

    1. 在build.gradle上创建两个不同的任务:
    2. task setupenvironment(type:Exec) 
      {
           def DirLocation = 'C:/../path/'
           workingDir = new File(DirLocation)
           commandLine DirLocation + 'environment.cmd'
              args =  [ '-x',       location,
                        '-y',           WorkspaceLocation,
                        '-z',           'generated'
                ]
      }
      task createwithpackage (type:Exec, dependsOn: setupenvironment) 
      {
           def DirLocation = 'C:/../path/'
           def WorkspaceLocation = 'C:/../Workspace'
           def location = 'C:/../location'
           workingDir = new File(DirLocation)
           commandLine DirLocation + 'package.bat'
              args =  [ '-x',       location,
                        '-y',           WorkspaceLocation,
                        '-z',           'generated'
                ]
      }
      
      //console: gradle createwithpackage --stacktrace
      ERROR -> Process 'command 'C:/../package.bat'' finished with non-zero exit value 255
      
      1. 在build.gradle上创建任务以调用具有两个进程的.bat
      2. task createall(type:Exec) { 
        {
             def DirLocation = 'C:/../'
             workingDir = new File(DirLocation)
             commandLine DirLocation + 'deploy.bat'
        }
        task createwithpackage(type:Exec) { 
        {
             def DirLocation = 'C:/../path/'
             def WorkspaceLocation = 'C:/../Workspace'
             workingDir = new File(DirLocation)
             commandLine DirLocation + 'package.bat'
                args =  [ '-x',       location,
                          '-y',           WorkspaceLocation,
                          '-z',           'generated'
                  ]
        }
        

        DEPLOY.BAT

        CALL "C:\..\ environment.cmd" 
        CALL gradle createwithpackage
        
        //console: gradle createall –stacktrace
        Exception throwed: createwithpackage: Could not find or load main class com.package.class
        

        但是,如果我在控制台中手动执行上述命令,一切正常。

        我做错了吗?

        - 编辑

        很抱歉这么晚回复,我按照你的链接上的建议尝试过。但是我一直都有这个错误,我调试并使用stacktrace来查找问题,最后我得到了它的工作,但仅用于一个任务

        这项工作仅适用于直接任务(在任务之前没有设置环境变量):

        task createwithpackage (type:Exec) 
        {
             def DirLocation = 'C:/../path/'
             def WorkspaceLocation = 'C:/../Workspace'
             def location = 'C:/../location'
             workingDir = new File(DirLocation)
             commandLine DirLocation + 'package'
                args =  [ '-x',       location,
                          '-y',           WorkspaceLocation,
                          '-z',           'generated'
                  ]
        }
        

        然而接下来的方法,开始很好,但最后当它试图保存在磁盘上时,抛出一个“拒绝访问”,所以我猜它必须是gradle权限,我不知道如何管理(我我以管理员身份运行cmd并具有所有权限):

        task createwithpackage (type:Exec) 
        {
             def DirLocation = 'C:/../path/'
             def WorkspaceLocation = 'C:/../Workspace'
             def location = 'C:/../location'
             workingDir = new File(DirLocation)
             commandLine 'cmd', '/c', 'package', '-x', location '-y', WorkspaceLocation, '-z', 'generated'
        }
        

        // gradle createwithpackage --debug

        09:27:57.640 [INFO] [org.gradle.process.internal.DefaultExecHandle] Successfully
         started process 'command 'cmd''
        09:29:06.193 [QUIET] [system.out] BIP0963E Error saving file in createwithpackage.
        09:29:06.203 [QUIET] [system.out]
        09:29:06.213 [QUIET] [system.out] The file "C:\path..\main
        /" could not be saved to disk.
        09:29:06.223 [QUIET] [system.out] The reason given was:
        09:29:06.223 [QUIET] [system.out] C:\path..\main (Access i
        s denied.)
        09:29:06.233 [QUIET] [system.out]
        09:29:06.233 [QUIET] [system.out]
        09:29:09.483 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Changing st
        ate to: FAILED
        09:29:09.503 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Process 'co
        mmand 'cmd'' finished with exit value 1 (state: FAILED)
        09:29:09.503 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceT
        askExecuter     
        

        有没有让gradle享有完全特权?任何进一步的帮助将不胜感激

        谢谢

1 个答案:

答案 0 :(得分:0)

您必须将其作为cmd in the Exec task执行:

commandLine 'cmd', '/c', DirLocation + 'deploy.bat'