首页 > 解决方案 > Gradle:创建具有依赖项的单个 JAR

问题描述

我正在将我们的产品构建从 Ant 迁移到 Gradle,从单个项目开始并收到以下错误:

> Could not resolve all files for configuration ':Shared:serverbase:compileClasspath'.
   > Could not find guava:guava:23.3-jre.
     Searched in the following locations:
       - https://jcenter.bintray.com/guava/guava/23.3-jre/guava-23.3-jre.pom
       - file:/F:/pros/X/java/guava/guava-23.3-jre.xml
     Required by:
         project :Shared:serverbase

为什么它在寻找xml -files 而不是jar

这是我的文件:项目根目录中的 build.gradle:

buildscript {
    repositories {
        jcenter()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath 'de.richsource.gradle.plugins:gwt-gradle-plugin:0.6' // gwt compiler plugin
    }
}

allprojects {

    apply from: rootProject.file('libraries.gradle')
    repositories {
        jcenter()
        ivy {
            url  "file://${rootProject.projectDir}/ThirdParty/java/"
            patternLayout  {
                artifact "[organization]/[module](-[revision])(.[ext])"
            }
        }
    }
}

subprojects {
    apply plugin: 'java-library'

    sourceCompatibility = '1.8'
    targetCompatibility = '1.8'


    compileJava.options.debugOptions.debugLevel = "lines,vars,source"
    compileJava.options.compilerArgs += ["-nowarn"]
    compileJava.options.debug = true
}

这个单一项目的 build.gradle:

sourceSets.main.java.srcDirs = ['src']

dependencies {
    implementation "guava:guava:${guavaVersion}"
    implementation "slf4j:jul-to-slf4j:${slf4jVersion}"
    implementation "logback:logback-classic:${logbackVersion}"

}

jar {
    manifest {
        attributes 'Class-Path': configurations.compileClasspath.collect { it.getName() }.join(' ')
    }        

}

设置.gradle:

rootProject.name = 'X'
include 'Shared:serverbase'

图书馆.gradle:

ext {
...
    guavaVersion = '23.3-jre'
...
}

(部分内容删减)

如果我将文件依赖项添加到 build.gradle 作为本地文件(如何将本地 .jar 文件依赖项添加到 build.gradle 文件?

implementation files("guava-${guavaVersion}.jar")

我遇到了很多错误,例如“错误:包 org.slf4j 不存在”,因此似乎根本不满足依赖关系。结果应该是一个带有编译源的 jar。

我是gradle的新手,请原谅我的不悟。

标签: javagradlebuildant

解决方案


您的 Guava 依赖项不正确。

更改自:

implementation "guava:guava:${guavaVersion}"

至:

implementation "com.google.guava:guava:${guavaVersion}"

推荐阅读