Gradle War 插件 - 重命名库

Gradle War plugin - Rename libraries

通过将我的一个项目从 Maven 3.6.3 迁移到 Gradle 6.5.1.

,我开始更深入地研究 Gradle

我已经到了必须在稍微定制的 impl 模块中构建一个 War 文件的阶段:我重命名了 lib 文件夹中的 Jars,并通过覆盖(来自Jar 在同一项目的 api 模块中构建)一些资源。 当前 Maven 配置如下:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <configuration>
    <outputFileNameMapping>@{groupId}@-@{artifactId}@-@{version}@.@{extension}@</outputFileNameMapping>
    <webResources>
      <resource>
        <directory>src/main/webapp</directory>
      </resource>
    </webResources>
    <overlays>
      <!-- Include the OpenAPI spec -->
      <overlay>
        <groupId>com.project.rest</groupId>
        <artifactId>api</artifactId>
        <type>jar</type>
        <includes>
          <include>specs/</include>
        </includes>
      </overlay>
    </overlays>
  </configuration>
</plugin>

所以我正在尝试为 Gradle 想出一些关于库重命名的类似的东西。
我在插件的文档中看到插件中有一个rename方法:

Renames a source file. The closure will be called with a single parameter, the name of the file. The closure should return a String object with a new target name. The closure may return null, in which case the original name will be used.

问题是它采用参数中的文件名,而我需要(为了模仿 outputFileNameMapping 选项)获取依赖项以便提取其元数据。
所以我认为这不是正确的选择。有没有办法用 Gradle 来实现这个?

谢谢

gradle war 插件有许多配置选项,包括 archiveFileName(或旧版本 gradle 上的 archiveName)。 archiveFileName 默认设置为:[archiveBaseName]-[archiveAppendix]-[archiveVersion]-[archiveClassifier].[archiveExtension]

这可以在 build.gradle 的 war {} 块中声明。你应该能够做这样的事情:

war {
  archiveFileName = "${project.group}-${project.name}-$archiveVersion.$archiveExtension"
}

有关可用配置选项的更多信息,请参阅the War documentation

这将重命名 war 文件本身。

如果您想重命名 war 文件的内容,您可以使用 war.rootSpec.rename(),如下所示:

// make a copy of the implementation configuration that can be resolved so we can loop over it.
configurations {
    implementationList {
        extendsFrom implementation
        canBeResolved true
    }
}

war {
    rootSpec.rename({ fileInWar ->
        def returnValue = fileInWar
        project.configurations.implementationList.resolvedConfiguration.resolvedArtifacts.each {
            if (it.file.name == fileInWar) {
                def depInfo = it.moduleVersion.id
                print "$returnValue -> "
                returnValue = "${depInfo.group}.${depInfo.name}-${depInfo.version}.${it.extension}"
                println "$returnValue"
            }
        }
        return returnValue
    })
}

但是请注意,如果您有重复的依赖项,这将无法解决冲突。