Factory methods don't return newly created object

I have issues with some specific factory methods in gradle (checked in 1.11 and 2.1).

For this code snippet in a plugin, a will be null even though the code itself and when stepping through it seems to do the right thing. The artifact(object, closure) method has a return type of MavenArtifact, so everything should be cool.

Why is “a” null ?

// New "maven-publish" style
        project.publishing {
            publications {
                mavenJava(MavenPublication) {
                    MavenPublication pub -> pub.with {
                        from project.components.java
                          artifact javaDocTask
                        artifact sourcesTask
                        MavenArtifact a = artifact(dependencyReportAttributes.file) {
                            MavenArtifact art -> art.with {
                                classifier dependencyReportAttributes.classifier
                                builtBy dependencyReportAttributes.builtBy
                            }
                        }
                        assert a != null
                    }
                }
            }
        }

I can’t explain it. Can you try rewriting your code without using the ‘pub.with’ closure?