How do I create javadoc jars for publishing to nexus only when a certain task is called?

I have a build where I defined javadoc jars to be created for our normal source and our test sources. I want to make sure they get published to our Nexus repository, so I added them to our publishing section like this:

publishing {
        publications {
			mavenJava(MavenPublication) {
				from components.java
			}
			mavenJavadoc(MavenPublication) {
				artifact(javadocJar) {
					classifier "javadoc"
				}
			}
			mavenTestJavadoc(MavenPublication) {
				artifact(testJavadocJar) {
					classifier "testjavadoc"
				}
			}
                }
	}
}

This works nicely, but one more thing I would like to accomplish is to have these javadoc jars only be created and published if a certain task is run, instead of always when a publish task is called. The reason I would like this is for during normal development, I don’t really care if javadoc jars are created when I do a publishToMavenLocal. But when we go to do a release, I’d like to be able to call another task to make sure they get published, like:

./gradlew test generateJavadoc publish

So I guess what I am looking for is to be able to conditionally add my javadoc jars to the publish task dependency.

Hey,

At the moment there is no good way of declaring this type of exclusions. When invoking the “publish” task, there shouldn’t be publishMavenLocal tasks triggered. have you manually defined a mavenLocal repository in your publication section? If you use “publishToMavenLocal” explicitly to install your publications to ~/.m2 you probably can just check for this tasks in the start parameters:

    publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
        if(!gradle.startParameter.taskNames.contains("publishToMavenLocal")){
            mavenJavadoc(MavenPublication) {
                artifact(javadocJar) {
                    classifier "javadoc"
                }
            }
        }
     }
}

Thank you René for the response, it should work perfectly. I took your response and altered it slightly to only add it if I am specifically publishing to Nexus.

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
        if( gradle.startParameter.taskNames.contains("publish")){
            mavenJavadoc(MavenPublication) {
                artifact(javadocJar) {
                    classifier "javadoc"
                }
            }
        }
     }
}