Gradle War 插件 – 重命名库

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

Gradle War plugin - Rename libraries

问题

我正在逐渐深入了解Gradle,通过将我有的一个项目从Maven 3.6.3迁移到Gradle 6.5.1。

我已经到了构建impl模块中的War文件的阶段,该文件稍微定制化:我重命名lib文件夹中的Jars,并通过一个覆盖(来自同一项目中的api模块构建的Jar)包含一些资源。

当前的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方法:

重命名源文件。闭包将以一个参数调用,即文件的名称。闭包应返回一个带有新目标名称的String对象。闭包可以返回null,此时将使用原始名称。

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

谢谢

英文:

I am starting to get deeper into Gradle by migrating one project I have From Maven 3.6.3 to Gradle 6.5.1.

I'm arriving at the stage where I have to build a War file in the impl module that is slightly customized: I rename the Jars in the lib folder, and include via an overlay (from a Jar built in the api module from the same project) some resources.
The current Maven configuration is the following:

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

So I'm trying to come up with something similar for Gradle regarding the libraries renaming.
I saw in the documentation of the plugin that there is a rename method in the plugin:

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

The issue is that is takes the name of file in parameter, whereas I would need (in order to mimic the outputFileNameMapping option) to get the dependency so I could extract its metadata.
So I assume this is not the right option. Is there a way to achieve this with Gradle?

Thanks

答案1

得分: 1

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

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

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

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

这将重命名war文件本身。

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

// 创建一个可以解析的实现配置的副本,以便我们可以循环遍历它。
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
    })
}

但请注意,如果您有重复的依赖关系,这将不会解决冲突。

英文:

The gradle war plugin has a number of configuration options, including archiveFileName (or archiveName on older versions of gradle). archiveFileName by default is set to: [archiveBaseName]-[archiveAppendix]-[archiveVersion]-[archiveClassifier].[archiveExtension]

This can be declared in the war {} block in your build.gradle. You should be able to do something like this:

war {
  archiveFileName = &quot;${project.group}-${project.name}-$archiveVersion.$archiveExtension&quot;
}

For more on the available configuration options, see the War documentation

This will rename the war file itself.

If you want to rename contents of the war file instead, you can use the war.rootSpec.rename(), like so:

// 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 -&gt;
        def returnValue = fileInWar
        project.configurations.implementationList.resolvedConfiguration.resolvedArtifacts.each {
            if (it.file.name == fileInWar) {
                def depInfo = it.moduleVersion.id
                print &quot;$returnValue -&gt; &quot;
                returnValue = &quot;${depInfo.group}.${depInfo.name}-${depInfo.version}.${it.extension}&quot;
                println &quot;$returnValue&quot;
            }
        }
        return returnValue
    })
}

However, note that this will not resolve conflicts if you have duplicate dependencies.

huangapple
  • 本文由 发表于 2020年8月3日 23:44:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63232639.html
匿名

发表评论

匿名网友

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

确定