Post

[Maven] Maven에서 ANT 실행하여 EXEC로 배포하기

[Maven] Maven에서 ANT 실행하여 EXEC로 배포하기

[MAVEN] Maven에서 ANT를 활용하여 EXEC로 배포하기

이전에 MAVEN에서 maven-antrun-plugin을 사용하여 scp로 파일을 배포하고, sshexec를 통해서 서비스를 재시작하는 빌드 스크립트를 소개했다.

하지만 서버 버젼이 변경되면서 암호화 지원 문제로 작동이 되지 않는 상황이 발생했다. 그래서 찾아보다가 원초적으로 exec 를 통해서 시스템 명령어를 통한 배포 방법을 찾아서 소개한다.

메이븐 빌드 스크립트(Ant의 EXEC를 통한 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
<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>
              <!-- 파일 전송 -->
              <exec executalbe="scp" failonerror="true">
                <arg value="-i"/>
                <arg value="${remote.keyfile}"/>
                <arg value="-P"/>
                <arg value="${remote.port}"/>
                <arg value="-o"/>
                <arg value="StrictHostKeyChecking=no"/>
                <arg value="${project.build.directory}/${project.build.finalName}.jar"/>
                <arg value="${remote.user}@${remote.host}:${remote.path}"/>
              </exec>
              
              <!-- 원격 명령 실행: 서비스 재시작 -->
              <exec executable="ssh">
                <arg value="-i"/>
                <arg value="${remote.keyfile}"/>
                <arg value="-P"/>
                <arg value="${remote.port}"/>
                <arg value="${remote.username}@${remote.host}"/>
                <arg value="sudo systemctl restart service_name"/>
              </exec>
            </target>
          </configuration>
          <goals>
            <goal>run</goal>
          </goals>
        </execution>
      </executions>
    </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>
  • 해당 방법은 시스템의 명령어를 ANT의 실행 명령을 통해서 직접 사용하는 방법으로 기존에 사용되던 의존성 라이브러리가 필요가 없다.
  • 대신에 시스템에서 sshscp에 대한 사용이 가능하도록 애플리케이션이 설치되어 있어야 한다.
This post is licensed under CC BY 4.0 by the author.