Retrive GAV for multi-project gradle build using buildscript

I am using below code in build.gradle file inside buildscript block.
As you can see by looking at code its retrieving GAV and URL of component and storing them to a file and its working fine.
The only problem with me is as it is in buildscript so it get executed once at startup and one more time again during task call (I am not much clear on exact calling position in code) and because of this my multi-project gradle build which is making six nexus upload but in my file(which got generated by appending component details) I am seeing records for 12 component (Every component detail is present twice).

So how I can avoid this problem?

afterEvaluate{
def file = new File(“componentGAV.txt”)
file.createNewFile()
tasks.withType(PublishToMavenRepository) { task ->
println “I am here…”
println task.publication.getProperty(‘groupId’)
println task.publication.getProperty(‘artifactId’)
println task.publication.getProperty(‘version’)
println task.repository.getProperty(‘url’)
file.append(task.publication.getProperty(‘groupId’)+":"+task.publication.getProperty(‘artifactId’)+":"+task.publication.getProperty(‘version’)+":"+task.repository.getProperty(‘url’)+"\n")
println file.text
}
}