Building mutiple Artifacts for Vaadin-Application [Gradle] - java

I am currently working on a Vaadin 8 project using the gradle vaadin-plugin.
In the project there are 3 SourceSets main,a and b. The later two exclude some views and features. Is it possible to build 3 Artifcats via gradle one for each sourceSet. I tried modifying the war task but that broke the views.
conf{
aCompile.extendsFrom compile
aRuntime.extendsFrom runtime
bCompile.extendsFrom compile
bRuntime.extendsFrom runtime
}
sourceSets {
main {
java {
srcDirs = ['src/main/java', 'src/main/generated']
}
}
a{
java {
srcDirs = ['src/main/java', 'src/main/generated']
exclude 'com/exmaple/features/b'
exclude 'com/exmaple/views/b'
}
compileClasspath += main.output
runtimeClasspath += main.output
output.resourcesDir = 'build/resources/main'
output.classesDir = 'build/classes/java/main'
}
b{
java {
srcDirs = ['src/main/java', 'src/main/generated']
exclude 'com/exmaple/features/a'
exclude 'com/exmaple/views/a'
}
compileClasspath += main.output
runtimeClasspath += main.output
output.resourcesDir = 'build/resources/main'
output.classesDir = 'build/classes/java/main'
}
}
task aWar(type: War) {
appendix = "a"
from sourceSets.a.output
}
task bWar(type: War) {
appendix = "b"
from sourceSets.b.output
}

From https://docs.gradle.org/current/userguide/artifact_management.html#sec:declaring_artifacts you should be able to declare an artifact for every source set.
In your case it would look something like this:
task aWar(type: War, dependsOn: classes) {
baseName = 'a.war'
classpath = project.configurations.aRuntime
}
task bWar(type: War, dependsOn: classes) {
baseName = 'b.war'
classpath = project.configurations.bRuntime
}
artifacts {
archives aWar, bWar
}
The WAR task also allows you to include/exclude classes using a regexp. Checkout more at https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.War.html

Related

Sonarqube not picking up gradle sourcesets

I have a java gradle project with a custom sourceset “src/integration-test” next to the standard “src/test” folder.
build.gradle
plugins {
...
id 'org.sonarqube' version '3.3'
id 'jacoco'
}
sourceSets {
integrationTest {
java {
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
srcDir file('src/integration-test/java')
}
resources.srcDir file('src/integration-test/resources')
}
}
jacocoTestReport {
getExecutionData().setFrom(fileTree(buildDir).include("/jacoco/*.exec"))
reports {
xml.required = true
}
}
sonarqube {
properties {
properties["sonar.tests"] += sourceSets.integrationTest.allSource.srcDirs.findAll({ it.exists() })
properties["sonar.coverage.jacoco.xmlReportPaths"] = "${project.buildDir}/reports/jacoco/test/jacocoTestReport.xml"
}
}
...
I’ve tried several things but Sonarqube still only picks up the unit tests in “src/test” and ignoring the ones in my custom sourceset.
Versions:
Gradle v7.3
Sonarqube v3.3
Java 17
What am I doing wrong?
Thanks.

Configuration with name 'integrationTestCompile' not found

when trying to upgrade my gradle version from 5.x to 7.x, getting below error
* What went wrong:
Configuration with name 'integrationTestCompile' not found.
Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
Below is my code
sourceSets {
create("integrationTest") {
compileClasspath += sourceSets.main.get().output
runtimeClasspath += sourceSets.main.get().output
runtimeClasspath += sourceSets.test.get().output
}
}
val integrationTestCompile: Configuration by configurations.getting {
extendsFrom(configurations.testImplementation.get())
}
val integrationTestRuntimeOnly: Configuration by configurations.getting {
extendsFrom(configurations.testRuntimeOnly.get())
}
tasks {
val integrationTest by creating(Test::class) {
description = "Running integration tests."
group = "verification"
testClassesDirs = sourceSets["integrationTest"].output.classesDirs
classpath = sourceSets["integrationTest"].runtimeClasspath
shouldRunAfter("test")
}
}
Using gradle with kotlin.
How to fix this?
In below way you can create integrationTest task in gradle latest version
configurations {
integrationTestImplementation.extendsFrom testImplementation
integrationTestRuntimeOnly.extendsFrom runtimeOnly
}
sourceSets {
integrationTest {
compileClasspath += sourceSets.main.output
compileClasspath += configurations['testRuntimeClasspath']
runtimeClasspath += compileClasspath
}
}
task integrationTest(type: Test) {
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
mustRunAfter test
}

Add multiple source test directories for tests

I have a project with the following directory structure:
src/main/java
src/main/resources
src/test/java
src/test/resources
I want to add a new folder, integrationTest:
src/integrationTest/java
src/integrationTest/resources
Where I want to keep integration tests totally separate from unit tests. How should I go about adding this? In the build.gradle, I'm not sure how to specify a new task that'd pick this folder build it and run the tests separately.
Gradle has a concept of source sets which is exactly what you need here. You have a detailed documentation about that in the Java Plugin documenation here : https://docs.gradle.org/current/userguide/building_java_projects.html#sec:java_source_sets
You can define a new source set "integrationTest" in your build.gradle
sourceSets {
integrationTest {
java {
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
srcDir file('src/integration-test/java')
}
resources.srcDir file('src/integration-test/resources')
}
}
This will automatically create new configurations integrationTestCompile and integrationTestRuntime, that you can use to define an new Task integrationTests:
task integrationTest(type: Test) {
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
}
For reference : a working full example can be found here : https://www.petrikainulainen.net/programming/gradle/getting-started-with-gradle-integration-testing/
Please add the newly created source folder also to source sets in build.gradle as below:
sourceSets {
main {
java {
srcDirs = ['src']
}
}
test {
java {
srcDirs = ['test']
}
}
integrationTest {
java {
srcDirs = ['integrationTest']
}
}
}
Cheers !

Kotlin+Gradle runnable jar - dependency causing "Could not find or load main class"

Gradle build file:
buildscript {
ext.kotlin_version = '1.2.41'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
group 'com.anivive'
version '0.1'
apply plugin: 'kotlin'
sourceSets {
main.java.srcDirs += 'src/main/kotlin/'
test.java.srcDirs += 'src/test/kotlin/'
}
repositories {
mavenLocal()
maven {
url "https://packagecloud.io/anivive/anivive/maven2"
}
mavenCentral()
}
dependencies {
compile 'anivive:Import_Utils:1.2.44'
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
jar {
baseName = 'XMLModelBuilder'
version = '0.1'
manifest {
attributes 'Main-Class': 'com.anivive.Main'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
File structure:
src
- main
- kotlin
- com
- anivive
- Main.kt
Main.kt:
package com.anivive
import com.anivive.util.ExampleClass
object Main {
#JvmStatic
fun main(args: Array<String>) {
ExampleClass.exampleMethod()
}
}
So, I'm trying to build a runnable jar file that will call a single method from a class in the dependency 'anivive:Import_Utils:1.2.44'.
If I remove the dependency, and change my main method to just println("Test"), the jar will run fine via java -jar build/libs/jarFile.jar and it will print out Test.
However, with the dependency included and when trying to call a function from it, I will get Error: Could not find or load main class com.anivive.Main whenever I try to run the Jar file. I suspect it has something to do with hosting the jar file on packagecloud, but I'm surprised this is a problem, considering I can run the program just fine from IntelliJ.
What gives?

How registering the output of a SourceSets for the dependencies

I have a project with the java plugin applied, and an additionnal source set
project(':dependencie') {
apply plugin: 'java'
sourceSets {
generated {
java {
srcDir 'src/generated/java'
}
}
main {
compileClasspath += generated.output
runtimeClasspath += generated.output
}
test {
compileClasspath += generated.output
runtimeClasspath += generated.output
}
}
}
And another depending of this one :
project(':dependsFrom') {
apply plugin: 'java'
dependencies {
compile(
project(':dependencie')
) { transitive = false }
}
}
My problemes is that when I am trying to compile dependsFrom, it doesn't find the classes of the sourceSet generated for the project dependencie. My workaround is to add the line
output.dir(generated.output, builtBy: 'regenerateFromWsdl')
to the main source set of dependencie but any insight to how "registering" the output of the generated source set for future dependencies would be really helpful.
I you want to model the generated code as a separate source set, you have to create a Jar and publish it via the runtime configuration:
task generatedJar(type: Jar) {
from sourceSets.generated.output
}
artifacts {
runtime generatedJar
}
Alternatively, you could add src/generated/java as another source directory for the main source set, in which case it will get compiled/packaged/exported together with that.
In fact I found a solution. You just need to add the generated sourceSet output to the jar task of dependencie project, as it is this same jar task which "inject" the project into the dependecie system.
My problem was that I tought that any additional sourceSet of the project would have been automatically used by all the tasks of the project.
Here the modified gradle script
project(':dependencie') {
apply plugin: 'java'
sourceSets {
generated {
java {
srcDir 'src/generated/java'
}
}
main {
compileClasspath += generated.output
runtimeClasspath += generated.output
}
test {
compileClasspath += generated.output
runtimeClasspath += generated.output
}
}
jar {
from sourceSets.generated.output
}
}

Categories

Resources