I have a Gradle plugin which codifies a collection of checks to perform on the codebase. I would like the plugin itself to be subject to the same checks.
Attempt 1
I found part of a solution here, which essentially tries to use buildSrc to produce a plugin which can then be applied from the main project.
This gives me an error someone else had also seen:
No signature of method: GradlePlugins.buildscript() is applicable for argument types: (GradlePlugins$_run_closure1) values: [GradlePlugins$_run_closure1#2e7cad0f]
Attempt 2
This was adapted from a second suggestion in the thread:
apply plugin: new GroovyScriptEngine(
[ file('src/main/java').absolutePath,
file('src/main/groovy').absolutePath,
file('src/main/resources').absolutePath ].toArray(new String[0]),
this.class.classLoader)
.loadScriptByName('acme/plugins/CommonPlugin.groovy')
But it seems like it can't find classes in the same package as the plugin:
Caused by: groovy.lang.MissingPropertyException: No such property: CheckStringsExtension for class: acme.plugins.CommonPlugin
Attempt 3
There was a comment on the other gist suggesting that it might not work if the plugin script contains a package declaration, so I also tried making a trivial wrapper:
import acme.plugins.CommonPlugin
import org.gradle.api.Plugin
import org.gradle.api.Project
class SelfApplyBootstrap implements Plugin<Project> {
#Override
void apply(Project project) {
new CommonPlugin().apply(project);
}
}
And then:
apply plugin: new GroovyScriptEngine(
[ file('src/main/java').absolutePath,
file('src/main/groovy').absolutePath,
file('src/main/resources').absolutePath ].toArray(new String[0]),
this.class.classLoader)
.loadScriptByName('SelfApplyBootstrap.groovy')
But I still get the same error:
Caused by: groovy.lang.MissingPropertyException: No such property: CheckStringsExtension for class: acme.plugins.CommonPlugin
Related
I started getting a strange failure when compiling a gradle task class. This is the task I created:
package sample
import groovy.transform.CompileStatic
import groovy.transform.TypeChecked
import org.gradle.api.artifacts.Dependency
import org.gradle.api.provider.Property
import org.gradle.api.tasks.AbstractCopyTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.bundling.Zip
import sample.internal.DataSourceXmlConfig
#TypeChecked
#CompileStatic
class DataSource extends Zip {
#Internal
final Property<File> warFile = project.objects.property(File.class)
DataSource() {
warFile.convention(project.provider {
def files = project.configurations.getByName('warApp').fileCollection { Dependency d ->
d.name == (archiveFileName.getOrElse("") - (~/\.[^.]+$/))
}
files.empty ? null : files.first()
})
}
/**
* This function is used to specify the location of data-sources.xml
* and injects it into the archive
* #param dsConf The configuration object used to specify the location of the
* file as well as any extra variables which should be injected into the file
*/
#Input
void dataSourceXml(#DelegatesTo(DataSourceXmlConfig) Closure dsConf) {
filesToUpdate {
DataSourceXmlConfig ds = new DataSourceXmlConfig()
dsConf.delegate = ds
dsConf.resolveStrategy = Closure.DELEGATE_FIRST
dsConf.call()
exclude('**/WEB-INF/classes/data-sources.xml')
from(ds.source) {
if (ds.expansions) {
expand(ds.expansions)
}
into('WEB-INF/classes/')
rename { 'data-sources.xml' }
}
}
}
private def filesToUpdate(#DelegatesTo(AbstractCopyTask) Closure action) {
action.delegate = this
action.resolveStrategy = Closure.DELEGATE_FIRST
if (warFile.isPresent()) {
from(project.zipTree(warFile)) {
action.call()
}
}
}
}
When groovy compiles this class, I get the following error:
Execution failed for task ':buildSrc:compileGroovy'.
BUG! exception in phase 'class generation' in source unit '/tmp/bus-server/buildSrc/src/main/groovy/sample/DataSource.groovy'
At line 28 column 28 On receiver: archiveFileName.getOrElse() with
message: minus and arguments: .[^.]+$ This method should not have
been called. Please try to create a simple example reproducing this
error and file a bug report at
https://issues.apache.org/jira/browse/GROOVY
Gradle version: 5.6
Groovy version: localGroovy() = 2.5.4
tl;dr, is this a bug or am I missing something about how these annotations work?
The first thing I tried to do was to remove either one of #TypeChecked and #CompileStatic annotations to see if the error goes away.
This actually fixed the problem right away. Compiling the source with either annotations added was successful, but fails when both are present.
I read some questions and answers regarding the use of both annotations, but none of them seemed to suggest that one cannot use both at the same time.
Finally, I tried switching the order of the annotations to see if that helps and to my surprise, it worked! No compilation errors!
This works:
#CompileStatic
#TypeChecked
class DataSource extends Zip { ... }
At this point, I guess my question would be, is this a bug or is there something I am not understanding about the use of both of these annotations? I'm leaning more towards it being a bug just because of the fact that the order made the error message go away.
I'm developping a Maven plugin and some of my tests are done through maven-invoker-plugin based on Groovy scripts (https://maven.apache.org/plugin-developers/plugin-testing.html).
This Maven plugin generates a Java class and I'd like to test this generated class behaviour with Groovy scripts.
I'm facing a problem with these Groovy scripts saying that it could not resolve the class:
Running post-build script: <blah>\verify.groovy
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 21: unable to resolve class com.mycompany.MyClass
# line 21, column 1.
import com.mycompany.MyClass
Here is the generated Java class from my plugin:
package com.mycompany;
public final class MyClass {
public static String getSomething() {
return "something";
}
}
Here is an extract of my Groovy test verify.groovy:
File generatedJavaFile = new File( basedir, "target/my-plugin/com/mycompany/MyClass.java" );
assert generatedJavaFile.exists()
assert generatedJavaFile.isFile()
File generatedClassFile = new File( basedir, "target/classes/com/mycompany/MyClass.class" );
assert generatedClassFile.exists()
assert generatedClassFile.isFile()
import com.mycompany.MyClass
assert MyClass.getSomething() == "the expected result"
I was wondering if something needs to be specified to the maven-invoker-plugin configuration to include the tested Maven project, or if it's just not possible and I need to find another way...
Thanks,
BenoƮt.
I am trying to write a Gradle plugin for XML Beans. I have started with one of the 'Hello from Gradle' plugin examples, and also a plugin published by R. Artavia here. That plugin went straight to jar - I am trying to only generate source. The generated source must then be compiled with other project source and included in a single jar. Other goals include
- full plugin - all I should need is "apply plugin: 'xmlbean'"
- I can configure source/code gen location and some features if I want to
- It detects whether it needs to be rebuilt. (well, eventually!!!)
I am off to a pretty good start, but am blocked defining a new sourceSet. I am getting an error "No such property 'srcDirs'" (or 'srcDir'). It seems there is something I have to define someplace to make a new sourceSet work but I cannot find it. I have tried several different syntaxes (with/without equal sign, brackets, srcDir/srcDirs, etc. - nothing is working...
What do I need to do inside a plugin to make a new sourceSet entry be properly recognized?
Thank you!
JKE
File: xmlbean.gradle (includes greeting plugin for the moment for debugging)
apply plugin: xmlbean
apply plugin: 'java'
xmlbean {
message = 'Hi'
greeter = 'Gradle'
}
class xmlbean implements Plugin<Project> {
void apply(Project project) {
project.extensions.create("xmlbean", xmlbeanExtension)
Task xmlbeanTask = project.task('xmlbean')
xmlbeanTask << {
project.configurations {
xmlbeans
}
project.dependencies {
xmlbeans 'org.apache.xmlbeans:xmlbeans:2.5.0'
}
project.sourceSets {
main {
java {
srcDirs += '$project.buildDir/generated-source/xmlbeans'
}
}
xmlbeans {
srcDirs = ['src/main/xsd']
}
}
ant.taskdef(name: 'xmlbean',
classname: 'org.apache.xmlbeans.impl.tool.XMLBean',
classpath: project.configurations.xmlbeans.asPath)
ant.xmlbean(schema: project.sourceSets.xmlbean.srcDir,
srconly: true,
srcgendir: "$project.buildDir/generated-sources/xmlbeans",
classpath: project.configurations.xmlbeans.asPath)
println "${project.xmlbean.message} from ${project.xmlbean.greeter}"
}
project.compileJava.dependsOn(xmlbeanTask)
}
}
class xmlbeanExtension {
String message
String greeter
}
File: build.gradle
apply from: '../gradle/xmlbeans.gradle'
dependencies {
compile "xalan:xalan:$ver_xalan",
":viz-common:0.0.1",
":uform-repository:0.1.0"
}
Console: Error message:
:idk:xmlbean FAILED
FAILURE: Build failed with an exception.
* Where:
Script 'C:\jdev\cpc-maven\try.g2\comotion\gradle\xmlbeans.gradle' line: 32
* What went wrong:
Execution failed for task ':idk:xmlbean'.
> No such property: srcDirs for class: org.gradle.api.internal.tasks.DefaultSourceSet_Decorated
...
BUILD FAILED
Gradle info: version 2.5 / groovy 2.3.10 / JVM 7u55 on Windows 7 AMD64
You should try to become familiar with the Gradle DSL reference guide, because it's a huge help in situations like this. For example, if you click on the sourceSets { } link in the left navigation bar, you're taken to this section on source sets.
From there, you'll discover that the sourceSets {} block is backed by a class, SourceSetContainer. The next level of configuration nested inside is backed by a SourceSet object, and then within that you have one or more SourceDirectorySet configurations. When you follow the link to SourceDirectorySet, you'll see that there are getSrcDirs() and setSrcDirs() methods.
So how does this help? If you look closely at the exception, you'll see that Gradle is saying it can't find a srcDirs property on DefaultSourceSet_Decorated, which you can hopefully infer is an instance of SourceSet. That interface does not have an srcDirs property. That's because your xmlbeans {} block is configuring a SourceSet, not a SourceDirectorySet. You need to add another nested configuration to gain access to srcDirs.
At this point, I'm wondering whether a new source set is the appropriate solution. Unfortunately it's not clear to me exactly what the plugin should be doing, so I can't offer any alternatives at this point.
I made custom task, name MySqlTask.groovy
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
public class MySqlTask extends DefaultTask {
def hostname = 'localhost'
def sql
#TaskAction
def runQuery() {
//to do something...
}
}
And, I put this file in rootProject/buildSrc/src/main/groovy.
My build file is customtasksourcetree.gradle.
task createDatabase(type: MySqlTask) { sql = 'CREATE DATABASE IF NOT
EXISTS example' }
When I run gradle with customtasksrc.gradle then raise error like this.
FAILURE: Build failed with an exception.
* Where:
Build file '/Users/need4spd/Programming/Java/workspace/gradleTest/customtasksourcetree.gradle' line: 1
* What went wrong:
A problem occurred evaluating root project 'gradleTest'.
> Could not find property 'MySqlTask' on root project 'gradleTest'.
I have read that, my custom task file in buildSrc will compiled and add to classpath during build time automatically.
I can see compiled MySqlTask.class in buildSrc/build/classes.
What's the problem? Thanks.
I think that the problem is due to the fact that you haven't imported MySqlTask in your build script
I'm trying to write a plugin to stuff all dependencies (unzipped) into the same jar. This is what I've tried:
apply plugin: CustomZipPlugin
class CustomZipPlugin implements Plugin<Project>{
void apply(Project project) {
project.configurations.add('include');
project.tasks.add(
name:'customZipTask', type: Zip)
{
from {project.configurations.include.collect{zipTree(it)}}
};
}
}
repositories{mavenCentral()}
dependencies{
include 'net.sourceforge.cobertura:cobertura:1.9.4.1'
}
This leads to: Cannot determine the dependencies of task ':customZipTask'
I also tried:
..
from project.configurations.include.collect{zipTree(it)}
..
This led to a: You can't change a configuration which is not in unresolved state!
However ... writing a custom task directly in my build script (instead of a build script) works .. i.e.:
task customZipTask(type: Zip){
from {project.configurations.include.collect{zipTree(it)}}
}
Any suggestions on how to implement this zip code in a plugin instead ? (preferably a non-hacky solution)
Aha!! I needed to prepend zipTree with 'project' .. that solved the problem. The error messages listed above were a bit misleading.
..
from project.configurations.include.collect{project.zipTree(it)}
..
How about println'ing all configurations ??
How would I fix the following? ( get a can't change configuration which is not resolved exception )
..
project.configurations.include.each {println it}
..