[maven-publish plugin] Automatically uploading artifacts when jar is not up-to-date

I would like to upload my jar to local repository automatically (i.e. just executing jar task) if and only if it is modified.

But, with following build.gradle,

apply plugin: 'maven-publish'
  publishing {
  repositories { mavenLocal() }
  publications {
    artifact(MavenPublication) {
      from components.java
    }
  }
}
jar.finalizedBy publish
publish.onlyIf { jar.didWork }

gradle jar

generated output like

:compileJava UP-TO-DATE

:processResources UP-TO-DATE

:classes UP-TO-DATE

:jar UP-TO-DATE

:generatePomFileForArtifactPublication

:publishArtifactPublicationToMavenLocalRepositoryUploading: xxxx.jar to repository remote at file:/C:/Users/xxxx/.m2/repository/

Transferring 136K from remote

Uploaded 136K

:publish SKIPPED

which, I didn’t expected.

How this can be achieved in easy and readable way?

‘publish’ is a lifecycle task. It only exists as a convenient entry point and it depends on the real tasks.

You need to add the onlyIf to the real publish task(s), i.e. ‘publishArtifactPublicationToMavenLocalRepository’.

Thank you for explanation, but this did not work. 'publishArtifactPublicationToMavenLocalRepository.onlyIf { jar.didWork} ’ fails saying > Task with name ‘publishArtifactPublicationToMavenLocalRepository’ not found in project ‘:my-project’

‘publishArtifactPublicationToMavenLocal.onlyIf { jar.didWork }’ and ‘publishArtifactPublicationToMavenLocalRepositoryUploading.onlyIf { jar.didWork }’

also failed to configure.

‘publishToMavenLocal.onlyIf { jar.didWork }’ does not give me any error, but still upload non-modified jar to local repository.

Try putting the reference to the publishing task inside a publishing block.

publishing {
publishArtifactPublicationToMavenLocalRepository.onlyIf { jar.didWork }
}

I know this isn’t great: this part of the configuration model is something we’re actively working on improving.

This works fine. Thank you very much.