programing tip

Gradle에서 사용하지 않는 종속성을 찾고 제거하는 방법

itbloger 2020. 8. 14. 07:29
반응형

Gradle에서 사용하지 않는 종속성을 찾고 제거하는 방법


내 프로젝트에서 사용하지 않는 종속성을 찾고 싶었습니다. Maven과 같은 gradle에 이것에 대한 기능이 있습니까?


업데이트 : 28-06-2016 : 사용되지 않는 종속성에 대한 Android 지원

20176 월 에 그들은 4.0.0 version루트 프로젝트 이름 "gradle-lint-plugin""nebula-lint-plugin". 또한 unused-dependencyAndroid 지원을 추가했습니다 .


20165 월 Gradle은 원치 않는 종속성을 찾고 제거하기 위해 gradle lint 플러그인구현 했습니다.

Gradle Lint 플러그인 : 전체 문서

Gradle Lint 플러그인은 Gradle 스크립트 및 관련 파일의 오용 또는 지원 중단 패턴을 식별하고보고하기위한 플러그 가능하고 구성 가능한 linter 도구입니다.

이 플러그인에는 다양한 규칙이 있습니다. 사용하지 않는 종속성 규칙 은 그중 하나입니다. 3 가지 특징이 있습니다.

  1. 사용하지 않는 종속성을 제거합니다.
  2. 코드에서 직접 사용하는 전이 종속성을 명시적인 1 차 종속성으로 승격합니다.
  3. 종속성을 '올바른'구성으로 재배치합니다.

규칙을 적용하려면 다음을 추가하십시오.

gradleLint.rules += 'unused-dependency'

미사용 종속성 규칙 에 대한 자세한 내용은 마지막 부분에 나와 있습니다.

Gradle Lint 플러그인을 적용하려면 :

buildscript { repositories { jcenter() } }
plugins {
  id 'nebula.lint' version '0.30.2'
}

또는 :

buildscript {
  repositories { jcenter() }
  dependencies {
    classpath 'com.netflix.nebula:gradle-lint-plugin:latest.release'
  }
}

apply plugin: 'nebula.lint'

린트 할 규칙을 정의합니다.

gradleLint.rules = ['all-dependency'] // add as many rules here as you'd like

엔터프라이즈 빌드의 경우 init.gradle 스크립트 또는 Gradle 적용 메커니즘을 통해 포함 된 gradle 스크립트에서 Lint 규칙을 정의하는 것이 좋습니다.

다중 모듈 프로젝트의 경우 allprojects 블록에 플러그인을 적용하는 것이 좋습니다.

allprojects {
  apply plugin: 'nebula.lint'
  gradleLint.rules = ['all-dependency'] // add as many rules here as you'd like
}


미사용 종속성 규칙 에 대한 자세한 내용은 이 부분에 나와 있습니다.

규칙을 적용하려면 다음을 추가하십시오.

gradleLint.rules += 'unused-dependency'

이 규칙은 프로젝트의 소스 세트 에서 생성 되는 컴파일 된 바이너리를 검사 하여 클래스 참조를 찾고 이러한 참조를 종속성 블록 에서 선언 한 종속성과 일치시킵니다 .

특히이 규칙은 종속성을 다음과 같이 조정합니다.

1) 사용하지 않는 종속성 제거

  • com.amazonaws : aws-java-sdk와 같은 패밀리 스타일 jar는 코드를 포함하지 않으므로 제거됩니다.

2) 코드에서 직접 사용하는 전이 종속성을 명시적인 1 차 종속성으로 승격

  • com.amazonaws : aws-java-sdk와 같은 패밀리 스타일 jar를 실제로 사용중인 부분으로 분리하고이를 1 차 종속성으로 추가하는 부작용이 있습니다.

3) 종속성을 '올바른'구성으로 재배치합니다.

  • Webjar가 런타임 구성으로 이동 됨
  • META-INF 외부의 클래스 및 콘텐츠를 포함하지 않는 Jar는 런타임으로 이동됩니다.
  • 'xerces', 'xercesImpl', 'xml-apis'는 항상 런타임 범위 여야합니다.
  • mysql-connector-java와 같은 서비스 공급자 (META-INF / 서비스를 포함하는 jar)는 입증 가능한 컴파일 시간 참조가없는 경우 런타임으로 이동됩니다.
  • 종속성은 가능한 가장 높은 소스 세트 구성으로 이동됩니다. 예를 들어, 'junit'는 기본 소스 세트 (드물게)에 명시적인 종속성이없는 한 testCompile로 재배치됩니다.


업데이트 : 이전 플러그인

귀하의 친절한 정보를 위해 이전 플러그인에 대해 공유하고 싶습니다

  1. 사용되지 않는 종속성을 찾아 선언되고 전이되는 Gradle 플러그인은 com.github.nullstress.dependency-analysis입니다.

But it's latest version 1.0.3 is created 23 December 2014. After that there is no update.

N.B: Many of our engineers are being confused about this plugin as they updated only the version number nothing else.


The project mentioned in the earlier answers seem to be dead. I use gradle-dependency-analyze. Setup is simple:

buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath 'ca.cutterslade.gradle:gradle-dependency-analyze:1.0.3'
  }
}

apply plugin: 'ca.cutterslade.analyze'

Then do:

$ gradle analyzeDependencies

I've had a lot of luck using the Gradle Dependency Analysis Plugin. To get started with it, add the following two things to your Gradle build script.

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "com.github.nullstress:DependencyAnalysisPlugin:1.0.3"
    }
}

and

apply plugin: "dependencyAnalysis"

Once those are in place, run gradle analyze. If there are unused dependencies, you'll get a build failure that shows output similar to the text below, plus a list of the unused dependencies (both declared and transitive). The build failure is really handy if you want to enforce that there should be no unused dependencies via a CI build.

:foo:analyze FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':foo:analyze'.
> The project has unused declared artifacts

The projects on most of the historical answers are dead, but gradle-dependency-analyze appears to be alive at the time of this writing (last commit was two days ago).


Editor's Note: This answer is out of date, please see the top answer.

You can try com.github.nullstress.dependency-analysis gradle plugin

Build script snippet for use in all Gradle versions:

buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath "com.github.nullstress:DependencyAnalysisPlugin:1.0.3"
  }
}

apply plugin: "com.github.nullstress.dependency-analysis"

Build script snippet for new, incubating, plugin mechanism introduced in Gradle 2.1:

plugins {
  id "com.github.nullstress.dependency-analysis" version "1.0.3"
}

Also, there is a thread (Is there a Gradle equivalent of "mvn dependency:analyze"?) in gradle forum about this.

참고URL : https://stackoverflow.com/questions/19379517/how-to-find-remove-unused-dependencies-in-gradle

반응형