Setting up dependencies for publish tasks - gradle

I programmatically create maven-publish publications and would like those to depend on a custom task. I have tried the following:
apply plugin: 'java'
apply plugin: 'maven-publish'
publishing.publications.create("A", MavenPublication) { groupId = "G" }
task t
afterEvaluate { publishToMavenLocal.dependsOn(t) }
Doing a gradle publishToMavenLocal does result in t running before publishToMavenLocal but the generated task, publishAPublicationToMavenLocal, runs before t which is not what I want. I have tried setting up dependencies by iterating all tasks with names beginning with publish. This does not work either since publishAPublicationToMavenLocal is not available in the afterEvaluate phase.

Related

How can I have Teamcity Artifactory plugin invoke bootJar instead of Jar in Gradle?

We are using Gradle 4.8.1 to generate Spring Boot executable jars. This works fine locally. However, we are using Teamcity to publish our artifacts into Artifactory.
The issue is, to my understanding, that the "artifactoryPublish" task invokes the "jar" task in Gradle, which uploads artifacts from "Archives". So, irrespective of whether teamcity invokes the "assemble" task, or the "bootjar" task, or the "build" task, the artifactory plugin is taking the output of the "jar" task in the end and publishes that, whereas we'd like to have the output of the "bootjar" task (fat jar) in artifactory.
Is there any way I can force artifactoryPublish to run bootjar instead of jar ? Or for the jar task to create a fat jar as well ? Or should I consider another approach ?
Here's my build.gradle from one of the subprojects
plugins {
id "org.springframework.boot" version "2.0.4.RELEASE"
id "io.spring.dependency-management" version "1.0.6.RELEASE"
}
apply plugin: 'java'
repositories {
mavenCentral()
}
description = 'atlas-data-service'
// Dynamically insert TeamCity build number if available
if (hasProperty("teamcity")) {
version = teamcity["build.number"]
println "Release version with TeamCity build number passed into gradle is " + version
} else {
// Take the default appVersion defined in top level build.gradle when building outside of TeamCity
version = "$appVersion"
}
jar {
baseName = 'data-service'
enabled = true
}
bootJar {
mainClassName = 'c.m.f.a.dataservice.AtlasDataServiceApplication'
baseName = 'data-service'
enabled = true
classifier = 'boot'
}
dependencies {
...
}
This question is from last year, but updating in case someone else comes looking with the same issue.
I used the Maven-publish plugin to get the job done.
https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/#publishing-your-application-maven-publish
apply plugin: 'maven-publish'
publishing.publications {
bootJava(MavenPublication) {
artifact bootJar
}
}

Gradle multi project builds, must plugins be applied in children projects' build.gradle files also?

If I have a multi-project build and in the parent build.gradle file, I declare the statement apply plugin: 'java', and I also wish to apply this to all the children projects also, is that enough or do I also have to have this declaration in all the children build.gradle files?
No, it is not enough.
Calling apply will only apply the plugin (script or binary) to the specific project that calls the method. However, you will not need to create a build.gradle file for each subproject, since you can access them in your root build.gradle file:
// for a specific (sub-)project
project(':sub1') {
apply plugin: 'java'
}
// for all subprojects
subprojects {
apply plugin: 'java'
}
// for all projects
allprojects {
apply plugin: 'java'
}

How to apply plugin to allprojects with new Gradle plugins mechanism?

Before Gradle 2.1 I could apply plugin to all projects by using allProjects closure (by prevoisly resolving the jar, of course):
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.0.1"
}
}
allprojects {
apply plugin: "com.jfrog.artifactory"
}
With new publishing mechanism it looks like the plugins closure can't be used inside allprojects:
allprojects {
plugins {
id "com.jfrog.artifactory" version "3.0.1"
}
}
fails with:
"Could not find method plugins() for arguments [build_xxxx_run_closure1_closure4#yyyyy] on root project"
What are the rules of using plugins closure? Is the plugin applied to current project only? If so, how can I apply it to all projects without repeating the plugins closure inside each build?
The new plugins {...} syntax cannot be used within a allprojects {...} or subprojects {...} closure. Additionally, it can only be used within build scripts (no script plugins, init scripts, etc). If you want to avoid having to apply the plugin to each project individually I'd suggest using the old notation. This is an issue the Gradle team is aware of and a solution will be introduced in future versions.
Update: Starting with Gradle 3.0 you can do this in a slightly modified way. You still have to explicitly use apply() but you no longer have to deal with all the buildscript { } nonsense to get the plugin on your classpath. This also allows you to conditionally apply plugins. Check out the Gradle 3.0 release notes for more information.
plugins {
id 'my.special.plugin' version '1.0' apply false
}
allprojects {
apply plugin: 'java'
apply plugin: 'my.special.plugin'
}

How to simplify this Gradle command line?

I am new to Gradle and have gotten a fairly sophisticated build working with it so far (runs tests, uses CodeNarc, generates API docs, generates a sources JAR, etc.). The command line for a "full build" (that is, to execute all the major tasks that the CI server should be running), I need to enter the following Gradle command at the shell:
gradle clean build check groovydoc sourcesJar createPom dist -Pversion=<version>
Where <version> is the version I want to build.
I would like to condense this and add an alias so that all of the above can be accomplished with something simple, like:
gradle full-build
Is this possible? If so, how?
My build.gradle
apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: 'codenarc'
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.3'
testCompile 'junit:junit:4.11'
}
groovydoc
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives sourcesJar
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
task dist(type: Zip, dependsOn: build) {
classifier = 'buildreport'
from('build/test-results') {
include '*.xml'
into 'tests'
}
from('build/reports/codenarc') {
into 'reports'
}
from('build/docs') {
into 'api'
}
from(sourcesJar) {
into 'source'
}
from('build/libs') {
exclude '*-sources.jar'
into 'bin'
}
}
task createPom << {
pom {
project {
groupId "me"
artifactId "myapp"
version version
}
}.writeTo("build/libs/myapp-${version}.pom")
}
In the above build invocation, at least check and sourcesJar can be omitted already. (build depends on check, and from(sourcesJar) tells Gradle that dist depends on sourcesJar.) By adding further task dependencies (e.g. build.dependsOn dist), you can cut it down to gradle clean build -Pversion=.... dist should not depend on build but on the specific tasks that produce the artifacts that go into the zip (e.g. groovydoc and createPom).
Reducing gradle clean build to gradle fullBuild is more difficult, as Gradle doesn't currently have a first-class concept of "aliases", and adding build.dependsOn(clean) is usually not desirable. One way out is to implement your own aliasing mechanism by manipulating gradle.startParameter.taskNames.

Clean task not found in gradle

I have a gradle build file and am not able to execute a build. I am working with old code and the code should be built with 1.0-milestone-7 version since other projects use this version.
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'base'
apply plugin: 'application'
assert gradle.gradleVersion == '1.0-milestone-7'
mainClassName = "my.ClassTest"
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
}
test {
systemProperties = System.properties
}
c:\work\gradletest\> gradle clean
FAILURE: Could not determine which tasks to execute.
* What went wrong:
Task 'clean' not found in root project 'gradletest'.
* Try:
Run gradle tasks to get a list of available tasks.
Anything I forgot?
Changing the filename to build.gradle solves the issue. Note that this is the case in 1.0-milestone-7.

Resources