기본 Ant 태스크에 공통 옵션을 적용해두고 호출할 수 있는 속임수 기법이 있다. Ant 태스크와 동일한 이름으로 함수를 만들고 거기서 ant.태스크이름으로 명시적으로 Ant 태스크를 호출하면서 기본값을 지정해주면 된다. exec 참조.
Ant 태스크 중에는 XML Attribute가 아닌 body에 값을 넣도록 처리하는 경우가 있다. 이 때 이것을 gant로 변경할 때는, 태스크 호출 맨 마지막에 바디의 내용을 문자열로 넣어주면 된다.
<echo level="error"> 이런 저런 메시지들을 출력해 봅시다! </echo>
변환하면
ant.echo(level: 'error', """이런 저런 메시지들을 출력해 봅시다!""")
Ant에서 표준출력으로 내보낼 때 [echo] blah.. blah.. 처럼 맨 앞에 태스크 이름이 나온다. taskname 속성을 통해 이를 조정할 수 있다.
ant.echo(message: 'Hello World', taskname: 'echo:hello')
결과는..
[echo:hello] Hello World
groovyc 태스크를 등록하는 것으로 알아본다. classpath 혹은 classpathref로 해당 태스크가 속한 라이브러리의 클래스패스를 지정할 수 있다.
taskdef(name: 'groovyc', classname: 'org.codehaus.groovy.ant.Groovyc') target(compile: 'Compile Groovy sources') { File dest = new File('classes') dest.mkdirs() File lib = new File('lib') lib.mkdirs() groovyc(srcdir: 'src', destdir: 'classes') { classpath { fileset(dir: 'lib') { include(name: '*.jar') } pathelement(path: 'classes') } } }
sshexec 등에서 사용)// 비밀번호를 입력 받아서 server.password Ant 프라퍼티로 저장한다. input(message: '비밀번호를 입력해주세요.', addproperty: 'server.password')
exec(executable: '명령어', failonerror: true) { arg value: '명령행 인자 1' arg value: '명령행 인자 2' arg line: '명령행인자를 한 줄로 쭉..' env key: '환경변수이름', value: '환경변수값' }
faileonerror : 오류 발생시 빌드 즉시 실패dir : 명령을 실행한 디렉토리 명시resultproperty : faileonerror=false일 때 리턴값을 저장할 프라퍼티 이름문자열 하나에 명령과 인자를 모두 넣은 상태에서 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') }
$GANT_HOME/lib에 라이브러리를 복사해 둔다.com.jcraft.jsch.JSchException: reject HostKey: 오류가 발생한다면 trust: true 옵션을 지정한다.ant.sshexec로 명시적으로 ant 를 지정해야만 작동한다.taskname 속성을 통해 접속중인 호스트명을 출력해주면 좋다.target(remotels: 'Remote list files') { ant.sshexec(host: '호스트네임/IP주소', username: '계정명', password: '비밀번호', failonerror: true, trust: true, command: 'ls /etc') }
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 -S는 stdin으로 부터 비밀번호를 입력받도록 해주는 옵션이다.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') }
echo를 이용하는 방법도 있다. usepty: true로 둔 상태에서 command: 'echo “비밀번호” | sudo -S 명령' 형태로 사용한다. input* 계열의 경우 가끔 입력 시점과 그걸 받아들이는 시점에 약간의 시간차가 발생하여 제대로 작동 안하는 경우가 있다. echo가 더 완벽하게 작동하는 듯./etc/sudoers 파일을 편집하여 아예 접속 계정에 비밀번호 없이 명령 실행 권한을 주는 방법도 있다. sudoers 참조.command: 'cmd1; cmd2; cmd3; …' : 여러 명령어를 실행한다.command: 'cmd1 & cmd2 & cmd3 & …' : 여러 명령어를 실행하되 앞의 명령이 성공해야만(and 조건) 다음 명령을 실행한다.commandResource: '명령이 줄 단위로 들어있는 파일명' : 파일에서 명령을 읽어서 실행한다.commandResource를 사용하면 inputstring등의 stdin으로 지정한 입력이 명령 실행시마다 반복 입력된다.sshexec와 동일하다. 단, 'username' 인자는 없다.사용자명[:비밀번호]@호스트명:/some/path 형태로 지정한다. 비밀번호는 keyfile 혹은 password로 대체 가능하다.target(remoteCopy: 'Remote Copy files') { ant.scp(host: '${host}', keyfile: '../id_rsa', failonerror: true, trust: true, toDir: 'username@${host}:/some/directory') { fileset(dir: '/local/directory') { include(name: '**/*.jar') } } }
file 단일 파일 지정. 로컬/원격 가능. @가 들어있으면 원격으로 간주.localFile : 단일 파일 지정. 무조건 로컬 파일.remoteFile : 단일 파일 지정. 무조건 리모트 파일. 사용자명@hostname:/some/path/.. 형태.fileset (dir: '로컬 경로') … : 로컬 파일셋 지정.toDir : 파일이 복사돼 들어갈 디렉토리. 로컬/원격 가능.localTodir : 파일이 복사돼 들어갈 디렉토리. 무조건 로컬 디렉토리.localToFile : 이하 생략…remoteToDirremoteToFile sshexec와 scp 사용시 빌드 파일에 사용자명과 비밀번호, passphrase 등을 직접 넣어 두는 것은 보안상 좋지 않다.input 태스크를 사용하여 프라퍼티로 만든 뒤에, 사용자명, 비밀번호 등은 실행시 입력 받아서 Ant 프라퍼티로 전달하는 것이 좋다. - 제일 좋다.gant -D username=사용자명 -D password=비밀번호 형태로 명령행에서 Ant 프라퍼티를 입력받고, 이 값을 사용한다. - 다른 사용자가 ps나 history 명령으로 명령행 옵션을 보는 것이 가능하다.target(inputSudoPassword: 'Input sudo password') { input(message: 'Input sudo password', addproperty: 'sudopassword') } target(remoteSudoLs: 'Remote sudo list files') { depends(inputSudoPassword) ant.sshexec(host: '${host}', username: '사용자명', keyfile: '/some/dir/id_rsa', failonerror: true, trust: true, usepty: true, command: 'echo "${sudopassword}" | sudo -S ls /var/log/httpd') }