Issue in using gradle in grails app


(smurfs.doll) #1

Hi

I am working on a grails project that uses gradle as the build tool.

I tried gradle build -PgrailsArgs=–binary,

it builds a jar and places it in the projects build/lib directory.

But, there is one more jar in the projects target directory, which is produced by grails.

So, i added a grailsTask in the build.gradle, to delete the jar from build/lib and copy the jar from projects target dir to build/lib.

task del(type: Delete) { delete 'build/libs' }
 task rename(type: Copy){
  from('target/')
  rename {String fileName ->
     if (fileName.endsWith('jar')) {
    String original = fileName
    String originalWithoutExtension = project.name + '_' + 2.0
    originalWithoutExtension + '.jar'
   }
  }
  into('build/libs/')
 }

Everything works fine till here.

But, when i do a gradle upload. it again builds a jar and replaces that jar that i created. :frowning:

Can you please help me on this …


(Peter Niederwieser) #2

There is no such thing as nesting tasks. (Where did you get this from?) All you need is the ‘rename’ task. Just change the type to ‘Sync’.


(smurfs.doll) #3

Thanks Peter …I have not used nested tasks. I have created two tasks: one to delete and another to copy jar from projects target directory to projects build/lib directory. It worked.

But, how to do the same thing in a gradle custom plugin? I have written the same functionality as a custom gradle plugin:

project.task('buildProject', description: 'Verify project, release, and update version to next.', group: BUILD_GROUP, type: GradleBuild) {
   startParameter = project.getGradle().startParameter.newInstance()
     tasks = [
    'build',
    //
0. (This Plugin) Initializes the corresponding SCM plugin (Git/Bazaar/Svn/Mercurial).
    'buildDir'
    //
1. (SCM Plugin) Check to see if source needs to be checked in.
    //'rename',
   ]
  }
    project.task('buildDir', group: BUILD_GROUP,
  description: 'buildDir') << this.&buildDir
void buildDir(){
           def directory = new File(subProject.getName(), 'build/libs')
   directory.deleteDir()
   def source = null
   try{
    source = new File(subProject.getName(), "target").list(new FileFilter()).getAt(0)
   }catch(Exception e) {
    throw new GradleException("Build Plugin, build is not successful")
   }
   def a = new File(subProject.getName()+ "/target",source.toString())
   def destination = new File(subProject.getName() + "/build/libs")
   println destination.absolutePath
   destination.mkdir()
   a.renameTo(new File(destination, subProject.getName() + "-" + subProject.getVersion() + ".jar"));
  }
   }

Can you please let me know, how to handle this?


(Peter Niederwieser) #4

I have not used nested tasks.

You declared a task inside another task, and that’s not supported.

Can you please let me know, how to handle this?

No idea what you are trying to do here. There is no difference to doing this in a plugin, except that the task syntax is slightly different.


(smurfs.doll) #5

I am trying to do the below thing in the custom gradle plugin. I have tried in the build.gradle and it worked fine by changing the type to Sync. I wanted to do the same in a custom gradle plugin. Can you please let me know how to handle this in cutom gradle plugin?

task rename(type: Sync){

from(‘target/’)

rename {String fileName ->

if (fileName.endsWith(‘jar’)) {

String original = fileName

String originalWithoutExtension = project.name + ‘_’ + 2.0

originalWithoutExtension + ‘.jar’

}

}

into(‘build/libs/’)

}


(Peter Niederwieser) #6

Your own code above already shows a way to declare tasks in a custom plugin …


(smurfs.doll) #7

I am new to writing custom gradle plugin. I tried with the java.io.File, it did not work. Can you please let me know, how to do that using Sync type …


(Peter Niederwieser) #8

Just do it like you did it for the ‘buildProject’ task. If you are having troubles with this, it’s worthwhile to step back and study the source code of some other plugins before writing your own.


(smurfs.doll) #9

Sure. i will try http://www.gradle.org/docs/current/userguide/custom_plugins.html Is there any command to set the type as sync in custom gradle plguin??


(Peter Niederwieser) #10

Again, just look at your declaration of the ‘buildProject’ task above, which also sets a type.


(smurfs.doll) #11

Hi Peter

I tried the below code, but it is not executing the meethod, if i add as type:sync.If i remove that, it works. import org.gradle.api.tasks.Sync

project.task('rename', group: BUILD_GROUP,
  description: 'Initializes the SCM plugin (based on hidden directories in your project

 any idea?s directory)',type: Sync) <<this.&deleteBuildDir

any idea?


(Peter Niederwieser) #12

Please post the complete code (don’t stop in the middle of a line) and wrap it in HTML code tags.


(smurfs.doll) #13

Hi Peter

I have created two tasks : build and overrideProjectDir

project.task('buildProject', description: 'Verify project, release, and update version to next.', group: BUILD_GROUP, type: GradleBuild) {
   startParameter = project.getGradle().startParameter.newInstance()
     tasks = [
    'build',
    'overrideProjectDir'
         ]
  }
project.task('overrideProjectDir', group: BUILD_GROUP,type:Sync,dependsOn: 'build',
  description: 'Initializes the SCM plugin (based on hidden directories in your project
void deleteBuildDir(){
from('target/')
  rename {String fileName ->
     if (fileName.endsWith('jar')) {
    String original = fileName
    String originalWithoutExtension = project.name + '_' + 2.0
    originalWithoutExtension + '.jar'
   }
  }
  into('build/libs/')
}

The above method is executed before it executes the ‘build’ task. I have added dependsOn . This happens only if i set the type as Sync. I use the command:

gradle buildProject

Can you please let me know, if i miss anything?

Thanks Smurfs


(Peter Niederwieser) #14

I have no clue what you are aiming for here. I’m afraid I can’t help any further.


(smurfs.doll) #15

I have grouped two tasks and then trying to execute one by one similar to the gradle release plugin https://github.com/townsfolk/gradle-release. But when i set to sync, it executes first before doing the build task.