【问题标题】:Same build.gradle file getting different versions for a dependency (org.json:json)相同的 build.gradle 文件为依赖项获取不同的版本(org.json:json)
【发布时间】:2018-03-19 08:22:38
【问题描述】:

我已经导入了一个与我的同事一起运行良好的项目,但是,一个 gradle 依赖项 (org.json:json) 被导入为不同的版本,尽管我们使用的是相同的构建。 gradle 文件如下所示

我的依赖版本是 2013,它们的版本是 2010,其中 JSONObject.getString() 函数在运行应用程序时都会导致我的机器出错。

如何确保我获得与他们相同的依赖关系以及造成这种差异的原因是什么?

build.gradle 文件如下所示:

buildscript {
    ext {
        springBootVersion = '1.5.9.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'

group = 'com.ejada'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.7

repositories {
    mavenCentral()
    mavenLocal()
    maven  {
        url 'http://repo.enonic.com/public/'
    }
}

configurations {
    providedRuntime
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile("org.springframework:spring-context-support")
    compile("org.springframework:spring-jdbc")
    compile files('libs/ojdbc6.jar')
    compile group: 'nl.captcha', name: 'simplecaptcha', version: '1.2.1'
    compile group: 'commons-codec', name: 'commons-codec', version: '1.9'
    compile group: 'org.antlr', name: 'stringtemplate', version: '4.0.2'
    compile("org.json:json")

    compile('com.google.code.gson:gson:2.7')

    // For XML Response
    compile('com.fasterxml.jackson.dataformat:jackson-dataformat-xml')

    compile("org.springframework.boot:spring-boot-starter-security")
    compile("org.springframework.ldap:spring-ldap-core")
    compile("org.springframework.security:spring-security-ldap")
    compile("com.unboundid:unboundid-ldapsdk")
    compile("org.springframework:spring-tx")

    compile('org.apache.directory.studio:org.apache.commons.io:2.4')

    compile("org.springframework.boot:spring-boot-devtools")
    compile group: 'com.github.ulisesbocchio', name: 'jasypt-spring-boot-starter', version: '1.4-java7'
    compile('javax.servlet:javax.servlet-api')
    compile("org.aspectj:aspectjweaver")
    compile("com.fasterxml.jackson.core:jackson-databind")

}

按要求添加 cmd gradlew dependencies --configuration runtime 的输出(以下两台机器完全相同)

runtime - Runtime dependencies for source set 'main' (deprecated, use 'runtimeOnly ' instead).
+--- org.springframework.boot:spring-boot-starter-web: -> 1.5.9.RELEASE
|    +--- org.springframework.boot:spring-boot-starter:1.5.9.RELEASE
|    |    +--- org.springframework.boot:spring-boot:1.5.9.RELEASE
|    |    |    +--- org.springframework:spring-core:4.3.13.RELEASE
|    |    |    \--- org.springframework:spring-context:4.3.13.RELEASE
|    |    |         +--- org.springframework:spring-aop:4.3.13.RELEASE
|    |    |         |    +--- org.springframework:spring-beans:4.3.13.RELEASE
|    |    |         |    |    \--- org.springframework:spring-core:4.3.13.RELEASE
|    |    |         |    \--- org.springframework:spring-core:4.3.13.RELEASE
|    |    |         +--- org.springframework:spring-beans:4.3.13.RELEASE (*)
|    |    |         +--- org.springframework:spring-core:4.3.13.RELEASE
|    |    |         \--- org.springframework:spring-expression:4.3.13.RELEASE
|    |    |              \--- org.springframework:spring-core:4.3.13.RELEASE
|    |    +--- org.springframework.boot:spring-boot-autoconfigure:1.5.9.RELEASE
|    |    |    \--- org.springframework.boot:spring-boot:1.5.9.RELEASE (*)
|    |    +--- org.springframework.boot:spring-boot-starter-logging:1.5.9.RELEASE
|    |    |    +--- ch.qos.logback:logback-classic:1.1.11
|    |    |    |    +--- ch.qos.logback:logback-core:1.1.11
|    |    |    |    \--- org.slf4j:slf4j-api:1.7.22 -> 1.7.25
|    |    |    +--- org.slf4j:jcl-over-slf4j:1.7.25
|    |    |    |    \--- org.slf4j:slf4j-api:1.7.25
|    |    |    +--- org.slf4j:jul-to-slf4j:1.7.25
|    |    |    |    \--- org.slf4j:slf4j-api:1.7.25
|    |    |    \--- org.slf4j:log4j-over-slf4j:1.7.25
|    |    |         \--- org.slf4j:slf4j-api:1.7.25
|    |    +--- org.springframework:spring-core:4.3.13.RELEASE
|    |    \--- org.yaml:snakeyaml:1.17
|    +--- org.springframework.boot:spring-boot-starter-tomcat:1.5.9.RELEASE
|    |    +--- org.apache.tomcat.embed:tomcat-embed-core:8.5.23
|    |    |    \--- org.apache.tomcat:tomcat-annotations-api:8.5.23
|    |    +--- org.apache.tomcat.embed:tomcat-embed-el:8.5.23
|    |    \--- org.apache.tomcat.embed:tomcat-embed-websocket:8.5.23
|    |         \--- org.apache.tomcat.embed:tomcat-embed-core:8.5.23 (*)
|    +--- org.hibernate:hibernate-validator:5.3.6.Final
|    |    +--- javax.validation:validation-api:1.1.0.Final
|    |    +--- org.jboss.logging:jboss-logging:3.3.0.Final -> 3.3.1.Final
|    |    \--- com.fasterxml:classmate:1.3.1 -> 1.3.4
|    +--- com.fasterxml.jackson.core:jackson-databind:2.8.10
|    |    +--- com.fasterxml.jackson.core:jackson-annotations:2.8.0
|    |    \--- com.fasterxml.jackson.core:jackson-core:2.8.10
|    +--- org.springframework:spring-web:4.3.13.RELEASE
|    |    +--- org.springframework:spring-aop:4.3.13.RELEASE (*)
|    |    +--- org.springframework:spring-beans:4.3.13.RELEASE (*)
|    |    +--- org.springframework:spring-context:4.3.13.RELEASE (*)
|    |    \--- org.springframework:spring-core:4.3.13.RELEASE
|    \--- org.springframework:spring-webmvc:4.3.13.RELEASE
|         +--- org.springframework:spring-aop:4.3.13.RELEASE (*)
|         +--- org.springframework:spring-beans:4.3.13.RELEASE (*)
|         +--- org.springframework:spring-context:4.3.13.RELEASE (*)
|         +--- org.springframework:spring-core:4.3.13.RELEASE
|         +--- org.springframework:spring-expression:4.3.13.RELEASE (*)
|         \--- org.springframework:spring-web:4.3.13.RELEASE (*)
+--- org.springframework:spring-context-support: -> 4.3.13.RELEASE
|    +--- org.springframework:spring-beans:4.3.13.RELEASE (*)
|    +--- org.springframework:spring-context:4.3.13.RELEASE (*)
|    \--- org.springframework:spring-core:4.3.13.RELEASE
+--- org.springframework:spring-jdbc: -> 4.3.13.RELEASE
|    +--- org.springframework:spring-beans:4.3.13.RELEASE (*)
|    +--- org.springframework:spring-core:4.3.13.RELEASE
|    \--- org.springframework:spring-tx:4.3.13.RELEASE
|         +--- org.springframework:spring-beans:4.3.13.RELEASE (*)
|         \--- org.springframework:spring-core:4.3.13.RELEASE
+--- nl.captcha:simplecaptcha:1.2.1
+--- commons-codec:commons-codec:1.9
+--- org.antlr:stringtemplate:4.0.2
|    \--- org.antlr:antlr-runtime:3.3
|         \--- org.antlr:stringtemplate:3.2.1 -> 4.0.2 (*)
+--- org.json:json: -> 20140107
+--- com.google.code.gson:gson:2.7
+--- com.fasterxml.jackson.dataformat:jackson-dataformat-xml: -> 2.8.10
|    +--- com.fasterxml.jackson.core:jackson-core:2.8.10
|    +--- com.fasterxml.jackson.core:jackson-annotations:2.8.0
|    +--- com.fasterxml.jackson.core:jackson-databind:2.8.10 (*)
|    +--- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:2.8.10
|    |    +--- com.fasterxml.jackson.core:jackson-core:2.8.10
|    |    +--- com.fasterxml.jackson.core:jackson-annotations:2.8.0
|    |    \--- com.fasterxml.jackson.core:jackson-databind:2.8.10 (*)
|    +--- org.codehaus.woodstox:stax2-api:3.1.4
|    \--- com.fasterxml.woodstox:woodstox-core:5.0.3
|         \--- org.codehaus.woodstox:stax2-api:3.1.4
+--- org.springframework.boot:spring-boot-starter-security: -> 1.5.9.RELEASE
|    +--- org.springframework.boot:spring-boot-starter:1.5.9.RELEASE (*)
|    +--- org.springframework:spring-aop:4.3.13.RELEASE (*)
|    +--- org.springframework.security:spring-security-config:4.2.3.RELEASE
|    |    +--- org.springframework.security:spring-security-core:4.2.3.RELEASE
|    |    |    +--- aopalliance:aopalliance:1.0
|    |    |    +--- org.springframework:spring-aop:4.3.9.RELEASE -> 4.3.13.RELEASE (*)
|    |    |    +--- org.springframework:spring-beans:4.3.9.RELEASE -> 4.3.13.RELEASE (*)
|    |    |    +--- org.springframework:spring-context:4.3.9.RELEASE -> 4.3.13.RELEASE (*)
|    |    |    +--- org.springframework:spring-core:4.3.9.RELEASE -> 4.3.13.RELEASE
|    |    |    \--- org.springframework:spring-expression:4.3.9.RELEASE -> 4.3.13.RELEASE (*)
|    |    +--- org.springframework:spring-aop:4.3.9.RELEASE -> 4.3.13.RELEASE (*)
|    |    +--- org.springframework:spring-beans:4.3.9.RELEASE -> 4.3.13.RELEASE (*)
|    |    +--- org.springframework:spring-context:4.3.9.RELEASE -> 4.3.13.RELEASE (*)
|    |    \--- org.springframework:spring-core:4.3.9.RELEASE -> 4.3.13.RELEASE
|    \--- org.springframework.security:spring-security-web:4.2.3.RELEASE
|         +--- org.springframework.security:spring-security-core:4.2.3.RELEASE (*)
|         +--- org.springframework:spring-beans:4.3.9.RELEASE -> 4.3.13.RELEASE (*)
|         +--- org.springframework:spring-context:4.3.9.RELEASE -> 4.3.13.RELEASE (*)
|         +--- org.springframework:spring-core:4.3.9.RELEASE -> 4.3.13.RELEASE
|         +--- org.springframework:spring-expression:4.3.9.RELEASE -> 4.3.13.RELEASE (*)
|         \--- org.springframework:spring-web:4.3.9.RELEASE -> 4.3.13.RELEASE (*)
+--- org.springframework.ldap:spring-ldap-core: -> 2.3.2.RELEASE
|    +--- org.slf4j:slf4j-api:1.7.21 -> 1.7.25
|    +--- org.springframework:spring-beans:4.3.4.RELEASE -> 4.3.13.RELEASE (*)
|    +--- org.springframework:spring-core:4.3.4.RELEASE -> 4.3.13.RELEASE
|    \--- org.springframework:spring-tx:4.3.4.RELEASE -> 4.3.13.RELEASE (*)
+--- org.springframework.security:spring-security-ldap: -> 4.2.3.RELEASE
|    +--- org.springframework.ldap:spring-ldap-core:2.2.0.RELEASE -> 2.3.2.RELEASE (*)
|    +--- org.springframework.security:spring-security-core:4.2.3.RELEASE (*)
|    +--- org.springframework:spring-beans:4.3.9.RELEASE -> 4.3.13.RELEASE (*)
|    +--- org.springframework:spring-context:4.3.9.RELEASE -> 4.3.13.RELEASE (*)
|    +--- org.springframework:spring-core:4.3.9.RELEASE -> 4.3.13.RELEASE
|    \--- org.springframework:spring-tx:4.3.9.RELEASE -> 4.3.13.RELEASE (*)
+--- com.unboundid:unboundid-ldapsdk: -> 3.2.1
+--- org.springframework:spring-tx: -> 4.3.13.RELEASE (*)
+--- org.apache.directory.studio:org.apache.commons.io:2.4
|    \--- commons-io:commons-io:2.4
+--- org.springframework.boot:spring-boot-devtools: -> 1.5.9.RELEASE
|    +--- org.springframework.boot:spring-boot:1.5.9.RELEASE (*)
|    \--- org.springframework.boot:spring-boot-autoconfigure:1.5.9.RELEASE (*)
+--- com.github.ulisesbocchio:jasypt-spring-boot-starter:1.4-java7
|    \--- com.github.ulisesbocchio:jasypt-spring-boot:1.4-java7
|         \--- org.jasypt:jasypt:1.9.2
+--- javax.servlet:javax.servlet-api: -> 3.1.0
+--- org.aspectj:aspectjweaver: -> 1.8.13
+--- com.fasterxml.jackson.core:jackson-databind: -> 2.8.10 (*)
\--- org.springframework.boot:spring-boot-starter-tomcat: -> 1.5.9.RELEASE (*)

【问题讨论】:

    标签: java json spring spring-boot gradle


    【解决方案1】:

    您可以使用gradle dependencies --configuration runtime 来确定包含不同版本的方式。稍后,您可以从不需要的组(或工件)中排除依赖项,并为依赖项定义所需的版本。

     compile('group:artifact:version') {
        exclude group: 'org.json', module: 'json' 
     }
     compile("org.json:json:requiredVersion")
    

    【讨论】:

    • 谢谢,如何在 Eclipse 中运行 gradle cmd?我在其中找不到终端,还是应该使用外部终端?另外我认为最好只指出所需的版本而不排除,对吗?
    • 我可以使用项目中的终端运行 cmd,但使用 gradlew,但是机器之间的依赖关系有何不同?
    • 是的,即使是 ('gradlew') 也会有所帮助。能否请您添加输出。这将有助于弄清楚它从哪里得到不同的版本@AhmedElkoussy
    • 根据 cmd 输出的要求更新了问题,令人惊讶的是它在两台机器上完全相同@lucid
    • 你能给我一些截图或一些我可以检查两个版本的东西吗? @AhmedElkoussy
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    相关资源
    最近更新 更多