[Maven] Maven에서 ANT 실행하여 SCP로 배포하기
[Maven] Maven에서 ANT 실행하여 SCP로 배포하기
[MAVEN] Maven에서 ANT를 활용하여 SCP로 배포하기
MAVEN에서 maven-antrun-plugin
을 사용하여 scp
로 파일을 배포하고, sshexec
를 통해서 서비스를 재시작하는 빌드 스크립트
메이븐 빌드 스크립트(Ant를 통한 SCP + SSH 실행)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>deploy-to-server</id>
<phase>package</phase> <!-- 또는 install, deploy 등 -->
<configuration>
<target>
<!-- 파일 전송 -->
<scp file="${project.build.directory}/${project.build.finalName}.jar"
todir="${remote.user}@${remote.host}:${remote.path}"
keyfile="${remote.keyfile}"
trust="true"
port="${remote.port}" />
<!-- 원격 명령 실행: 서비스 재시작 -->
<sshexec host="${remote.host}"
username="${remote.user}"
keyfile="${remote.keyfile}"
port="${remote.port}"
command="sudo systemctl restart service_name"
trust="true" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-jsch</artifactId>
<version>1.10.13</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<!-- 프로파일 예시 -->
<profiles>
<profile>
<id>deploy-target-server</id>
<properties>
<remote.user>coolioso</remote.user>
<remote.host>127.0.0.1</remote.host>
<remote.port>22</remote.port>
<remote.keyfile>/Users/coolioso/.ssh/id_rsa</remote.keyfile>
<remote.path>/home/centos/service</remote.path>
</properties>
</profile>
</profiles>
- 비밀번호 없이 사용하기 위해서 SSH 키 기반 인증으로 설정
- keyfile : PRIVATE SSH 키 파일의 경로
- 윈도우인 경우에는
\
대신에/
의 파일 구분자를 사용
- 비밀번호를 사용하기 위해서는
keyfile
대신에password
정보를 사용 - 업로드된 파일명을 변경
SSH 키를 사용시에 Invalid privatekey 라는 에러가 발생 해결법
개인키의 문제가 없지만 Java기반의 Ant 태스크의 scp
/ sshexec
에서 오류가 발생한다면 개인키 파일의 형식이 문제다.
명령행으로 ssh
는 OpenSSH 클라이언트를 쓰기 때문에 문제가 없지만, Maven + Ant + JSch
나 commons-net
같은 Java 기반 라이브러리에서는 OpenSSH PRIVATE KEY
형식을 인식하지 못한다.
OPENSSH PRIVATE KEY 형식
다음과 같은 형식으로 시작하면 OPENSSH PRIVATE KEY
형식이다.
1
-----BEGIN OPENSSH PRIVATE KEY-----
해결방법
OpenSSH로 생성한 키를 PEM 포맷으로 개인키 변환 하면 사용할 수 있다.
다음 명령어를 실행하면 기존 키파일을 PEM형식으로 바꿔 저장하고, publickey
파일을 다시 등록하지 않아도 그대로 사용할 수 있다.
1
ssh-keygen -p -m PEM -f ~/.ssh/id_rsa
-p
: 키 파일 비밀번호 재설정-m PEM
: PEM 포맷으로 강제 변환-f
: 기존 키파일 경로
This post is licensed under CC BY 4.0 by the author.