gpt4 book ai didi

Gradle : how can I call a 'def' from an imported script?

转载 作者:行者123 更新时间:2023-12-04 15:22:16 26 4
gpt4 key购买 nike

我目前正在模块化我们的 gradle 构建,以便拥有一个包含许多全局内容的 libs/commons.gradle 文件。我需要这个,因为软件的各个分支并行开发,我们希望避免在所有分支之间传播每个脚本文件更改。

所以我创建了那个lib文件并使用“apply from”来加载它:

申请自:'gradle/glib/commons.gradle'

在 commons.gradle 我定义了 svnrevision 函数:

...

def svnRevision = {
ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
SVNClientManager clientManager = SVNClientManager.newInstance(options);
SVNStatusClient statusClient = clientManager.getStatusClient();
SVNStatus status = statusClient.doStatus(projectDir, false);
SVNRevision revision = status.getCommittedRevision();
return revision.getNumber().toString();
}

...

我从包含 build.gradle 的函数中调用该函数:

...
task writeVersionProperties {
File f = new File(project.webAppDirName+'/WEB-INF/version.properties');
if (f.exists()) { f.delete(); }

f = new File(project.webAppDirName+'/WEB-INF/version.properties');
FileOutputStream os = new FileOutputStream(f);
os.write(("version="+svnRevision()).getBytes());
os.flush();
os.close();
}

...

但我最终在:

...
FAILURE: Build failed with an exception.

* Where:
Build $PATH_TO/build20.gradle

* What went wrong:
A problem occurred evaluating root project 'DEV_7.X.X_GRADLEZATION'.
> Could not find method svnRevision() for arguments [] on root project 'DEV_7.X.X_GRADLEZATION'.

...

所以我的问题是 : 如何在包含脚本中定义的 gradle 中调用子函数?

任何帮助表示赞赏!

最佳答案

来自 http://www.gradle.org/docs/current/userguide/writing_build_scripts.html :

13.4.1. Local variables

Local variables are declared with the def keyword. They are only visible in the scope where they have been declared. Local variables are a feature of the underlying Groovy language.

13.4.2. Extra properties

All enhanced objects in Gradle's domain model can hold extra user-defined properties. This includes, but is not limited to, projects, tasks, and source sets. Extra properties can be added, read and set via the owning object's ext property. Alternatively, an ext block can be used to add multiple properties at once.



如果您将其声明为:
ext.svnRevision = {
...
}

并且不要更改调用,我希望它会起作用。

关于 Gradle : how can I call a 'def' from an imported script?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25641543/

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