Dependency between publications and distTar

How do I make publications depend on distTar? The error I keep getting is:

Invalid publication 'mavenJava': artifact file does not exist: '/path/to/my/project-0.1.0-SNAPSHOT.tgz'

Here is an excerpt of my build.gradle (2.2.1) file:

distTar {
  compression = Compression.GZIP
}
distributions {
  main {
    baseName = project.name
    contents {
      into('bin') {
        from 'bin'
      }
      into('lib') {
        from 'lib'
      }
  }
}
publishing {
  publications {
    mavenJava(MavenPublication) {
      artifact(distTar.archivePath)
    }
  }
  repositories {
    maven {
      url "$buildDir/repository"
    }
  }
}

However, when I type gradle distTar, it does create the dist file aforementioned, and after that I can run gradle publish. How do I make the publish task depend on distTar? I tried adding publications (dependsOn:‘distributions’) but that did not work as I get:

Could not find method publishing() for arguments [{dependsOn=distributions}, ...

Thanks.

Assuming the task ‘distTar’ is of type ‘Tar’ then you can simply do ‘artifact distTar’ which creates an implicit dependency. In fact, you can pass any instance of ‘AbstractArchiveTask’ to the ‘artifact()’ method.

FWIW, if the task distTar wasn’t of type Tar, or if you wanted to be more explicit, another option is to set the “builtBy” for your artifact:

publishing {
  publications {
    mavenJava(MavenPublication) {
      artifact(distTar.archivePath) {
        builtBy: distTar
      }
    }
  }
}

I’m unclear whether this has exactly the same effect as Mark’s suggestion, or if the result is subtly different.

The end result of Mickey’s solution is the same. In that case you are simply explicitly stating the build dependency, rather than having Gradle infer it from the task.

In case someone (including me) comes looking for the final answer, this works:

apply plugin: 'distribution'
apply plugin: 'maven-publish'
  distTar {
  compression = Compression.GZIP
}
distributions {
  main {
    baseName = project.name
    contents {
      from 'poems'
    }
  }
}
  publishing {
  publications {
    mavenJava(MavenPublication) {
      artifactId 'foo'
      groupId 'bar'
      version '1.0'
      artifact(distTar)
    }
  }
  repositories {
    maven {
      url "$buildDir/repository"
    }
  }
}

Execute with: gradle publish