Develop/Springboot

springboot 외부 라이브러리 jar 파일 추가 방법 | How to Add External Library JAR Files in Spring Boot

로컬 프로젝트 안에 외부에서 받아온 라이브러리인 jar파일을 추가하는 방법을 찾아보았다.gradle에 존재하지 않는 외부의 .jar 파일을 스프링 부트 안에 의존성을 추가하는 방법이다. /build.gradle 파일 안에 명시를 해주어야 한다. 1. 한 개의 파일을 넣는 방법dependencies { implementation files('libs/something_local.jar')} 2. 디렉토리에 위치한 모든 라이브러리의 의존성을 추가하는 방법dependencies { compile fileTree(dir: 'libs', include: ['*.jar'])} build.gradle에 넣고, build gradle을 하게 되면, 아래와 같이 내가 추가한 외부 라이브러리인 .jar 파일 안에..

springboot 외부 라이브러리 jar 파일 추가 방법 | How to Add External Library JAR Files in Spring Boot

728x90

로컬 프로젝트 안에 외부에서 받아온 라이브러리인 jar파일을 추가하는 방법을 찾아보았다.

gradle에 존재하지 않는 외부의 .jar 파일을 스프링 부트 안에 의존성을 추가하는 방법이다.

/build.gradle 파일 안에 명시를 해주어야 한다.

 

1. 한 개의 파일을 넣는 방법

dependencies {
    implementation files('libs/something_local.jar')
}

 

2. 디렉토리에 위치한 모든 라이브러리의 의존성을 추가하는 방법

dependencies {
   compile fileTree(dir: 'libs', include: ['*.jar'])
}

 

build.gradle에 넣고, build gradle을 하게 되면, 아래와 같이 내가 추가한 외부 라이브러리인 .jar 파일 안에 들어있는 Java 파일을 로드하고, 사용할 수 있게 된다.

 

3. 로컬 디렉토리를 라이브러리로 만들어서 의존성을 추가하는 방법

여기서도 마찬가지로 libs/something_local.jar 파일을 추가하는 방법이다.

repositories {
	mavenCentral()
	flatDir {
         dirs 'libs'
	}
}
dependencies {
	compile( name: 'something_local')
}

 

4. 외부라이브러리 추가시 충돌

java.lang.NoSuchMethodError: 'com.google.api.client.http.HttpRequest com.google.api.client.http.HttpRequest.setResponseReturnRawInputStream(boolean)'
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.buildHttpRequest(AbstractGoogleClientRequest.java:434) ~[google-api-client-1.30.4.jar:1.30.4]

외부 라이브러리로 .jar 파일을 추가하면서 정말 골치를 썩었던 문제였다.

분명, 내가 build.gradle에 추가한 dependency는 가장 상위버전이고, NoSuchMethodError 와 관련한 글을 구글링 해보았을 때 하위 버전에서 발생하는 문제이니 상위버전으로 version을 업데이트 하라는 해결을 준다.

그런데 intellij 상으로 아래 메세지를 보면 1.30.4 버전으로 가장 상위버전으로 표시가 된다.

 

이때 의심해야할 것은 외부 라이브러리에서도 똑같은 dependency를 사용하는데 외부 라이브러리의 버전이 낮을 경우이다.

이 경우에 classPath에 혼동이 생겨서 일어난 에러로 보인다. 내가 build.gradle에 추가한대로 현재 프로젝트의 classpath를 따라야하는데, 외부 라이브러리인 jar 파일의 classpath와의 충돌 때문인 것 같다.

Solution : 나는 이경우에 외부 라이브러리인 jar 파일을 압축 해제후, 해당 모듈을 지우고 다시 jar로 압축하는 방식을 사용했다.

 

jar 파일 압축 / 압축 해제

jar 파일을 라이브러리로 만드려는 과정에서, jar 파일안에 사용되는 라이브러리 gson, slf4j 두개가 내가 사용하는 springboot dependency와 충돌이 나는 상황이 있었다.

그래서 처음에 해당하는 모듈 두개를 아래 방식으로 삭제하려고 했다.

dependencies {
  compile (files('libs/something_local.jar')){
    exclude group: 'org.slf4j';
  }
}

그런데 적용이 되지 않는 방법이라서 결국 포기하고 외부 라이브러리인 jar 파일을 압축 해제하고 duplicate를 일으키는 모듈 그룹을 삭제하기로 결정했다.

 

압축 풀기

shell 열기 > jar 파일이 위치하는 directory로 이동하기

jar파일의 이름이 example.jar 라고 하자.

jar xvf [파일명].jar
jar xvf example.jar

 

압축 하기

shell 열기 > jar 파일이 위치하는 directory로 이동하기

현재 디렉토리를 압축하려고 하며, 압축 파일의 이름을 example.jar 라고 하자.

jar cvf [파일명].jar [디렉토리]
jar cvf example.jar .

 

I looked into how to add an external jar file (a library obtained from outside) to a local project.

This is how to add a dependency for an external .jar file that doesn't exist in gradle into a Spring Boot project.

You need to specify it in the /build.gradle file.

 

1. Adding a single file

dependencies {
    implementation files('libs/something_local.jar')
}

 

2. Adding dependencies for all libraries in a directory

dependencies {
   compile fileTree(dir: 'libs', include: ['*.jar'])
}

 

Once you add this to build.gradle and run build gradle, you'll be able to load and use the Java files inside the external .jar library you added, as shown below.

 

3. Adding a dependency by turning a local directory into a library

This is also a way to add the libs/something_local.jar file.

repositories {
	mavenCentral()
	flatDir {
         dirs 'libs'
	}
}
dependencies {
	compile( name: 'something_local')
}

 

4. Conflicts when adding external libraries

java.lang.NoSuchMethodError: 'com.google.api.client.http.HttpRequest com.google.api.client.http.HttpRequest.setResponseReturnRawInputStream(boolean)'
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.buildHttpRequest(AbstractGoogleClientRequest.java:434) ~[google-api-client-1.30.4.jar:1.30.4]

This was a really frustrating issue I ran into while adding an external .jar file as a library.

The dependency I added to build.gradle was clearly the latest version, and when I googled about NoSuchMethodError, the solutions all said it's an issue with older versions and to update to a newer version.

But looking at the message in IntelliJ below, it shows version 1.30.4, which is the latest version.

 

What you should suspect in this case is when the external library also uses the same dependency but with a lower version.

This error seems to be caused by confusion in the classPath. The current project should follow the classpath I specified in build.gradle, but it appears to conflict with the classpath of the external jar library.

Solution: In my case, I extracted the external jar file, removed the conflicting module, and then repackaged it as a jar.

 

Compressing / Extracting jar files

While trying to turn a jar file into a library, there was a situation where two libraries used inside the jar file — gson and slf4j — were conflicting with my Spring Boot dependencies.

So I initially tried to remove those two modules using the method below.

dependencies {
  compile (files('libs/something_local.jar')){
    exclude group: 'org.slf4j';
  }
}

But this approach didn't work, so I gave up on it and decided to extract the external jar file and delete the module groups causing the duplicates.

 

Extracting

Open shell > Navigate to the directory where the jar file is located

Let's say the jar file is named example.jar.

jar xvf [파일명].jar
jar xvf example.jar

 

Compressing

Open shell > Navigate to the directory where the jar file is located

Let's say we want to compress the current directory and name the archive example.jar.

jar cvf [파일명].jar [디렉토리]
jar cvf example.jar .

 

댓글

Comments