[英]Configure Gradle Plugin inside Gradle plugin
I want to create a custom Gradle plugin that will encapsulate Checkstyle and PMD configurations.我想创建一个自定义 Gradle 插件,它将封装 Checkstyle 和 PMD 配置。 So, other projects can just apply one custom plugin without bothering about any additional configurations.因此,其他项目可以只应用一个自定义插件,而无需担心任何额外的配置。
I applied checkstyle
plugin.我应用checkstyle
插件。
plugins {
id 'java-gradle-plugin'
id 'checkstyle'
}
And then I applied it inside my custom plugin.然后我将它应用到我的自定义插件中。
public class CustomPlugin implements Plugin<Project> {
public void apply(Project project) {
project.getPluginManager().apply(CheckstylePlugin.class);
}
}
When I try to build the project I get an error.当我尝试构建项目时出现错误。
Unable to find: config/checkstyle/checkstyle.xml
How can I override other plugin's properties?如何覆盖其他插件的属性? For example, I want to change the default checkstyle.xml
path.例如,我想更改默认的checkstyle.xml
路径。 I can do it manually inside build.gradle
of the plugin project itself.我可以在插件项目本身的build.gradle
中手动完成。 But in this case, other projects that apply the plugin won't have this configurations defined by default (I tested it).但是在这种情况下,其他应用该插件的项目将不会默认定义此配置(我测试过)。
I managed to configure checkstyle
plugin with ChecktyleExtension
.我设法用ChecktyleExtension
配置了checkstyle
插件。
public class MetricCodingRulesGradlePluginPlugin implements Plugin<Project> {
public void apply(Project project) {
project.getPluginManager().apply("checkstyle");
project.getExtensions().configure(CheckstyleExtension.class, checkstyleExtension -> {
checkstyleExtension.setConfigFile(new File("style/checkstyle.xml"));
});
}
}
checkstyle.xml
is placed in the plugin project. checkstyle.xml
放在插件项目中。 When I try to apply it within any other project, checkstyle
searches it inside the current project directory but not the plugin's one.当我尝试在任何其他项目中应用它时, checkstyle
会在当前项目目录而不是插件目录中搜索它。 Is it possible to overcome this issue?有可能克服这个问题吗? I don't want users of that plugin to put any additional files inside their project.我不希望该插件的用户将任何其他文件放入他们的项目中。
I put the config files to resources
folder and tried to read the content.我将配置文件放入resources
文件夹并尝试读取内容。
public class MetricCodingRulesGradlePluginPlugin implements Plugin<Project> {
public void apply(Project project) {
project.getPluginManager().apply("checkstyle");
project.getExtensions().configure(CheckstyleExtension.class, checkstyleExtension -> {
URL url = getClass().getClassLoader().getResource("style/checkstyle.xml");
System.out.println("URL: " + url);
try {
checkstyleExtension.setConfigFile(
Paths.get(url.toURI())
.toFile()
);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
});
}
}
When I apply the plugin to another project, I get the following error:当我将插件应用到另一个项目时,出现以下错误:
URL: jar:file:/Users/user/.gradle/caches/jars-9/8f4176a8ae146bf601f1214b287eb805/my-plugin-0.0.1-SNAPSHOT.jar!/style/checkstyle.xml
Caused by: java.nio.file.FileSystemNotFoundException
at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
Java cannot read the file from the jar
archive for some reason. Java 由于某种原因无法从jar
存档中读取文件。 Any approaches to overcome this error?有什么方法可以克服这个错误?
You'd need to bundle the checkstyle.xml
within your plugin's resources
folder, so when you ship it, you can always access it from within the plugin code.您需要将checkstyle.xml
捆绑在插件的resources
文件夹中,这样当您发布它时,您始终可以从插件代码中访问它。
Basically, you need to put the config under src/main/resources/checkstyle.xml
of the plugin and then access it like this:基本上,您需要将配置放在插件的src/main/resources/checkstyle.xml
下,然后像这样访问它:
URL resourceURL = getClass().getClassLoader().getResource("checkstyle.xml");
if (resourceURL != null) {
File resourceFile = File(resourceURL.getFile());
checkstyleExtension.setConfigFile(resourceFile);
}
Also remember, if you ship your plugin as a .jar
, you'd need to unpack the checkstyle.xml
into a temp file beforehand.还要记住,如果您将插件作为.jar
,则需要事先将checkstyle.xml
解压到一个临时文件中。 Roughly:大致:
File temp = File.createTempFile(".checkstyle", ".xml")
try (FileOutputStream out = new FileOutputStream(temp)) {
try (InputStream resourceStream = getClass().getClassLoader().getResourceAsStream("checkstyle.xml")) {
byte[] buffer = new byte[1024];
int bytes = resourceStream.read(buffer);
while (bytes >= 0) {
out.write(buffer, 0, bytes);
bytes = resourceStream.read(buffer);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.