gradle.buildStarted not triggered - groovy

I am not able to have buildStarted triggered in my gradle build, not sure what I am doing wrong.
I have a root project gradle file like this
version '1.0'
buildscript {
repositories {
maven { url 'http://repo.jfrog.org/artifactory/gradle-plugins' }
}
dependencies {
classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '2.1.0')
}
}
gradle.buildStarted {
println "buildStart"
}
gradle. buildFinished {
println "buildFinished"
}
apply plugin: 'groovy'
apply plugin: 'maven'
.........
buildFinished works as I am able to see corresponding println but buildStarted never seem to get triggered.
EDIT
Include init.gradle that uses build listener
class MyBuildAdapter extends BuildAdapter {
void buildStarted(Gradle gradle) {
println "buildStarted"
}
}
gradle.addBuildListener new MyBuildAdapter()

The problem is that the buildStarted event occurs before you get a chance to register a callback in a build script. You'll have to use one of the other hooks. For details, see Gradle.addBuildListener in the Javadoc.

I had the same issue in a custom plugin I wrote, and found that only projectsEvaluated callback is the one that gets executed before buildFinished in the BuildListener.
It's been 7 years, and finally Gradle got around to admitting that it didn't make sense to have this method in the first place. It'll be removed in Gradle 7.
https://docs.gradle.org/6.5.1/userguide/upgrading_version_5.html#apis_buildlistener_buildstarted_and_gradle_buildstarted_have_been_deprecated

Related

How can a custom gradle task be ran from the body of a build

I have the following custom plugin:
class GenPlugin implements Plugin<Project> {
void apply(Project project) {
project.task("gen", type:GenTask)
}
}
That adds the following task to the project:
class GenTask extends DefaultTask {
#TaskAction
def gen() {
println "hello"
}
}
The groovy code is packaged and deployed to a local maven repository, I then reference the dependency in a build with:
apply plugin: 'gen'
I can run the task successfully by using gradle gen, how can I run this task in my build.gradle without specifying it as an argument?
You can use defaultTasks, have a look at the docs.
Running the task from gradle itself is highly discouraged. You can do it with:
project.tasks.gen.execute()
however I don't recommend you to go that way. Instead you can define a dependency on the task you run from command line. If this task is called e.g. afterGen, then use:
afterGen.dependsOn gen

Create a Groovy executable jar with Spock test set as to be executed

I want to create jar with two groovy files, AppLogic.groovy which consists of two few groovy classes and another file, AppSpec that has Spock test suite and I would like to have this Spock class executed (set as executable). How can I create such jar with all dependencies? I found sth similar for jUnit here: how to export (JUnit) test suite as executable jar but could not adapt it for my needs.
I use gradle for build, here is my build.gradle file:
group 'someGroup'
version '1.0'
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin:'application'
sourceCompatibility = 1.7
repositories {
//some repos here
maven { url "http://repo.maven.apache.org/maven2" }
}
dependencies {
//some dependencies here
}
I was browsing around and found SpockRuntime, but I do not know if and how I can use it to achive my goal.
And the winner is:
static void main(String[] args) {
EmbeddedSpecRunner embeddedSpecRunner = new EmbeddedSpecRunner()
embeddedSpecRunner.runClass(MySpec)
}
I do not advise using the EmbeddedSpecRunner from spock implementation as described in accepted answer.
This is what I found to work reliably with gradle 4.9. The basic approach is to use:
The gradle application plugin to create a single tarfile with all testRuntimeClasspath dependencies and shell scripts to run the spock tests
The gradle maven-publish plugin to publish the tar file as an artifact to your maven repo (in my case nexus)
The build.gradle file looks like this:
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'maven-publish'
apply plugin: 'application'
mainClassName = 'org.junit.runner.JUnitCore' // The junit 4 test runner class
applicationName = 'run-tests-cli' // Feel free to change
repositories {
...
}
dependencies {
...
testImplementation "org.codehaus.groovy:groovy-all:${groovyVersion}"
testImplementation "org.spockframework:spock-core:${spockVersion}"
}
// Package compiled spock / junit tests to <artifact>-test-<version>.jar
task testJar(type: Jar) {
classifier = 'tests'
from sourceSets.test.output.classesDirs
}
// Copy all testRuntimeClasspath dependencies to libs folder
task copyToLibs(type: Copy) {
from configurations.testRuntimeClasspath
into "$buildDir/libs"
}
// Make sure test jar is copied
copyToLibs.dependsOn('testJar')
// Make sure platform-specific shell scripts are created after copyToLibs
startScripts.dependsOn(copyToLibs)
// Configure what goes into the tar / zip distribution file created by gradle distribution plugin assembleDist task
distributions {
main {
contents {
// Include test jar
from(testJar) {
into "lib"
}
// Include all dependencies from testRuntimeClasspath
from(copyToLibs) {
into "lib"
}
}
}
}
startScripts {
// Ensure ethat all testRuntimeClasspath dependencies are in classpath used by shell scripts
classpath = project.tasks['testJar'].outputs.files + project.configurations.testRuntimeClasspath
}
publishing {
repositories {
maven {
def releasesRepoUrl = "https://nexus.yourcompany.com/repository/maven-releases/"
def snapshotsRepoUrl = "https://nexus.yourcompany.com/repository/maven-snapshots/"
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
credentials {
username = rootProject.getProperty('NEXUS_USERNAME')
password = rootProject.getProperty('NEXUS_PASSWORD')
}
}
}
publications {
maven(MavenPublication) {
groupId = 'com.yourgroupId'
version = "${rootProject.getVersion()}"
}
TestJar(MavenPublication) {
artifact(testJar)
}
RunTestsCliTar(MavenPublication) {
artifact(distTar)
artifactId "${applicationName}"
}
}
}
Now you can do the following:
To build the project (including the tar file) without running test task: gradle -x test clean build
To publish artifacts produced by project (including tar file to maven repo - in my case nexus): gradlew -x test publish. Note you will need to provide credentials to upload artifacts to repo. It is good practice to define them (NEXUS_USERNAME, NEXUS_PASSWORD in my example) in ~/.gradle/gradle.properties or specify them via -P options on the gradle command line.

gradle configurations for tasks

I have gradle build script with plugins: groovy, jacoco and maven.
In dependencies beside jars is:
testCompile group: 'org.spockframework', name: 'spock-core', version: '0.7-groovy-2.0'
Now when I created task integTest(Type: Test) and :
configurations{
integTestCompile {
extendsFrom testCompile
}
integTestRuntime {
extendsFrom integTestCompile
}
}
everything works OK, but I wanted to add some modularization and now all integTest tasks are created that way:
task "${module}_${suite}" (type: Test)
and when I tried implement same changes in configurations I got an error:
Could not find method ModuleName_Suite1Runtime() for arguments [build_55s7pmm2c6k8n8e2n1i59t3b5b$_run_closure5_closure28_closure29_closure31#3394214b] on root project 'projectName'.
for
configurations {
"${module}_${suite}Compile"{
extendsFrom testCompile
}
"${module}_${suite}Runtime"{
extendsFrom "${module}_${suite}Compile"
}
}
and another error with different configuration:
No signature of method: java.lang.String.extendsFrom() is applicable for argument types: (org.gradle.api.internal.artifacts.configurations.DefaultConfiguration_Decorated) values: [configuration ':testCompile']
for
configurations{
"${module}_${suite}Compile".extendsFrom(testCompile)
"${module}_${suite}Runtime".extendsFrom("${module}_${suite}Compile")
}
Without "taskName+Compile" and "taskName+Runtime" I got ClassNotFound Exception for spock specification. So I'm sure I need this like in previous version.
I'm pretty sure it's something straightforward, but I can't find any tip in google.
Any help would be appreciated.
The syntax being used inside the configurations {...} block is implemented via some Groovy magic that unfortunately is not called when you use a String literal. Instead, if you want to create a configuration with a dynamic name you'll want to call the create() method on the ConfigurationsContainer.
configurations.create("${module}_${suite}Compile") {
extendsFrom configurations.testCompile
}

Fixing Gradle Artifactory plugin publishing issue

I have a multi-project build that I am building with Gradle:
myapp/
myapp-client/
myapp-shared/
myapp-server/
build.gradle
settings.gradle
Where settings.gradle looks like:
include ':myapp-shared'
include ':myapp-client'
include ':myapp-server'
I have successfully got Gradle to compile my Groovy source code, run unit tests, generate GroovyDocs, and package both binary and source JARs for all 3 subprojects. The build invocation for which is: gradle clean build groovydoc sourcesJar -Pversion=<whatever version I specify>.
I am now attempting to add the Gradle-Artifactory plugin such that:
All 3 subprojects get POMs generated for them; and
All 3 subproject binary JARs, POMs and source JARs get published to my locally-running Artifactory; and
The artifactoryPublish task executes whenever gradle build is invoked
Here's my best attempt (my complete build.gradle):
allprojects {
buildscript {
repositories {
maven {
url 'http://localhost:8081/artifactory/plugins-release'
credentials {
username = "admin"
password = "password"
}
name = "maven-main-cache"
}
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.0.1"
}
}
apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: "com.jfrog.artifactory"
version="0.0.1"
group = "mygroup"
repositories {
mavenCentral()
add buildscript.repositories.getByName("maven-main-cache")
maven {
url "http://localhost:8081/artifactory/mydev-snapshots"
}
}
artifactory {
contextUrl = "http://localhost:8081/artifactory"
publish {
repository {
repoKey = 'mydev-snapshots'
username = "admin"
password = "password"
maven = true
}
defaults {
publications ('mavenJava')
}
}
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
}
rootProject {
artifactoryPublish.skip=true
}
subprojects {
apply plugin: 'groovy'
apply plugin: 'eclipse'
sourceCompatibility = '1.7'
targetCompatibility = '1.7'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
repositories {
mavenLocal()
mavenCentral()
maven {
url "https://repository.apache.org/content/repositories/snapshots"
}
maven {
url "http://localhost:8081/artifactory/mydev-snapshots"
}
maven {
url "https://code.google.com/p/guava-libraries/"
}
}
dependencies {
compile (
'org.codehaus.groovy:groovy-all:2.3.7'
)
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
build(dependsOn: 'artifactoryPublish')
}
When I run gradle clean build groovydoc sourcesJar -Pversion=0.1.1, I get the following command line exception:
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\Users\myuser\sandbox\eclipse\workspace\myapp\build.gradle' line: 14
* What went wrong:
A problem occurred evaluating root project 'myapp'.
> You can't change a configuration which is not in unresolved state!
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 2.589 secs
My question: what is going on here, and what do I need to do (specifically) to fix it and get the Artifactory plugin publishing?
Bonus question: I'm specifying version number in 2 places (the build invocation as well as inside the build.gradle file. I want to specify version number only via the build invocation. How do I configure artifactoryPublish (or rather, the Gradle-Artifactory plugin) to accept the version I specify from the command-line?
Number of issues here:
buildscript should be top-level block, not inside allprojects
When using Artifactory, you don't need to specify any other repositories except of Artifactory (don't need mavenCentral())
If you want to use artifactoryPublish you need to configure the Artifactory plugin. Here are the docs and here are two fully working examples of multi-module Gradle projects: 1 and 2. Some highlights:
You need to apply maven or maven-publish plugin.
You need to add the produced artifacts to configuration or publication accordingly.
You need to configure the plugin with Artifactory instance you are working with, provide resolution and deployment repository names, credentials (usually for deployment only) and specify which configuration or publication you want to publish.

How to use uploadConfigurationName and buildConfigurationName

In gradle documentation we can read:
For each configuration in your project, Gradle provides the tasks uploadConfigurationName and buildConfigurationName [18].
As I understand I can create build which looks like this (without any plugin because I don't want to use plugins in this project):
configurations {
productSrc
}
// create zip file which will be published
buildProductSrc(type: Copy) << {
// do the job
}
// publish zip which were build by buildProductSrc
uploadProductSrc {
repositories {
ivy {
url "http://ivy.repo/repo"
}
}
}
So if I run gradle buildProductSrc uploadProductSrc it will build zip and piblish it on ivy repository. Do I understand it correctly becouse it doesn't work?
[UPDATE]
According to Peter Niederwieser answer this is a working version of build:
apply plugin: 'base'
configurations {
productSrc
}
// create zip file which will be published
buildProductSrc << { // unable to create specific task, for example 'type: Copy'
// do the job
}
// publish zip which were build by buildProductSrc
uploadProductSrc {
repositories {
ivy {
url "http://ivy.repo/repo"
}
}
}
To get uploadConfigurationName and buildConfigurationName tasks, you'll have to apply the base plugin, or a plugin that in turn applies the base plugin (java, groovy, etc.). Alternatively, you can declare and configure such tasks yourself (but it takes more effort).

Resources