Generating and publishing parent POM

I’ve read before that Gradle doesn’t support generating a parent pom, and likewise doesn’t support publishing one. The biggest problem I see is that AbstractMavenResolver.addPomAndArtifact sets DeployTask.file to the “mainArtifact” and I need that mainArtifact to be the actual pom. Or its that the DefaultArtifactPomContainer uses the “mainArtifact” as the second argument in creating the DefaultMavenDeployer. I couldn’t figure out how to override the mainArtifact cleanly, so I took two approaches. One is to implement PomArtifactContainer, e.g.:

def parentPomContainer = new ArtifactPomContainer() {
          void addArtifact(org.apache.ivy.core.module.descriptor.Artifact artifact, File src) {
            throw new InvalidUserDataException("Parent POMs can not have artifacts")
        }
          Set<MavenDeployment> createDeployableFilesInfos() {
            ArtifactPom activeArtifactPom = new DefaultArtifactPom(parentPom);
            File pomFile = file("build/poms/pom-parent.xml");
            PublishArtifact pomArtifact = activeArtifactPom.writePom(pomFile);
              Set<MavenDeployment> mavenDeployments = new HashSet<MavenDeployment>();
            mavenDeployments.add(new DefaultMavenDeployment(pomArtifact, pomArtifact, new HashSet<PublishArtifact>()));
            return mavenDeployments;
        }
    }

Then set in the mavenDeployer:

uploadArchive.repositories.mavenDeployer {
    artifactPomContainer = parentPomContainer
}

This works, but I have I have to define the pom outside of the mavenDeployer so that it’s accessible to the parentPomContainer as parentPom and there’s code here that isn’t exactly relevant to what I’m trying to do. The second approach was to just force mainArtifact to be pom output, before it exists (since the pom doesn’t get generated till later). I did this by declaring a configuration with one artifact that will exist:

configurations {
        pom
    }
    task writeDefaultPom {
        parentPomFile = file("${buildDir}/poms/pom-default.xml")
        doLast {
            parentPomFile.getParentFile().mkdirs()
            parentPomFile.createNewFile()
        }
    }
      artifacts {
        pom(writeDefaultPom.parentPomFile) {
            name 'default'
            type 'pom'
            builtBy writeDefaultPom
        }
    }
    uploadPom {
        configuration = configurations.pom
        repositories.mavenDeployer {
              ... typical
repository and pom stuff ....
        }
    }

Any other variant produced weird errors. If I put pom outside the mavenDeploy it fails with a [ant:null] exception with no stack. If the file doesn’t exist, I I get file not found. It’s not ideal, but it works and could be helpful to someone else.