사용자 도구

사이트 도구


gant:tasks

문서의 이전 판입니다!


Gant Tasks

Ant Task Override

기본 Ant 태스크에 공통 옵션을 적용해두고 호출할 수 있는 속임수 기법이 있다. Ant 태스크와 동일한 이름으로 함수를 만들고 거기서 ant.태스크이름으로 명시적으로 Ant 태스크를 호출하면서 기본값을 지정해주면 된다. exec 참조.

TaskDef

exec

  • 예시
    exec(executable: '명령어', failonerror: true) {
      arg value: '명령행 인자 1'
      arg value: '명령행 인자 2'
      arg line: '명령행인자를 한 줄로 쭉..'
      env key: '환경변수이름', value: '환경변수값'
    }
    • faileonerror : 오류 발생시 빌드 즉시 실패
    • dir : 명령을 실행한 디렉토리 명시
    • resultproperty : faileonerror=false일 때 리턴값을 저장할 프라퍼티 이름

명령을 받아서 파싱해서 ant.exec로 자동 넘기기

문자열 하나에 명령과 인자를 모두 넣은 상태에서 failonerror를 항상 true로 한 기본 exec를 만들어 두고 호출 할 수 있다.

def exec(command, dir = '.') {
    def splittedCommand = command.split(' ')
    def executable = splittedCommand[0]
 
    ant.exec(executable: executable, failonerror: true, dir: dir) {
        if (splittedCommand.size() > 1) {
            splittedCommand[1..-1].each {
                arg value: it
            }
        }
    }
}
 
target(hello: 'world') {
    // ant.exec보다 로컬에 선언한 함수 exec가 우선된다.
    exec('cmd /c dir')
}

sshexec

  • JSch 0.1.42 이상 버전 필요. Ant 1.8.3 이상 권장
  • Ant의 sshexec는 리눅스간 뿐만 아니라 Windows에서 리눅스로의 접속도 매우 잘 지원한다.
  • Key 생성은 Linux SSH 참조
  • 실행시 com.jcraft.jsch.JSchException: reject HostKey: 오류가 발생한다면 trust: true 옵션을 지정한다.
  • ant.sshexec로 명시적으로 ant 를 지정해야만 작동한다.

명령 사용자명/비밀번호 직접 지정방식

target(remotels: 'Remote list files') {
    ant.sshexec(host: '호스트네임/IP주소',
        username: '계정명',
        password: '비밀번호',
        failonerror: true,
        trust: true,
        command: 'ls /etc')
}

Key 기반 로그인

target(remotels: 'Remote list files') {
    ant.sshexec(host: '호스트네임/IP주소',
        username: '계정명',
        keyfile: '${user.home}/.ssh/id_dsa',
        passphrase: '패스프레이즈',
        failonerror: true,
        trust: true,
        command: 'ls /etc')
}

sudo

  • sudo -Sstdin으로 부터 비밀번호를 입력받도록 해주는 옵션이다.
  • inputstring (문자열), inputproperty (지정된 프라퍼티에 있는 값), input (파일의 내용) 등의 옵션은 stdin으로 보낼 내용을 적는 방법들이다.
  • usepty는 가상의 TTY를 생성해준다(ssh -t). Ant 1.8.3 이상 버전이 필요하다.
  • 위 옵션들을 적용하면 비밀번호 입력없이 sudo 사용이 가능해진다.
target(remotels: 'Remote list files') {
    ant.sshexec(host: '호스트네임/IP주소',
        username: '계정명',
        keyfile: '${user.home}/.ssh/id_dsa',
        passphrase: '패스프레이즈',
        failonerror: true,
        trust: true,
        inputstring: 'sudo비밀번호\n', // \n로 엔터치게 해준다.
        usepty: true,
        command: 'sudo -S ls /var/log/httpd')
}

여러개의 명령 실행

  • command: 'cmd1; cmd2; cmd3; …' : 여러 명령어를 실행한다.
  • command: 'cmd1 & cmd2 & cmd3 & …' : 여러 명령어를 실행하되 앞의 명령이 성공해야만(and 조건) 다음 명령을 실행한다.
  • commandResource: '명령이 줄 단위로 들어있는 파일명' : 파일에서 명령을 읽어서 실행한다.
    • commandResource를 사용하면 inputstring등의 stdin으로 지정한 입력이 명령 실행시마다 반복 입력된다.

scp

  • 대부분의 SSH 관련 설정은 sshexec와 동일하다.
gant/tasks.1350880181.txt.gz · 마지막으로 수정됨: 2012/10/22 13:29 저자 kwon37xi