[关闭]
@ZeroGeek 2017-09-15T01:55:02.000000Z 字数 1587 阅读 840

Gradle依赖管理

Gradle


1.添加依赖

  1. dependencies {
  2. compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
  3. // 简写方式
  4. compile 'org.hibernate:hibernate-core:3.6.7.Final'
  5. }

2.基本添加方式

  1. apply plugin: 'java'
  2. //so that we can use 'compile', 'testCompile' for dependencies
  3. dependencies {
  4. //for dependencies found in artifact repositories you can use
  5. //the group:name:version notation
  6. compile 'commons-lang:commons-lang:2.6'
  7. testCompile 'org.mockito:mockito:1.9.0-rc1'
  8. //map-style notation:
  9. compile group: 'com.google.code.guice', name: 'guice', version: '1.0'
  10. //declaring arbitrary files as dependencies
  11. compile files('hibernate.jar', 'libs/spring.jar')
  12. //putting all jars from 'libs' onto compile classpath
  13. compile fileTree('libs')
  14. }

3.解决依赖冲突

排除部分重复依赖

  1. apply plugin: 'java' //so that I can declare 'compile' dependencies
  2. dependencies {
  3. compile('org.hibernate:hibernate:3.1') {
  4. //in case of versions conflict '3.1' version of hibernate wins:
  5. force = true
  6. //excluding a particular transitive dependency:
  7. exclude module: 'cglib' //by artifact name
  8. exclude group: 'org.jmock' //by group
  9. exclude group: 'org.unwanted', module: 'iAmBuggy' //by both name and group
  10. //disabling all transitive dependencies of this dependency
  11. transitive = false
  12. }
  13. }

Project依赖

  1. compile project(':common')

所有类型

配置处理依赖

  1. configurations {
  2. compile {
  3. description = 'compile classpath'
  4. transitive = true
  5. }
  6. runtime {
  7. extendsFrom compile
  8. }
  9. }
  10. configurations.compile {
  11. description = 'compile classpath'
  12. }
  13. // 不使用compile,改成runtime
  14. dependencies {
  15. runtime "org.groovy:groovy:2.2.0@jar"
  16. runtime group: 'org.groovy', name: 'groovy', version: '2.2.0', ext: 'jar'
  17. }

查看依赖

./gradlew dependencies app:dependencies
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注