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

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

Gradle: create a single JAR with dependencies

问题

以下是已翻译的内容:

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

  1. > 无法解析配置项“:Shared:serverbase:compileClasspath”的所有文件。
  2. > 找不到guava:guava:23.3-jre
  3. 在以下位置搜索:
  4. - https://jcenter.bintray.com/guava/guava/23.3-jre/guava-23.3-jre.pom
  5. - file:/F:/pros/X/java/guava/guava-23.3-jre.xml
  6. 所需项:
  7. 项目 :Shared:serverbase

为什么它在寻找xml文件而不是jar文件?

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

  1. buildscript {
  2. repositories {
  3. jcenter()
  4. maven {
  5. url "https://plugins.gradle.org/m2/"
  6. }
  7. }
  8. dependencies {
  9. classpath 'de.richsource.gradle.plugins:gwt-gradle-plugin:0.6' // gwt编译器插件
  10. }
  11. }
  12. allprojects {
  13. apply from: rootProject.file('libraries.gradle')
  14. repositories {
  15. jcenter()
  16. ivy {
  17. url "file://${rootProject.projectDir}/ThirdParty/java/"
  18. patternLayout {
  19. artifact "[organization]/[module](-[revision])(.[ext])"
  20. }
  21. }
  22. }
  23. }
  24. subprojects {
  25. apply plugin: 'java-library'
  26. sourceCompatibility = '1.8'
  27. targetCompatibility = '1.8'
  28. compileJava.options.debugOptions.debugLevel = "lines,vars,source"
  29. compileJava.options.compilerArgs += ["-nowarn"]
  30. compileJava.options.debug = true
  31. }

此单一项目的build.gradle:

  1. sourceSets.main.java.srcDirs = ['src']
  2. dependencies {
  3. implementation "guava:guava:${guavaVersion}"
  4. implementation "slf4j:jul-to-slf4j:${slf4jVersion}"
  5. implementation "logback:logback-classic:${logbackVersion}"
  6. }
  7. jar {
  8. manifest {
  9. attributes 'Class-Path': configurations.compileClasspath.collect { it.getName() }.join(' ')
  10. }
  11. }

settings.gradle:

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

libraries.gradle:

  1. ext {
  2. ...
  3. guavaVersion = '23.3-jre'
  4. ...
  5. }

(某些内容已删除)

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

  1. 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:

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

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

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

  1. buildscript {
  2. repositories {
  3. jcenter()
  4. maven {
  5. url "https://plugins.gradle.org/m2/"
  6. }
  7. }
  8. dependencies {
  9. classpath 'de.richsource.gradle.plugins:gwt-gradle-plugin:0.6' // gwt compiler plugin
  10. }
  11. }
  12. allprojects {
  13. apply from: rootProject.file('libraries.gradle')
  14. repositories {
  15. jcenter()
  16. ivy {
  17. url "file://${rootProject.projectDir}/ThirdParty/java/"
  18. patternLayout {
  19. artifact "[organization]/[module](-[revision])(.[ext])"
  20. }
  21. }
  22. }
  23. }
  24. subprojects {
  25. apply plugin: 'java-library'
  26. sourceCompatibility = '1.8'
  27. targetCompatibility = '1.8'
  28. compileJava.options.debugOptions.debugLevel = "lines,vars,source"
  29. compileJava.options.compilerArgs += ["-nowarn"]
  30. compileJava.options.debug = true
  31. }

build.gradle of this single project:

  1. sourceSets.main.java.srcDirs = ['src']
  2. dependencies {
  3. implementation "guava:guava:${guavaVersion}"
  4. implementation "slf4j:jul-to-slf4j:${slf4jVersion}"
  5. implementation "logback:logback-classic:${logbackVersion}"
  6. }
  7. jar {
  8. manifest {
  9. attributes 'Class-Path': configurations.compileClasspath.collect { it.getName() }.join(' ')
  10. }
  11. }

settings.gradle:

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

libraries.gradle:

  1. ext {
  2. ...
  3. guavaVersion = '23.3-jre'
  4. ...
  5. }

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

  1. 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依赖项不正确。

从:

  1. implementation "guava:guava:${guavaVersion}"

更改为:

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

Your Guava dependency is not correct.

Change from:

  1. implementation "guava:guava:${guavaVersion}"

To:

  1. 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:

确定