Publishing of source artifacts fails with exception for Maven publication to an Artifactory repository

I have a multi-project build consisting of JAR, WAR and other projects and, using Gradle 2.1, I want to publish artifacts created to our Artifactory repository, using Gradle’s new publication mechanism. This works all fine as long as I do not try to include source artifacts. As soon as I activate the line 'artifact sourcesJar ’ in the build script publish-maven.gradle listed below, an exception is thrown when starting ‘gradle artifactoryPublish’.

Any idea what causes the error?

Thanks Thorsten

// publish-maven.gradle script; some sensitive information has been removed
allprojects {
 apply plugin: 'maven-publish'
 apply plugin: 'com.jfrog.artifactory'
   artifactory {
  contextUrl = // ...
 }
}
  artifactoryPublish.skip = true
  subprojects {
 artifactory {
  publish {
   repository {
    //...
   }
   defaults {
    publications ('mavenJava')
   }
  }
    resolve {
   repository {
    // ...
   }
  }
 }
   publishing {
  publications {
   mavenJava(MavenPublication) {
    if (plugins.hasPlugin('java') && !(plugins.hasPlugin('war') || plugins.hasPlugin('ear'))) {
     from components.java
     artifact sourcesJar // ** This line throws the exception. **
    }
    else if (plugins.hasPlugin('war')) {
     from components.web
    }
   }
  }
 }
}
// excerpt of build.grade script
buildscript {
 repositories {
  maven {
   // ...
  }
 }
   dependencies {
  classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:3.0.1'
 }
}
  subprojects {
 afterEvaluate { subproject ->
  def manifestAttributes = [
   // ...
  ]
    if (plugins.hasPlugin('java') && !(plugins.hasPlugin('war') || plugins.hasPlugin('ear'))) {
   sourceCompatibility = subproject.javaSourceCompatibility
   targetCompatibility = subproject.javaTargetCompatibility
        dependencies {
    // ...
   }
     jar {
    manifest.mainAttributes(manifestAttributes)
   }
     task sourcesJar(type: Jar) {
    manifest.mainAttributes(manifestAttributes)
    classifier = 'sources'
    from sourceSets.main.allJava
   }
     artifacts {
    archives sourcesJar
   }
  }
 }
}
  apply from: "${rootProject.projectDir}/publish-maven.gradle"

Exception: Exception is: org.gradle.api.ProjectConfigurationException: A problem occurred configuring project ‘:admin’.

at org.gradle.configuration.project.LifecycleProjectEvaluator.addConfigurationFailure(LifecycleProjectEvaluator.java:79)

at org.gradle.configuration.project.LifecycleProjectEvaluator.evaluate(LifecycleProjectEvaluator.java:57)

at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:521)

at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:82)

at org.gradle.configuration.DefaultBuildConfigurer.configure(DefaultBuildConfigurer.java:31)

at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:129)

at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:106)

at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:86)

at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.java:80)

at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:33)

at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:24)

at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:36)

at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:26)

at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:51)

at org.gradle.internal.Actions$RunnableActionAdapter.execute(Actions.java:171)

at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:237)

at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:210)

at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:35)

at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:24)

at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:206)

at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169)

at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)

at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)

at org.gradle.launcher.Main.doAction(Main.java:33)

at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)

at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:54)

at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:35)

at org.gradle.launcher.GradleMain.main(GradleMain.java:23) Caused by: org.gradle.model.internal.core.ModelRuleExecutionException: Exception thrown while executing model rule: org.gradle.api.publish.plugins.PublishingPlugin$Rules#publishing(org.gradle.api.plugins.ExtensionContainer)

at org.gradle.model.internal.registry.DefaultModelRegistry.doCreate(DefaultModelRegistry.java:392)

at org.gradle.model.internal.registry.DefaultModelRegistry.createAndClose(DefaultModelRegistry.java:319)

at org.gradle.model.internal.registry.DefaultModelRegistry.get(DefaultModelRegistry.java:315)

at org.gradle.model.internal.registry.DefaultModelRegistry.element(DefaultModelRegistry.java:211)

at org.gradle.model.internal.registry.DefaultModelRegistry.toInput(DefaultModelRegistry.java:430)

at org.gradle.model.internal.registry.DefaultModelRegistry.toInputs(DefaultModelRegistry.java:422)

at org.gradle.model.internal.registry.DefaultModelRegistry.fireMutation(DefaultModelRegistry.java:405)

at org.gradle.model.internal.registry.DefaultModelRegistry.fireMutations(DefaultModelRegistry.java:340)

at org.gradle.model.internal.registry.DefaultModelRegistry.close(DefaultModelRegistry.java:326)

at org.gradle.model.internal.registry.DefaultModelRegistry.createAndClose(DefaultModelRegistry.java:320)

at org.gradle.model.internal.registry.DefaultModelRegistry.get(DefaultModelRegistry.java:315)

at org.gradle.model.internal.registry.DefaultModelRegistry.get(DefaultModelRegistry.java:205)

at org.gradle.configuration.project.TaskModelRealizingConfigurationAction.execute(TaskModelRealizingConfigurationAction.java:31)

at org.gradle.configuration.project.TaskModelRealizingConfigurationAction.execute(TaskModelRealizingConfigurationAction.java:27)

at org.gradle.configuration.project.ConfigureActionsProjectEvaluator.evaluate(ConfigureActionsProjectEvaluator.java:34)

at org.gradle.configuration.project.LifecycleProjectEvaluator.evaluate(LifecycleProjectEvaluator.java:55)

… 26 more Caused by: org.gradle.api.ProjectConfigurationException: A problem occurred configuring project ‘:base’.

at org.gradle.configuration.project.LifecycleProjectEvaluator.addConfigurationFailure(LifecycleProjectEvaluator.java:79)

at org.gradle.configuration.project.LifecycleProjectEvaluator.evaluate(LifecycleProjectEvaluator.java:57)

at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:521)

at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:82)

at org.gradle.api.publish.internal.ProjectDependencyPublicationResolver.resolve(ProjectDependencyPublicationResolver.java:35)

at org.gradle.api.publish.maven.internal.publication.DefaultMavenPublication.addProjectDependency(DefaultMavenPublication.java:111)

at org.gradle.api.publish.maven.internal.publication.DefaultMavenPublication.from(DefaultMavenPublication.java:102)

at publish_maven_9cpk8qgb59tcob1di6fj2pljn$_run_closure2_closure5_closure11_closure12.doCall(/Users/t/d/root/publish-maven.gradle:63)

at org.gradle.api.internal.ClosureBackedAction.execute(ClosureBackedAction.java:59)

at org.gradle.api.internal.AbstractPolymorphicDomainObjectContainer.create(AbstractPolymorphicDomainObjectContainer.java:65)

at org.gradle.api.internal.PolymorphicDomainObjectContainerConfigureDelegate._configure(PolymorphicDomainObjectContainerConfigureDelegate.java:46)

at org.gradle.api.internal.ConfigureDelegate.invokeMethod(ConfigureDelegate.java:73)

at publish_maven_9cpk8qgb59tcob1di6fj2pljn$_run_closure2_closure5_closure11.doCall(/Users/t/d/root/publish-maven.gradle:60)

at org.gradle.api.internal.ClosureBackedAction.execute(ClosureBackedAction.java:59)

at org.gradle.util.ConfigureUtil.configure(ConfigureUtil.java:130)

at org.gradle.util.ConfigureUtil.configure(ConfigureUtil.java:91)

at org.gradle.api.internal.AbstractNamedDomainObjectContainer.configure(AbstractNamedDomainObjectContainer.java:68)

at org.gradle.api.internal.AbstractNamedDomainObjectContainer.configure(AbstractNamedDomainObjectContainer.java:24)

at org.gradle.api.internal.ClosureBackedAction.execute(ClosureBackedAction.java:51)

at org.gradle.api.publish.internal.DefaultPublishingExtension.publications(DefaultPublishingExtension.java:48)

at org.gradle.api.publish.internal.DefaultPublishingExtension_Decorated.publications(Unknown Source)

at publish_maven_9cpk8qgb59tcob1di6fj2pljn$_run_closure2_closure5.doCall(/Users/t/d/root/publish-maven.gradle:58)

at org.gradle.api.internal.ClosureBackedAction.execute(ClosureBackedAction.java:59)

at org.gradle.listener.ActionBroadcast.execute(ActionBroadcast.java:39)

at org.gradle.api.internal.plugins.ExtensionsStorage$DeferredConfigurableExtensionHolder.configureNow(ExtensionsStorage.java:183)

at org.gradle.api.internal.plugins.ExtensionsStorage$DeferredConfigurableExtensionHolder.get(ExtensionsStorage.java:162)

at org.gradle.api.internal.plugins.ExtensionsStorage.getByType(ExtensionsStorage.java:77)

at org.gradle.api.internal.plugins.DefaultConvention.getByType(DefaultConvention.java:116)

at org.gradle.api.publish.plugins.PublishingPlugin$Rules.publishing(PublishingPlugin.java:87)

at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:63)

at org.gradle.model.internal.inspect.ModelRuleInspector$MethodModelCreator.invoke(ModelRuleInspector.java:271)

at org.gradle.model.internal.inspect.ModelRuleInspector$MethodModelCreator.create(ModelRuleInspector.java:253)

at org.gradle.model.internal.registry.DefaultModelRegistry.doCreate(DefaultModelRegistry.java:389)

… 41 more Caused by: org.gradle.model.internal.core.ModelRuleExecutionException: Exception thrown while executing model rule: org.gradle.api.publish.plugins.PublishingPlugin$Rules#publishing(org.gradle.api.plugins.ExtensionContainer)

at org.gradle.model.internal.registry.DefaultModelRegistry.doCreate(DefaultModelRegistry.java:392)

at org.gradle.model.internal.registry.DefaultModelRegistry.createAndClose(DefaultModelRegistry.java:319)

at org.gradle.model.internal.registry.DefaultModelRegistry.get(DefaultModelRegistry.java:315)

at org.gradle.model.internal.registry.DefaultModelRegistry.element(DefaultModelRegistry.java:211)

at org.gradle.model.internal.registry.DefaultModelRegistry.toInput(DefaultModelRegistry.java:430)

at org.gradle.model.internal.registry.DefaultModelRegistry.toInputs(DefaultModelRegistry.java:422)

at org.gradle.model.internal.registry.DefaultModelRegistry.fireMutation(DefaultModelRegistry.java:405)

at org.gradle.model.internal.registry.DefaultModelRegistry.fireMutations(DefaultModelRegistry.java:340)

at org.gradle.model.internal.registry.DefaultModelRegistry.close(DefaultModelRegistry.java:326)

at org.gradle.model.internal.registry.DefaultModelRegistry.createAndClose(DefaultModelRegistry.java:320)

at org.gradle.model.internal.registry.DefaultModelRegistry.get(DefaultModelRegistry.java:315)

at org.gradle.model.internal.registry.DefaultModelRegistry.get(DefaultModelRegistry.java:205)

at org.gradle.configuration.project.TaskModelRealizingConfigurationAction.execute(TaskModelRealizingConfigurationAction.java:31)

at org.gradle.configuration.project.TaskModelRealizingConfigurationAction.execute(TaskModelRealizingConfigurationAction.java:27)

at org.gradle.configuration.project.ConfigureActionsProjectEvaluator.evaluate(ConfigureActionsProjectEvaluator.java:34)

at org.gradle.configuration.project.LifecycleProjectEvaluator.evaluate(LifecycleProjectEvaluator.java:55)

… 72 more Caused by: groovy.lang.MissingPropertyException: Could not find property ‘sourcesJar’ on org.gradle.api.publish.maven.internal.publication.DefaultMavenPublication_Decorated@573aeab2.

at org.gradle.api.internal.AbstractDynamicObject.propertyMissingException(AbstractDynamicObject.java:43)

at org.gradle.api.internal.AbstractDynamicObject.getProperty(AbstractDynamicObject.java:35)

at org.gradle.api.internal.CompositeDynamicObject.getProperty(CompositeDynamicObject.java:94)

at org.gradle.api.publish.maven.internal.publication.DefaultMavenPublication_Decorated.getProperty(Unknown Source)

at publish_maven_9cpk8qgb59tcob1di6fj2pljn$_run_closure2_closure5_closure11_closure12.doCall(/Users/t/d/root/publish-maven.gradle:64)

at org.gradle.api.internal.ClosureBackedAction.execute(ClosureBackedAction.java:59)

at org.gradle.api.internal.AbstractPolymorphicDomainObjectContainer.create(AbstractPolymorphicDomainObjectContainer.java:65)

at org.gradle.api.internal.PolymorphicDomainObjectContainerConfigureDelegate._configure(PolymorphicDomainObjectContainerConfigureDelegate.java:46)

at org.gradle.api.internal.ConfigureDelegate.invokeMethod(ConfigureDelegate.java:73)

at publish_maven_9cpk8qgb59tcob1di6fj2pljn$_run_closure2_closure5_closure11.doCall(/Users/t/d/root/publish-maven.gradle:60)

at org.gradle.api.internal.ClosureBackedAction.execute(ClosureBackedAction.java:59)

at org.gradle.util.ConfigureUtil.configure(ConfigureUtil.java:130)

at org.gradle.util.ConfigureUtil.configure(ConfigureUtil.java:91)

at org.gradle.api.internal.AbstractNamedDomainObjectContainer.configure(AbstractNamedDomainObjectContainer.java:68)

at org.gradle.api.internal.AbstractNamedDomainObjectContainer.configure(AbstractNamedDomainObjectContainer.java:24)

at org.gradle.api.internal.ClosureBackedAction.execute(ClosureBackedAction.java:51)

at org.gradle.api.publish.internal.DefaultPublishingExtension.publications(DefaultPublishingExtension.java:48)

at org.gradle.api.publish.internal.DefaultPublishingExtension_Decorated.publications(Unknown Source)

at publish_maven_9cpk8qgb59tcob1di6fj2pljn$_run_closure2_closure5.doCall(/Users/t/d/root/publish-maven.gradle:58)

at org.gradle.api.internal.ClosureBackedAction.execute(ClosureBackedAction.java:59)

at org.gradle.listener.ActionBroadcast.execute(ActionBroadcast.java:39)

at org.gradle.api.internal.plugins.ExtensionsStorage$DeferredConfigurableExtensionHolder.configureNow(ExtensionsStorage.java:183)

at org.gradle.api.internal.plugins.ExtensionsStorage$DeferredConfigurableExtensionHolder.get(ExtensionsStorage.java:162)

at org.gradle.api.internal.plugins.ExtensionsStorage.getByType(ExtensionsStorage.java:77)

at org.gradle.api.internal.plugins.DefaultConvention.getByType(DefaultConvention.java:116)

at org.gradle.api.publish.plugins.PublishingPlugin$Rules.publishing(PublishingPlugin.java:87)

at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:63)

at org.gradle.model.internal.inspect.ModelRuleInspector$MethodModelCreator.invoke(ModelRuleInspector.java:271)

at org.gradle.model.internal.inspect.ModelRuleInspector$MethodModelCreator.create(ModelRuleInspector.java:253)

at org.gradle.model.internal.registry.DefaultModelRegistry.doCreate(DefaultModelRegistry.java:389)

… 87 more

I think the problem is that the ‘sourcesJar’ task is only added in ‘afterEvaluate’, but ‘publish-maven.gradle’ tries to access it before that.