Passing arguments to a task

Ok, I figured out the problem. Apparently, it was this line that caused the mysterious task already exists problem.

from 'build.gradle' { into 'foobar' } 

Slightly revised code from yours causing the issue

apply plugin: 'java'
apply plugin: 'maven-publish'

group = 'org.gradle.foo'
version = '1.0.0-SNAPSHOT'

publishing {
    publications {
        module1(MavenPublication) {
            artifact myTask("foo")
        }
        module2(MavenPublication) {
            artifact myTask("bar")
        }
    }
}

def myTask(artifactName) {
    return tasks.create("my${artifactName}Task", Zip) {
        from 'build.gradle' { into 'foobar' } 
        baseName = artifactName
    }
}

If I change it to from 'build.gradle' into 'foobar', the problem went away. Thanks for all your help!

Link to equivalent stackoverflow question, for better quality : https://stackoverflow.com/questions/20410854/how-to-pass-parameters-or-arguments-into-a-gradle-task/48370451#48370451