본문 바로가기

Maven

maven-assembly-plugin


mvn package를 하면, 해당 프로젝트의 의존성 라이브러리들은 그대로 둔채 패키징을 합니다. 그런데 저의 경우 프로젝트가 의존하고 있는 다른 라이브러리들도 같이 배포해줘야 했습니다.

"maven-assembly-plugin" 이 이런 일을 해주는게 아닌지 싶어서 이것 저것 해봤는데, 이 플러그인은 컴파일된 class 파일들을 만들어서 의존하고 있는 클래스들까지 통째로 가지고 있는 jar 파일을 만들 때 사용하는 거더군요.

그런데 저같은 경우 특정 dependency의 클래스들만 jar 파일에 함께 패키징 하고 싶습니다. 이유는 모든 dependency 의 클래스들을 jar 파일로 패키징하니 사이즈가 어마어마 해서요..;; ( 다른 dependency 들은 운영서버에서 글로벌하게 잘 가지고 있습니다. )


위 말을 쫌 더 자세히 써보도록 하겠다.


내가 만든 소스를 메이븐으로 패키징하고( mvn claen package ) 

회사의 클러스터링 환경에서 돌려보니


"NoClassFound" 에러가 뜬다. 뭔가하고 보니 참조했던 dependency 안에 있는 

Class 파일을 못찾는 다더라


<dependency>
<groupId>com.xxxx.xxxxxxxx</groupId>
<artifactId>xxxx</artifactId>
<version>x.x.x</version>
<classifier>jar-with-dependencies</classifier>
</dependency>




그래서 찾아본 방법이

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

</executions>
</plugin>


dependencies 로 참조한 모든 것을 포함해서 패키징하는 방법이 있네 +_+ 굳굳...!!

( descriptorRef 의 jar-with-dependencies 라는 명칭만 봐도 뭐하는지 알겠음 ㅋㅋ )

즐거운 마음에 빌드하고 실행!!


그리고 서버가 멈춤... :D 하하....

( 한 4시간 있으니까 됬던듯...  ㅋㅋ )



빌드한 결과물을 보니 


-rw-r--r--  1 myname  staff   117M  6 13 11:21 jarFileName-jar-with-dependencies.jar

-rw-r--r--  1 myname  staff    29K  6 13 11:21 jarFileName.jar



용량이... 후덜덜.... 이유는 dependencies 에서 참조하는 모든 class 파일을 물고 jar 파일로 패키징해버린것이다 ㅠㅠ



해결책은 내가 참조하고 싶지 않은 dependency 의 scope 을 아래와 같이 설정해 주면 된다.


<dependency>
<groupId>xxx.xxxxx. xxxxx </groupId>

<artifactId>xxxxx</artifactId>
<version>x.x.x</version>
<scope>provided</scope>
</dependency>


이라고 설정하고 물고 싶지 않은것에는 scope 를 provided 로 설정한 후에

mvn clean package !!


-rw-r--r--  1 username  staff    29K  6 13 11:32 original-jarFileName.jar

-rw-r--r--  1 username  staff   507K  6 13 11:32 jarFileName.jar


보는것처럼 내가 만든 jar 가 기존과는 다르게 깔끔하게 내가 scope compile 이라고 설정한 

dependency 만 물고 들어간것을 볼 수 있다! ( 원래 더 정확하게 보려면 해당 jar 파일의 압축을 풀어 그 안에 포함된  class 파일을 살펴보면 된다. ^^ )