In gradle, how to copy a subset of compiled test classes - gradle

I tried taking a portion of the compiled test classes of a Gradle project, and put them in a jar file (to create a test case). What happened was, only those classes got compiled out of all test classes. Any idea how to do it right?
Here's my build.gradle:
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
task testSampleJar(type: Zip) {
archiveName "sample.jar"
from compileTestJava {
include "org/example/samples/**"
}
}
test {
dependsOn(testSampleJar)
}
So what seems to be happening is that by just defining the testSampleJar task, the compileTestJava task gets modified to only compile under org.example.samples package. My intent was to use the outputs of compileTestJava and pick something out of them them.
Thanks,
Uri

OK, figured it out, so the two possible solutions are:
//a global filter
task testSampleJar(type: Zip) {
archiveName "sample.jar"
from compileTestJava
include "org/example/samples/**"
}
Or
//Child specification - notice the parentheses around compileTestJava
task testSampleJar(type: Zip) {
archiveName "sample.jar"
from (compileTestJava) {
include "org/example/samples/**"
}
}
I guess the original code was defining compileTestJava instead of using its output, but I don't have a full grasp of Groovy/Gradle DSL to be certain.

Related

gradle - Add custom configuration to installDist for a specific task

I'm trying to make a custom runtime dependency configuration, so that the specified dependencies will only be installed for a specific task. The dependencies are installed using the installDist task. So it seems like I need the configuration to be added to the runtimeClasspath for one task and not the other. I'm thinking I need a custom distribution, but I'm not sure how to set that to have a different runtimeClasspath.
In the example below, I want the run2 task to have the myRuntimeDep dependencies installed, but for the run1 task I do not.
I've be struggling to figure this out all day, does someone know what I'm missing?
Example build.gradle:
configurations {
myRuntimeDep.extendsFrom runtimeOnly
}
dependencies {
...
myRuntimeDep 'the:dependency:1.0'
}
task run1(type: JavaExec, dependsOn: installDist) {
// does not need myRuntimeDep dependencies
}
task run2(type: JavaExec, dependsOn: installDist) {
// needs myRuntimeDep dependencies
}
So after a long weekend, I sort of got something working. Maybe someone can tell me if there's a better way? Also, it doesn't fully work because it doesn't follow transitive dependencies with the configuration (which is kind of a pain because all sub-dependencies need to be manually added).
Solution:
top-level build.gradle
...
subprojects {
configurations {
fooRuntime.extendsFrom runtimeOnly
fooClasspath.extendsFrom runtimeClasspath, fooRuntime
}
distributions {
foo {
contents {
from installDist.destinationDir
from(configurations.fooClasspath) {
into 'lib'
}
}
}
}
installFooDist.dependsOn installDist
}
project A build.gradle
dependencies {
fooRuntime project(':projectB')
fooRuntime project(':projectC') // only need this because transitive dependencies won't work
}
task run(type: JavaExec, dependsOn: installFooDist) {
classpath = fileTree("$installFooDist.destinationDir/lib")
}
project B build.gradle
dependencies {
fooRuntime project(':projectC')
}
task run(type: JavaExec, dependsOn: installFooDist) {
classpath = fileTree("$installFooDist.destinationDir/lib")
}

Gradle 5 QueryDsl Generating Duplicate Classes

I am using queryDsl to generate Q classes with Gradle. It used to work fine with Gradle 3.5, but on upgrading to Gradle 5.5.1, it is failing with duplicate class error.
My generateQueryDsl task works fine generating the classes under 'gensrc/' but on compileJava, the classes are generated again under 'build/generated/' which ends up giving duplicate class error.
dependencies {
api("org.springframework.boot:spring-boot-starter-data-jpa") {
exclude group: "org.hibernate", module: "hibernate-entitymanager"
exclude group: "org.hibernate", module: "hibernate-core"
exclude group: "org.apache.tomcat", module: "tomcat-jdbc"
}
api("com.zaxxer:HikariCP:${hikaricpVersion}")
api("com.h2database:h2:1.4.193")
api("mysql:mysql-connector-java")
api("com.microsoft.sqlserver:sqljdbc42:6.0.8112")
api("org.springframework.data:spring-data-jpa")
api("org.springframework:spring-jdbc")
api("org.springframework:spring-orm")
api("org.eclipse.persistence:javax.persistence:${eclipseLinkPersistenceVersion}")
api("org.eclipse.persistence:eclipselink:${eclipseLinkVersion}")
api("org.eclipse.persistence:org.eclipse.persistence.jpa:${eclipseLinkVersion}")
api("com.mysema.querydsl:querydsl-sql:${queryDslVersion}")
api("com.mysema.querydsl:querydsl-jpa:${queryDslVersion}")
api("com.mysema.querydsl:querydsl-apt:${queryDslVersion}")
annotationProcessor('com.mysema.querydsl:querydsl-apt:3.7.4:jpa')
annotationProcessor("org.springframework.boot:spring-boot-starter-data-jpa")
}
task generateQueryDSL(type: JavaCompile, group: 'build) {
source = sourceSets.main.java
classpath = configurations.compileClasspath
options.annotationProcessorPath = configurations.annotationProcessor
destinationDir = file('gensrc/main/java')
}
compileJava {
dependsOn generateQueryDSL
}
error: duplicate class: com.persistence.domain.model.QOrganizationBasedModel
and likewise for all generated classes
When you use the annotationProcessor configuration, the default compileJava task adds the processor to the compiler, and it will generate classes in build/generated/sources/annotationProcessor/java/main.
In your case, you also declare an additional JavaCompile task, which you give the same annotationProcessor configuration, which will then generate the same classes again.
To solve this, I would simply delete generateQueryDSL task entirely as compileJava most likely does everything you need already. And if you like the generated sources in a different folder, you can do that through CompileOptions, but I would recommend having them under the build folder for most cases.

Gradle Kotlin Native - You have not specified any compilation arguments. No output has been produced

I'm pretty new to Gradle and Kotlin Native. I want to setup my build.gradle so that I can use Native to compile my code. I'm using the Konan plugin to do this. It worked fine when I hard-coded my class in the konanArtifacts.program.srcFiles. But obviously if I add more classes, I'll have to add the paths to srcFiles and I don't (and shouldn't) need to. So I looked around to find a way to add any classes I add automatically, and I attempted sourceSets. But when I try to run the compileKonanClientMingw task now my gradle console gives me the following:
error: you have not specified any compilation arguments. No output has been produced.
I'm not entirely sure that I'm using sourceSets correctly, but this seems to be the farthest I got. Below is my build.gradle
build.gradle:
buildscript {
ext.kotlin_version = '1.2.41'
ext.konan_version = '0.6'
repositories {
mavenCentral()
maven {
url "https://dl.bintray.com/jetbrains/kotlin-native-dependencies"
}
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-native-gradle-plugin:$konan_version"
}
}
plugins {
id 'java'
}
group 'net.dev909'
version '1.0'
apply plugin: 'kotlin'
apply plugin: 'konan'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
sourceSets.main.allSource.srcDir '/src/main/kotlin/'
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
konanArtifacts {
program('client-' + version) {
srcFiles sourceSets.main.runtimeClasspath
}
}
Source sets are a part of the Java world and the Java plugin, so the Kotlin/Native plugin avoids using them to be independent on Java. But you still able to compile a bunch of classes, just specify the srcDir parameter:
konanArtifacts {
program('client-' + version) {
srcDir 'src/main/kotlin/'
}
}
Also note that src/main/kotlin/ is a default source path. So if you use only this path, you don't need to specify it explicitly. You may just write:
konanArtifacts {
program('client-' + version)
}

How to set a dependency for a project with all subprojects?

I don't like that I repeat every repository dependency (let us say, junit), for the main project and for subprojects. Is there a possibility to make the subproject to use the dependencies of the main project. Or is there another way to escape that repetition?
Unlike the accepted answer it's better to use the following:
allprojects {
plugins.withType(JavaPlugin) {
dependencies {
testCompile 'junit:junit:4.12'
}
}
}
The changes will be applied immediately if java plugin already exists or will watch for it to be added and apply later.
Updated
At the moment I use better way to control configuration for plugin - pluginManager. The effect is the same as for plugins.withType, but you don't have to know plugin's class name:
Example for org.springframework.boot plugin:
apply plugin: 'org.springframework.boot'
allprojects {
pluginManager.withPlugin('org.springframework.boot') {
springBoot {
buildInfo()
layout 'DIR'
}
}
}
root/build.gradle
allprojects {
if (plugins.hasPlugin('java')) {
dependencies {
testCompile 'junit:junit:4.12'
}
}
}

How to compile generated and non-generated classes in gradle when they are dependent?

How to compile generated and non-generated classes in gradle when non-generated classes(.java) are dependent on generated classes(.java)?
My generated classes are in $buildDir/generated-sources/thrift
My non generated classes are in src/main/java
The generated classes (.java files) are need to compile the non generated classes (.java files again) to finally put everything under $buildDir/classes/main (all .class files should be here)
Another way to say this would be my non generated classes in (src/main/java) need generated classes as a library dependency (.class files) to compile and run. So far I have the below build.gradle file but it doesn't quite work
plugins {
id "org.jruyi.thrift" version "0.3.1"
}
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: "org.jruyi.thrift"
group 'com.peernova'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile files('/usr/local/lib/thrift-0.9.2.jar')
compile 'org.slf4j:slf4j-log4j12:1.7.12'
testCompile group: 'junit', name: 'junit', version: '4.11'
//compile files("$buildDir/classes/main") {
builtBy 'compileThrift'
}
}
def generatedDir = "$buildDir/generated-sources/thrift"
compileThrift {
thriftExecutable "/usr/local/bin/thrift"
//sourceDir "src/main/thrift"
sourceDir "$buildDir/../../../lib/service_api"
createGenFolder false
}
task thrift(type: Exec) {
commandLine '/usr/local/bin/thrift'
}
compileJava {
dependsOn 'compileThrift'
}
task(run_server, dependsOn: 'classes', type: JavaExec) {
if ( project.hasProperty("appArgs") ) {
args Eval.me(appArgs)
}
main = 'com.apple.project.servicehandlers.server'
classpath = sourceSets.test.runtimeClasspath
}
idea.module.sourceDirs += file("build/generated-sources/thrift")

Resources