[Maven] package org.junit does not exists 에러 발생시

Published on: 2013. 11. 14. 14:09 by louis.dev
오늘 우연히 Maven을 통해 packaging을 진행을 하다가 이런 에러를 발생시키면서 packaging이 진행되지 않았다.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.1:compile (default-compile) on project ${프로젝트명}: Compilation failure: Compilation failure:
[ERROR] ${에러 소스파일 경로}\${파일명}.java:[10,38] package org.junit.runners.Parameterized does not exist
[ERROR] ${에러 소스파일 경로}\${파일명}.java:[10,38] package org.junit.runners.Parameterized does not exist
[ERROR]
[ERROR] ${에러 소스파일 경로}\${파일명}.java:[10,38] package org.junit.runners.Parameterized does not exist
[ERROR]
[ERROR] ${에러 소스파일 경로}\${파일명}.java:[10,38] package org.junit.runners.Parameterized does not exist
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :Infinite
에러내용을 읽어보니 org.junit.runers.Parameterized라는 패키지가 존재하지 않는다는 문제였다. 아마  검색을 통하여 이 포스트를 찾았다면 에러가 발생된 패키지 명은 달라도 모두 junit에서 발생한 문제일 것이다. 이문제는 컴파일 단계에서 junit 라이브러리가 포함이 되지 않아 발생하는 문제로 Maven의 버그가 아니라 pom파일의 dependency 설정때문에 발생한다.
<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.8.1</version>
	<scope>test</scope>
</dependency>
위와같이 pom파일의 dependency 설정을통해 junit 라이브러리를 추가하게 되는데 이때 해당라이브러리의 사용범위를 지정할수있도록 Maven은 scope라는 엘리먼트를 지원해 주고 있다. 이때 scope를 test로 선언하게 되면 해당라이브러리는 테스트 중에만 사용을 하고 실제 packaging 중에는 포함이 되지 않는다. 이렇게 사용범위를 지정함으로서 불필요한 라이브러리 포함을 줄여 어플리케이션의 사이즈를 줄일수 있도록 만들게 되었다. 따라서 위와 같은 빌드 실패는 어플리케이션이 빌드되고 배포 되어질때 junit 라이브러리는 scope 설정때문에 패키징에서 제외되었음으로 org.junit.runers.Parameterized 패키지가 존재하지 않아 발생되는 문제이다. 필자의 경우에는 실제 org.junit.runers.Parameterized 패키지를 사용한 것이 아니라 mybatis의 @Param 어노테이션을 사용하려고 이클립스에서 지원하는 code assist 기능을 사용하여 입력하였고 그때 잘못 입력하여 java파일 상단에 import org.junit.runners.Parameterized.Parameters; 가 포함되었기 때문에 위와 같은 문제가 발생했다. 만약 각자의 코트에서 junit class를 런타임시에도 필요하다면(그럴일은 거의 없겠지만) scope 엘리먼트를 제거하면 되고, 그렇지 않으면 불필요한 import 문을 제거해 주면 깔끔하게 해결된다. 다른 dependency scope를 확인하고 싶다면 여기를 통해 확인하자.