Maven 입문 | 자체 라이브러리 개발 및 이용 | maven-assembly-plugin 의존 라이브러리 포함

의존 라이브러리를 포함시키기

패키지를 할시에 의존 라이브러리로 Jar 파일에 포함하여 저장 시킬 수 있는데, 이는 maven-assembly-plugin이라는 플러그인을 설정하면 할 수 있다. 아래에 종속 라이브러리를 포함하여 Jar 파일에 저장하기 위한 설정을 올려 두었다.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<configuration> 태그에 대해

설정 정보를 기술하는 <configuration><descriptorRefs>라는 태그를 제공하고, 나아가 그 안에 <descriptorRef>라는 태그를 준비하고 있다. 여기에 jar-with-dependencies로 지정하여 Jar의 일체화를 위한 설정임을 지정할 수 있다.

그 후에 <executions>에 있는 <execution>에 필요한 설정을 한다. <phase>으로 package 를 <goals><goal>으로 single 을 각각 지정하고 있는데, 이는 패키징을 할때 1개의 Jar 파일로 정리하도록 하는 것이다. Jar 파일명은 SampleMavenApp-1.0-jar-with-dependencies.jar과 같이 -jar-with-dependencies가 붙여진다.

이 플러그인 설정을 pom.xml에 추가하여, 다시 mvn package하여 패키지를 해보자. 그리고 다시 java 커멘드로 Jar 파일을 실행해 본다.

$ java -classpath target\SampleMavenApp-1.0-jar-with-dependencies.ja com.devkuma.App

이번에는 제대로 프로그램이 실행될 것이다. Jar 파일에 MyLib이 포함되기 때문이다. 생성된 Jar 파일은 다른 컴퓨터에 복사하고 실행하여도 문제없이 동작한다.

SampleMavenApp의 완성된 pom.xml

SampleMavenApp의 pom.xml의 완성된 전체 빌드 스크립트를 올려 둔다.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.devkuma</groupId>
  <artifactId>SampleMavenApp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SampleMavenApp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
     <dependency>
       <groupId>com.devkuma.mylib</groupId>
       <artifactId>MyLib</artifactId>
       <version>1.0-SNAPSHOT</version>
     </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <configuration>
          <mainClass>com.devkuma.App</mainClass>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
        <configuration>
          <archive>
            <manifest>
                <mainClass>com.devkuma.App</mainClass>
                <addClasspath>true</addClasspath>
                <addExtensions>true</addExtensions>
                <packageName>com.devkuma</packageName>
            </manifest>
          </archive>
        </configuration>
      </plugin>
 
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
           <execution>
             <phase>package</phase>
             <goals>
               <goal>single</goal>
             </goals>
          </execution>
        </executions>
      </plugin>
     
    </plugins>
  </build>

</project>

이번 했던 작업에서 알 수 있듯이, 자체 라이브러리의 사용은 라이브러리 자체에 아무것도 특별한 작업은 필요없다. 유일하게 mvn install 명령으로 라이브러리를 로컬 저장소에 설치할 뿐이다.

최대의 포인트는 라이브러리를 이용하는 프로젝트에 있다. 먼저 <dependency> 태그 라이브러리를 추가하는 것이 필수 사항이다. 이것으로 exec:java라면 수행할 수 있다.

의존 라이브러리까지 모든 1개의 Jar에 정리하는 exec-assembly-plugin 플러그인 설정을 추가한다. 이는 프로젝트에 고유의 설정 등은 없기에, 여기에 게재한 태그를 그대로 복사해서 붙여 넣기하여 사용하면 될 것이다.




최종 수정 : 2021-08-27