gpt4 book ai didi

gradle installDist : How do I prepend the group id to dependency jar name (clashing due to different group id but same artifact id and version)?

转载 作者:行者123 更新时间:2023-12-03 02:48:18 24 4
gpt4 key购买 nike

Gradle 中的问题

我有这个 build.gradle在名为 proj 的目录中:

apply plugin: 'java'
apply plugin: 'java-library-distribution'
repositories {
mavenCentral()
}
dependencies {
compile 'com.davfx:util:1.0.0'
compile 'org.johnnei:util:1.0.0'
}

然后我运行 gradle installDist并找到目录 build/install/proj/lib只包含一个 util-1.0.0.jar .我期待两个 util-1.0.0.jar文件,但我认为由于文件系统的工作方式,这是不可能的。

问题是: 如何在 jar 名称前加上组 ID? Gradle 是否有类似 Maven 的 prependGroupId ?

Maven 中同样的问题

要重现该问题,请将以下片段复制到 pom.xml ,然后运行 ​​ mvn dependencies:copy-dependencies在同一个目录中。观察 target/dependency目录只包含一个文件 util-1.0.0.jar .但是,与 Gradle 不同的是,Maven 可以通过 prependGroupId 解决这个问题。 . (信用在评论部分归功于图纳基。)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.spacetimecat</groupId>
<artifactId>example</artifactId>
<version>0.0.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>com.davfx</groupId>
<artifactId>util</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.johnnei</groupId>
<artifactId>util</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>

</project>

旧的(更一般的)问题描述

假设有两个具有这些坐标的项目:
  • G:A:1.2.3 ,
  • H:A:1.2.3 ,

  • 也就是说,它们具有不同的组 ID,但具有相同的工件 ID 和版本。

    假设我创建了一个项目 P这取决于这两者。我只有一个 web-1.7.0.jar当我组装一个分发包(一个包含我的项目 jar 及其所有可传递依赖项的包)时的文件。问题是:依赖项中的 jar 文件之一被覆盖,因为它们具有相同的文件名,因为 Gradle/Maven 默认将这些 jar 命名为 artifactId-version-classifier.jar ,虽然他们有不同的组。

    最佳答案

    这很困惑,但您可以针对 tempCompile 声明您的依赖项配置然后添加一个 renamejars任务到DAG将 jar 重命名为 $buildDir/renamedJars目录。 compile然后配置可以包含来自 $buildDir/renamedJars 的 jars目录。

    重要 - 此解决方案将丢失所有依赖项的组/工件/版本,这意味着下游项目将无法执行依赖项解析。

    configurations {
    tempCompile
    }
    dependencies {
    tempCompile 'com.davfx:util:1.0.0'
    tempCompile 'org.johnnei:util:1.0.0'
    compile fileTree("$buildDir/renamedJars")
    }
    task renameJars {
    inputs.files configurations.tempCompile
    outputs.dir "$buildDir/renamedJars"
    doLast {
    mkdir "$buildDir/renamedJars"
    ResolvedConfiguration rc = configurations.tempCompile.resolvedConfiguration
    Set<ResolvedArtifact> artifacts = rc.resolvedArtifacts
    artifacts.each { ra ->
    ModuleVersionIdentifier mvi = ra.moduleVersion.id
    copy {
    from ra.file
    into "$buildDir/renamedJars"
    rename { old ->
    return "${mvi.group}-${mvi.name}-${ra.classifier}-${mvi.version}.${ra.extension}"
    }
    }
    }
    }
    }
    compileJava.dependsOn renameJars


  • Configuration.getResolvedConfiguration()
  • ResolvedConfiguration
  • ResolvedArtifact
  • ModuleVersionIdentifier
  • 关于gradle installDist : How do I prepend the group id to dependency jar name (clashing due to different group id but same artifact id and version)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42174572/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com