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
Related
I'm currently trying to migrate my old ant system to gradle. I'm pretty new to gradle and still learning, so maybe this is something trivial I just overlooked.
I got a file, called delete.list which contains a list of files I want to delete.
This is my code so far:
task deleteLib(type:DeleteFiles) {
deleteList = file("${buildDir}/delete.list")
}
class DeleteFiles extends DefaultTask {
#SkipWhenEmpty
#InputFile
File deleteList
DeleteFiles()
{
description = 'Deletes Libs from Integris zip'
}
#TaskAction
void delete(){
def lines = deleteList.readLines()
lines.each {
delete fileTree(dir: "${project.buildDir}", include: "${it}")
}
}
}
delete.list:
lib/java/activation.jar
lib/java/pdfbox*.jar
lib/java/fontbox*.jar
lib/java/xmpbox*.jar
lib/java/jempbox*.jar
lib/java/iText*.jar
lib/java/itext*.jar
lib/java/jakarta-poi.jar
lib/java/commons-net*.jar
lib/java/jfreechart*.jar
lib/java/jcommon*.jar
lib/java/dom4j*.jar
lib/java/xmlbeans*.jar
lib/java/jaxen*.jar
lib/java/avalon-framework*.jar
lib/java/batik-all*.jar
After googling a bit I found this solution, as my _delete.list may not exist during building phase.
My current problem is that gradle seems to have a problem with the fileTree method:
* What went wrong:
Execution failed for task ':deleteLib'.
> Could not find method fileTree() for arguments [{dir=C:\entwicklung\Testumgebung\testProjectGradle\build, include=lib/java/activation.jar}] on task ':deleteLib' of type DeleteFiles.
Have somebody an idea what I'm missing here?
Since both methods are defined on Project and groovy looks for the method defined in task you need to explicitly invoke the methods on project instance:
project.with {
delete fileTree(dir: "${project.buildDir}", include: "${it}")
}
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.
Inside gradle copy filter task in my build.gradle file, I am trying to read label value from gradle.properties file or from a variable. Please refer below piece of code:
def label = "2.2"
task filterJS(type: Copy) {
from 'src/main/webapp'
into 'build/webapp'
filter(ReplaceTokens, tokens: [vlabel: $label])
}
In HTML file, I have #vlabel#
On running gradle filterJS, getting below exception
What went wrong:
A problem occurred evaluating project ':CargoSystemUX'.
Could not find property $label on task :CargoSystemUX:filterJS.
I am not able to replace $label with its value at run time. Please suggest me the solution for this.
It should be:
import org.apache.tools.ant.filters.ReplaceTokens
def label = "2.2"
task filterJS(type: Copy) {
from 'webapp'
into 'filtered'
filter(ReplaceTokens, tokens: [vlabel: label])
}
Without $ when referring to label and also note the import statement.
I am facing an issue executing my task of type Copy,:
Skipping the task as it has no source files
I get if I run in the debug mode.
My Plugin.groovy class (where the call to the plugin task in made )
Task task = project.tasks.create("makeJarPlugin", MakeJarPluginTask.class)
task.dependsOn("clearDistPlugin", "build")
My MakeJarPluginTask.grrovy
class MakeJarPluginTask extends Copy {
#TaskAction
def makeJar(){
logger.lifecycle("creating a jar *********************")
delete('dist/')
from('build/intermediates/bundles/release')
into('dist/')
include('classes.jar')
def jarName = new VersionName().getNameWithVersion() + '.jar'
rename('classes.jar', jarName)
}
}
Now, I execute this task in my android studio project using
gradlew makeJarPlugin --info
It gives me the output:
Skipping task ':network:makeJar1' as it has no source files.
makeJar1 UP-TO-DATE
There is something wrong with the type Copy as in the same way I execute my delete task and it executes. Any pointers!
It seems that this answer may be helpful.
Hint: you need to configure the task. Otherwise it won't be executed since the whole configuration is done in makeJar which is too late since this is an action.
Instead of using copy you can also try:
class MakeJarPluginTask extends DefaultTask {
#TaskAction
def makeJar() {
logger.lifecycle("creating a jar *********************")
delete('dist/')
project.copy {
from('build/intermediates/bundles/release')
into('dist/')
include('classes.jar')
def jarName = new VersionName().getNameWithVersion() + '.jar'
rename('classes.jar', jarName)
}
}
}
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}
..