Can not move to maven-publish because version is calculated after task graph ready

I spent some time trying to move from upload/artifacts to maven-publish but I got blocked by the early resolution of files in the maven-publish implementation. We rely on a dynamic version calculation taken right from the gradle docs at http://www.gradle.org/docs/current/userguide/tutorial_using_tasks.html, but it turns out that the artifacts method resolves files during script configuration time instead of delaying until execution time.

Since project.version is a mutable value and all other parts of gradle has worked so far, I suggest that this is actually a defect in maven-publish, which should instead save a reference to the task or archive task and retrieve the file to publish later.

Ideas, comments?

This is also an issue with the publication instance which grabs the version from the project before the execution phase.

Here’s a cludgy workaround that relies on internal classes and that seems to work but hasn’t really been tested much.

private void changeProjectVersion(Project project, String newVersion) {
        String oldVersion = project.version
        project.version = newVersion
        // Need to fix the version in all artifacts in publishing since it grabs it too early (during configuration)
        project.publishing.publications.each { MavenPublication publication ->
            if (publication.version == oldVersion) {
                def oldArtifacts = publication.artifacts.toList()
                def newArtifacts = oldArtifacts.collect { MavenArtifact artifact ->
                    String name = artifact.file.name
                    if (name.contains(oldVersion)) {
                        String newName = name.replace(oldVersion, newVersion)
                        File newFile = new File(artifact.file.parentFile, newName)
                        MavenArtifact newArtifact = new DefaultMavenArtifact(newFile, artifact.extension, artifact.classifier)
                        newArtifact.with {
                            builtBy(artifact.getBuildDependencies())
                        }
                        return newArtifact
                    } else {
                        // Nothing needed to be fixed
                        return artifact
                    }
                }
                publication.version = newVersion
                publication.setArtifacts(newArtifacts)
            }
        }
    }

The ability to specify builtBy in the configuration block for an artifact isn’t mentioned anywhere btw. It should be added to the documentation somewhere (especially important for file artifacts).

Is there any way to get a response to this problem?

The current implementation of the ‘maven-publish’ plugin has some known limitations, which is one reason why it’s still incubating. These limitations will be fixed once the new configuration model is ready. I’m not sure if you can do better in the meantime (without stepping back to the ‘maven’ plugin).