2017-03-21 7 views

答えて

2

すべての依存関係を分析し、不適切なものが見つかった場合は例外をスローすることができます。ここでは、クラスパス上の特定の依存関係のためにそれを行うコードだ:

apply plugin: 'java' 
repositories { 
    mavenCentral() 
} 

// This is just to have some dependencies on the classpath. 

dependencies { 
    compile 'org.springframework.boot:spring-boot-starter:1.5.1.RELEASE' 
    compile "org.webjars:webjars-locator:+" 
} 

// run this task to analyze all deps 

task checkDeps { 
    doLast { 
     // this closure simply prints a given dependency and throws an exception if slf4j-api is found 
     def failIfBad = { dep -> 
      println "CHECKING: $dep.module.id.group:$dep.module.id.name" 
      if (dep.module.id.name == 'slf4j-api') { 
       throw new GradleException("$dep.module.id.group:$dep.module.id.name on classpath!") 
      } 
     } 
     // this is a closure with recursion that calls the above failCheck on all children and their children 
     def checkChildren 
     checkChildren = { dep -> 
      if (dep.children.size() != 0) { 
       dep.children.each { child -> 
        failIfBad(child) 
        checkChildren(child) 
       } 
      } 
     } 
     // this is how you get all dependencies in the compile scope, iterate on the first level deps and then their children 
     configurations.compile.resolvedConfiguration.firstLevelModuleDependencies.each { firstLevelDep -> 
      failIfBad(firstLevelDep); 
      checkChildren(firstLevelDep); 
     } 
    } 
} 

全体のコードは、おそらく最適化することができ、より洗練されたルールが必要になりますが、それはあなたが始めるために必要なものを与える必要があります。

関連する問題