Gradle task dependencies on dynamically created tasks - groovy

I have a multiproject build where I need to publish some dependencies to a custom local Maven repository on disk and then add this folder to a distribution zip.
I define publishing tasks for each subproject using the maven-publish plugin.
subprojects {
apply plugin: 'maven-publish'
configurations {
offlineDependencies
}
publishing {
publications {
configurations.each { config ->
if (config.name == "offlineDependencies") {
def files = config.files
config.dependencies.each { dep ->
files.each { file ->
if (file.name == "${dep.name}-${dep.version}.jar") {
"${dep.name}"(MavenPublication) {
artifact (file) {
groupId = "${dep.group}"
artifactId = "${dep.name}"
version = "${dep.version}"
}
}
}
}
}
}
}
}
repositories {
maven {
name 'dependencies'
url '../build/repository'
}
}
}
}
Using the distribution plugin I create a zip file
distributions {
release {
baseName 'release'
contents {
from('src/main/resources/')
into("repository"){
from("$buildDir/repository")
}
}
}
}
How can I make sure that all the dynamically created publish tasks are run before creating the zip file?
I tried making a new task for all subprojects that depends on the dynamically created tasks, but it seems they're not created yet at that time.
subprojects {
task offlineDep << {
println 'Creating offline dependencies'
}
offlineDep.dependsOn {
tasks.findAll { task -> task.name.endsWith('PublicationToDependenciesRepository') }
}
}

I found a solution to this problem. By collecting the names of the artifacts and generating the tasknames I know will be created later and adding them as dependencies.
subprojects {
apply plugin: 'maven-publish'
def offlineDependencyNames = []
publishing {
publications {
configurations.each { config ->
if (config.name == "offlineDependencies") {
def files = config.files
config.dependencies.each { dep ->
files.each { file ->
if (file.name == "${dep.name}-${dep.version}.jar") {
offlineDependencyNames << dep.name
"${dep.name}"(MavenPublication) {
artifact (file) {
groupId = "${dep.group}"
artifactId = "${dep.name}"
version = "${dep.version}"
}
}
}
}
}
}
}
}
repositories {
maven {
name 'dependencies'
url "${rootProject.buildDir}/repository"
}
}
}
task publishOfflineDependencies
publishOfflineDependencies.dependsOn {
offlineDependencyNames.collect { name ->
"publish${name[0].toUpperCase()}${name.substring(1)}PublicationToDependenciesRepository"
}
}
}
releaseDistZip.dependsOn {
subprojects.collect {
p -> p.path + ':publishOfflineDependencies'
}
}

Related

Failed to apply plugin [id 'org.gradle.checkstyle']?

How to solve this error?
Error:(24, 1) A problem occurred evaluating root project 'myproject10'.
Failed to apply plugin [id 'org.gradle.checkstyle']
Could not create service of type CachingFileHasher using TaskExecutionServices.createFileSnapshotter().
subprojects {
def projectName = it.name
if (projectName != "graphql-java-support") {
apply from: "../gradle/dependencies.gradle"
}
buildscript {
System.properties['com.android.build.gradle.overrideVersionCheck'] = 'true'
repositories {
jcenter()
maven { url "https://jitpack.io" }
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}
}
repositories {
jcenter()
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
maven { url 'https://maven.google.com' }
}
if (projectName != "graphql-java-support") {
apply plugin: 'checkstyle'
checkstyle {
toolVersion = "5.9"
configFile rootProject.file('checkstyle.xml')
configProperties = ['checkstyle.cache.file': rootProject.file('build/checkstyle.cache')]
ignoreFailures false
showViolations true
}
task checkstyle(type: Checkstyle) {
source 'src/main/java'
include '**/*.java'
classpath = files()
}
afterEvaluate {
if (project.tasks.findByName('check')) {
check.dependsOn('checkstyle')
}
}
}
}
project.ext.preDexLibs = !project.hasProperty('disablePreDex')
subprojects {
project.plugins.whenPluginAdded { plugin ->
if ("com.android.build.gradle.AppPlugin".equals(plugin.class.name)) {
project.android.dexOptions.preDexLibraries = rootProject.ext.preDexLibs
} else if ("com.android.build.gradle.LibraryPlugin".equals(plugin.class.name)) {
project.android.dexOptions.preDexLibraries = rootProject.ext.preDexLibs
}
}
}

No such field: maven for class: org.jfrog.gradle.plugin.artifactory.dsl.PublisherConfig

I try to build java library using android studio and want to host at my local artifactory.
I face this problem at noon. Try different code, but no improvements.
buildscript {
repositories {
jcenter()
}
repositories {
maven {
url 'http://localhost:8081/artifactory/libs-release-local'
credentials {
username = "${artifactory_user}"
password = "${artifactory_password}"
}
name = "maven-main-cache"
}
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.0.1"
}
}
project.getExtensions().getByName("ext")
apply plugin: 'scala'
apply plugin: 'maven-publish'
apply plugin: "com.jfrog.artifactory"
version = '1.0.0-SNAPSHOT'
group = 'com.buransky'
repositories {
add buildscript.repositories.getByName("maven-main-cache")
}
dependencies {
compile 'org.scala-lang:scala-library:2.11.2'
}
tasks.withType(ScalaCompile) {
scalaCompileOptions.useAnt = false
}
artifactory {
contextUrl = "${artifactory_contextUrl}"
publish {
repository {
repoKey = 'libs-snapshot-local'
username = "${artifactory_user}"
password = "${artifactory_password}"
maven = true
}
defaults {
publications ('mavenJava')
}
}
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
Above my build.gradle code block.
Please help. I didn't found any solutions in google
Looking here it looks like it's
artifactory {
publish {
repository {
ivy {
mavenCompatible = true
}
}
}
}
Instead of
artifactory {
publish {
repository {
maven = true
}
}
}

gradle clean in linux does not delete the contents of buildDir

Gradle 2.13
Revision: 3b427b1481e46232107303c90be7b05079b05b1c
OS: Linux 3.10.0-327.36.3.el7.x86_64 amd64
In linux on calling the clean task the contents of buildDir path are not deleted whereas the same in windows deletes the contents of buildDir(expected behaviour). Whats the reason ?
Code sample
apply plugin : 'java'
ant.importBuild 'build.xml'
sourceCompatibility = 1.8
def projVersion = ant.properties['version']
def projName = "xyz"
def buildPath = "/home/test/build"
def dependencyPath = "/home/test/dependency"
buildDir = new File("$buildPath","$projName")
def serviceDirBin = new File(buildDir , 'bin')
def serviceDirConf = new File(buildDir , 'conf')
def serviceDirThirdparty = new File(buildDir , 'thirdparty')
repositories {
flatDir {
dirs file("$dependencyPath"),
}
}
sourceSets{
main{
java{
srcDir 'src'
}
output.classesDir 'classes'
}
test{
java {
srcDir 'src/test'
}
}
}
dependencies {
/*Some compile dependencies*/
}
jar{
destinationDir = new File(serviceDirThirdparty, 'lib')
baseName='xyz'
manifest {
'Build-Timestamp': new Date().format('yyyy-dd-MM HH:mm:ss'),
'Specification-Version': '1.0',
'Implementation-Version': "$projVersion"
}
}
task xyzpackager << {
copy{
from ("$buildPath/sf/distribution/bin")
into file(serviceDirBin)
}
copy{
from ('conf')
into file(serviceDirConf)
}
copy{
from configurations.compile
from configurations.runtime
into new File(serviceDirThirdparty,'lib')
}
}
test {
reports {
html.enabled = false
junitXml.enabled = true
junitXml.destination = file("TestReport/xml")
}
}
jar.doLast {
delete "$buildDir/tmp","$buildDir/dependency-cache","$buildDir/classes","$buildDir/libs"
}
compileTestJava.doLast{
delete "TestReport"
}
the jar is copied to thirdparty folder and the bin/conf folders have some other files. SO basically i want to delete the buildDir before every new build

Different dependencies for different flavors and build types in gradle

Suppose I have such build.gradle:
android {
productFlavors {
flavor1 {
}
flavor2 {
}
}
}
How do I specify different dependencies such as:
dependencies {
flavor1DebugCompile ...
flavor1ReleaseCompile ...
flavor2DebugCompile ...
flavor2ReleaseCompile ...
}
Gradle will generate errors such as: Could not find method flavor1DebugCompile() for arguments ...
it's a bug.
try this:
def customDeps = [
flavor1DebugCompile : dependencies.project(path: ':yourproject', configuration: 'flavor1Debug'),
flavor2DebugCompile : dependencies.project(path: ':yourproject', configuration: 'flavor2Debug'),
flavor1ReleaseCompile : dependencies.project(path: ':yourproject', configuration: 'flavor1Release'),
flavor2ReleaseCompile : dependencies.project(path: ':yourproject', configuration: 'flavor2Release'),
]
configurations.all() { config ->
Dependency d = customDeps.get(config.name)
if (d != null) {
config.dependencies.add(d)
}
}

Facing issues while running cucumber with serenity

To integrate Cucumber with Serenity, I have created below Gardle file. Serenity is working fine, however I am not able to use it with Cucumber. When I use #RunWith(CucumberWithSerenity.class) in runner class, it gives me unresolved type error.
build.gradle:
apply plugin: "java"
apply plugin: "maven"
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'net.serenity-bdd.aggregator'
apply plugin: 'com.jfrog.bintray'
group = "myorg"
version = 1.0
repositories {
maven {
url "http://nexus2.sdmc.ao-srv.com/content/groups/inhouse_dit/"
}
}
buildscript {
repositories {
maven {
url "http://nexus2.sdmc.ao-srv.com/content/groups/inhouse_dit/"
}
}
dependencies {
classpath("net.serenity-bdd:serenity-gradle-plugin:1.0.47")
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:0.6'
}
}
ext {
bintrayBaseUrl = 'https://api.bintray.com/maven'
bintrayRepository = 'maven'
bintrayPackage = 'serenity-cucumber'
projectDescription = 'Serenity Cucumber integration'
if (!project.hasProperty("bintrayUsername")) {
bintrayUsername = 'wakaleo'
}
if (!project.hasProperty("bintrayApiKey")) {
bintrayApiKey = ''
}
serenityCoreVersion = '1.0.49'
cucumberJVMVersion = '1.2.2'
//versionCounter = new ProjectVersionCounter(isRelease: project.hasProperty("releaseBuild"))
}
sourceSets.all { set ->
def jarTask = task("${set.name}Jar", type: Jar) {
baseName = baseName + "-$set.name"
from set.output
}
artifacts {
archives jarTask
}
}
sourceSets {
api
impl
}
dependencies {
apiCompile 'commons-codec:commons-codec:1.5'
implCompile sourceSets.api.output
implCompile 'commons-lang:commons-lang:2.6'
compile "info.cukes:cucumber-java:${cucumberJVMVersion}"
compile "info.cukes:cucumber-junit:${cucumberJVMVersion}"
testCompile 'net.serenity-bdd:core:1.0.47'
testCompile 'net.serenity-bdd:serenity-junit:1.0.47'
testCompile('junit:junit:4.11')
testCompile('org.assertj:assertj-core:1.7.0')
testCompile('org.slf4j:slf4j-simple:1.7.7')
testCompile sourceSets.api.output
testCompile sourceSets.impl.output
testCompile 'org.codehaus.groovy:groovy-all:2.3.6'
testCompile("org.spockframework:spock-core:0.7-groovy-2.0") {
exclude group: "junit"
exclude module: "groovy-all"
}
testCompile("com.github.goldin:spock-extensions:0.1.4") {
exclude module: "spock-core"
exclude module: "slf4j-api"
}
runtime configurations.apiRuntime
runtime configurations.implRuntime
}
gradle.startParameter.continueOnFailure = true
jar {
from sourceSets.api.output
from sourceSets.impl.output
manifest {
attributes("Implementation-Title": "Serenity Cucumber Plugin",
"Implementation-Version": project.version.toString())
}
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar, javadocJar
}
bintray {
user = bintrayUsername //this usually comes form gradle.properties file in ~/.gradle
key = bintrayApiKey //this usually comes form gradle.properties file in ~/.gradle
publications = ['mavenJava'] // see publications closure
pkg {
repo = 'maven'
userOrg = 'serenity'
name = 'serenity-cucumber'
desc = 'Serenity Cucumber integration'
licenses = ['Apache-2.0']
labels = ['serenity','bdd','cucumber']
}
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: uri("${buildDir}/repo"))
addFilter("main") { artifact, file -> artifact.name == project.name }
["api", "impl"].each { type ->
addFilter(type) { artifact, file -> artifact.name.endsWith("-$type") }
// We now have to map our configurations to the correct maven scope for each pom
["compile", "runtime"].each { scope ->
configuration = configurations[type + scope.capitalize()]
["main", type].each { pomName ->
pom(pomName).scopeMappings.addMapping 1, configuration, scope
}
}
}
}
}
}
task wrapper (type: Wrapper) {
gradleVersion = '2.3'
distributionUrl = 'http://nexus2.sdmc.ao-srv.com/content/repositories/inhouse_dit_thirdparty/org/gradle/gradle-bin/2.3/gradle-2.3-bin.zip'
}
Please suggest what I need to change to run serenity with Cucumber. Thanks in advance.
You need to add serenity-cucumber to your dependencies:
testCompile 'net.serenity-bdd:serenity-cucumber:1.0.17'
Following line fixed the issue for me (gradle 4.10):
testCompile group: 'net.serenity-bdd', name: 'serenity-cucumber', version: '1.9.40'

Resources