1
votes

I am writing a task to unzip the file/files from a particular location in one common folder like this

class UnzipTask extends DefaultTask{
@TaskAction
def unzip(){
def library = []
    def comp = []


        project.fileTree( dir: 'libs', include: '*.jar').visit { FileVisitDetails details ->
            if ( !details.file.name.contains('dev') ) {
                library<< details.file.name
            }
        }
        library.each { fileName ->
            comp << fileName.substring( 0, fileName.length()-4 )
        }
        project.copy{
        comp.each { dist ->
                def dName = dist.substring( 3, dist.lastIndexOf('-') )
                def zipFile = project.file("${libs/${dist}.zip")
                println("this is zipFIle"+zipFile)
                from( project.zipTree( zipFile ))

                {
                    into ( "${distributionName}")
                }
    }

   }

  }

Now when I execute the task, it gives me NullPointerException and no other detail. I have no idea what else is required.

This is what I get in the stackTrace:

Caused by: java.lang.NullPointerException at org.gradle.api.internal.file.IdentityFileResolver.doResolve(IdentityFileResolver.java:39) at org.gradle.api.internal.file.AbstractFileResolver.resolve(AbstractFileResolver.java:81) at org.gradle.api.internal.file.AbstractFileResolver.resolve(AbstractFileResolver.java:63) at org.gradle.api.internal.file.AbstractFileResolver.withBaseDir(AbstractFileResolver.java:59) at org.gradle.api.internal.file.DefaultFileLookup.getFileResolver(DefaultFileLookup.java:41) at org.gradle.api.internal.file.copy.FileCopier.getCopyVisitor(FileCopier.java:59) at org.gradle.api.internal.file.copy.FileCopier.copy(FileCopier.java:49) at org.gradle.api.internal.file.DefaultFileOperations.copy(DefaultFileOperations.java:134) at org.gradle.api.internal.project.AbstractProject.copy(AbstractProject.java:776) at org.gradle.api.internal.project.AbstractProject.copy(AbstractProject.java:772) at org.gradle.api.Project$copy$5.call(Unknown Source) at com.vitalconnect.gradle.tasks.UnzipDistributionTask.unzipDist(UnzipDistributionTask.groovy:25) at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:75) at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.doExecute(AnnotationProcessingTaskFactory.java:227) at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:220) at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:209) at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:585) at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:568) at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80) at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61) ... 60 more

2
Don't you have a stacktrace? This should include the line number. On a side note, consider using more concise Groovy-ish constructs like def comp = library.collect { f -> f.substring(0,f.length()-4) }majk
ok, Thanks, But right now, I have come to a point where if I remove into from the curly braces , the code works fine. But that way my folder name will be the name of the last value in the array. I want separate folder names for all the valuessver
from specifies ConfigurableFileCollection, while into is a property of the copy task/command (this means that you can't put into into from). If you need two different target folders, you need two different copy tasks, i.e. by swapping project.copy { and comp.each { dist-> majk
Is the unclosed brace in "${libs/${dist}.zip" a typo?majk
Thanks, that worked!!sver

2 Answers

0
votes

You didn't define variable d, and you have defined dName twice. Could it be the issue?

0
votes

Met the same problem, on Gradle 3.1. As the gradle crew explains here, in a forum thread:

The problem you were seeing is caused by the copy spec not having a base dir. The error message leaves a lot to be desired though. I've opened GRADLE-34064 for that.

[...]

Quite simply, every copy operation needs a call to into() w/o a closure parameter.

[...]

We explicitly validate this when using a Copy task but not when calling project.copy() for some reason.

Seems like they gave it a won't fix status.

In my case I needed to change:

copy {
  // wrong, the main spec lacks `into` call, as I do `from("x", {...})`
  from "src/META-INF/persistence.xml", { into "classes/META-INF" }
}

into:

copy {
  // now ok
  from "src/META-INF/persistence.xml"
  into "classes/META-INF"
}