Gradle:创建一个包含依赖项的单个JAR。

huangapple go评论76阅读模式
英文:

Gradle: create a single JAR with dependencies

问题

以下是已翻译的内容:

我正在将我们产品的构建从Ant迁移到Gradle,起初是从一个单一的项目开始的,然后出现了以下错误:

> 无法解析配置项“:Shared:serverbase:compileClasspath”的所有文件。
   > 找不到guava:guava:23.3-jre。
     在以下位置搜索:
       - 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
     所需项:
         项目 :Shared:serverbase

为什么它在寻找xml文件而不是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编译器插件
    }
}

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(' ')
    }        
           
}

settings.gradle:

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

libraries.gradle:

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

(某些内容已删除)

如果我将文件依赖项作为本地文件添加到build.gradle(https://stackoverflow.com/questions/20700053/how-to-add-local-jar-file-dependency-to-build-gradle-file)

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

我会得到大量类似“error: package org.slf4j does not exist”的错误,所以似乎根本没有满足依赖关系。
结果应该是一个带有已编译源代码的单一jar文件。

我是Gradle的初学者,请原谅我的不足之处。

英文:

I am migrating our product's build from Ant to Gradle, having started from a single project and got the following error:

> 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

Why it is looking for xml-files instead of jar?

Here are my files:
build.gradle in project's root directory:

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 of this single project:

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(' ')
    }        
           
}

settings.gradle:

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

libraries.gradle:

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

(some content stripped)

And if I add file dependency to build.gradle as local file (https://stackoverflow.com/questions/20700053/how-to-add-local-jar-file-dependency-to-build-gradle-file)

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

I got tons of errors like 'error: package org.slf4j does not exist' so it seems that dependencies were not satisfied at all.
The outcome should be a single jar with compiled sources.

I am a novice in gradle, please forgive my unenlightenment.

答案1

得分: 1

你的Guava依赖项不正确。

从:

implementation "guava:guava:${guavaVersion}"

更改为:

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

Your Guava dependency is not correct.

Change from:

implementation "guava:guava:${guavaVersion}"

To:

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

huangapple
  • 本文由 发表于 2020年5月5日 08:56:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/61604055.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定