Gradle changes JRE after "Gradle Refresh" - java

I'm currently using LibericaJDK-11 as my default JRE in eclipse, also in my project. I added Gradle to my Project and everytime I Refresh the Project via Gradle. It sets the JRE to JavaSE-11. And i have to manually put it back to Liberica.
Is there a way to set a default JRE for Gradle in gradle.build oder gradle.settings?
The following code is my gradle.build.
plugins {
id 'java-library'
id 'application'
id 'pmd'
}
version = '0.0.1'
group = 'zuulsLostPlace.MainApp'
application{
mainClassName = "zuulsLostPlace.MainApp"
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.4.2'
}
test {
useJUnitPlatform()
}
repositories {
mavenCentral()
}
task(runGameNoGUI, dependsOn: 'classes', type: JavaExec) {
group 'application'
description 'Starts the Text Adventure without GUI'
main = 'zuulsLostPlace.model.Game'
standardInput = System.in
ignoreExitValue true
classpath = sourceSets.main.runtimeClasspath
}
task(runGameGUI, dependsOn: 'classes', type: JavaExec) {
group 'application'
description 'Starts the Text Adventure with GUI'
main = 'zuulsLostPlace.MainApp'
standardInput = System.in
ignoreExitValue true
classpath = sourceSets.main.runtimeClasspath
}
task myJavadocs(type: Javadoc) {
source = sourceSets.main
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
options.addStringOption('encoding', 'UTF-8')
options.addStringOption('charSet', 'UTF-8')
}
}

Related

Invalid image reference registry when using jib to dockerize gradle project

I had a problem when dockerize the Gradle project.
running gradle jib
What went wrong:
Execution failed for task ':jib'.
Invalid image reference registry.hub.docker.com/myusername/${rootProject.name}, perhaps you should check that the reference is formatted correctly according to https://docs.docker.com/engine/reference/commandline/tag/#extended-description
For example, slash-separated name components cannot have uppercase letters
Root Module
setting.gradle
rootProject.name = 'myproject'
include 'discovery-service'
include 'client-service'
include 'notification-service'
include 'api-gateway'
build.gradle
jib {
var tag = 'latest'
from {
image = 'eclipse-temurin:17.0.4.1_1-jre'
}
to {
image = 'registry.hub.docker.com/myUsername/${rootProject.name}'
}
}
One of the service
plugins {
id 'java'
id 'org.springframework.boot' version '2.7.5'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}
group = 'com.leekify'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
jar {
baseName = 'client-service'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
....
}
tasks.named('test') {
useJUnitPlatform()
}
setting.gradle
rootProject.name = 'client-service'
Use double-quotes instead of single quotes for the jib.to.image string so that Gradle evaluates the variable.

Why I can not compile JavaFX program using grade?

yesterday I created a JavaFX project and I don't know why but when I want to run the app with commands java says:
Gradle task : build
Command : java -jar "name_of_the_file.jar"
Error: Could not find or load main class com.example.HelloApplication
Caused by: java.lang.NoClassDefFoundError:javafx/application/Application
but I can run it using gradlew run
module-info.java file
module com.example{
requires javafx.controls;
requires javafx.fxml;
opens com.example ;
exports com.example;
}
Manifest.MF file
Manifest-Version: 1.0
Main-Class: com.example.HelloApplication
build.gradle file
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.13'
id 'org.beryx.jlink' version '2.25.0'
}
group 'com.example'
version '1.0-SNAPSHOT'
jar{
manifest{
attributes ('Main-Class' : 'com.example.HelloApplication' )
}
}
repositories {
mavenCentral()
}
ext {
junitVersion = '5.8.2'
}
sourceCompatibility = '17'
targetCompatibility = '17'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
application {
mainModule = 'com.example'
mainClass = 'com.example.jfx.HelloApplication'
}
javafx {
version = '17.0.2'
modules = ['javafx.controls', 'javafx.fxml']
}
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
}
test {
useJUnitPlatform()
}
jlink {
imageZip = project.file("${buildDir}/distributions/app-${javafx.platform.classifier}.zip")
options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
launcher {
name = 'app'
}
}
jlinkZip {
group = 'distribution'
}
and project structure
project structure
I am using inteliij idea community and my operating system is windows
I really don't know what is wrong it seems all correct to me. Strange

Gradle "Couldn't replace placeholder" on build

I'm having a JavaFX project configured with Gradle in IntelliJ.
Whenever I'm trying to run gradlew.bat clean build or simply the build task directly from IntelliJ, this is what I receive:
> Task :startScripts FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':startScripts'.
> Couldn't replace placeholder in E:\git\yuber\build\scripts\yuber
I'm also using the shadowJar plugin, which actually works when I'm trying to create a jar file and run it. And it works perfectly. Although, I'm not sure why it won't build. Looked everywhere for an answer, but seems that no one actually had this problem at this moment?
This is my build.gradle file:
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.10'
id 'org.beryx.jlink' version '2.24.1'
id "com.github.johnrengelman.shadow" version "7.1.2"
}
group 'com.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
ext {
junitVersion = '5.8.2'
}
sourceCompatibility = '11'
targetCompatibility = '11'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
application {
mainModule = 'com.example.yuber'
mainClass = 'com.example.yuber.Launcher'
}
javafx {
version = '11.0.2'
modules = ['javafx.controls', 'javafx.fxml', 'javafx.web']
}
dependencies {
implementation('org.controlsfx:controlsfx:11.1.0')
implementation('com.dlsc.formsfx:formsfx-core:11.3.2') {
exclude(group: 'org.openjfx')
}
implementation('net.synedra:validatorfx:0.1.13') {
exclude(group: 'org.openjfx')
}
implementation('org.kordamp.ikonli:ikonli-javafx:12.2.0')
implementation('org.kordamp.bootstrapfx:bootstrapfx-core:0.4.0')
implementation('eu.hansolo:tilesfx:11.48') {
exclude(group: 'org.openjfx')
}
implementation 'org.json:json:20220320'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.2.2'
implementation 'com.fasterxml.jackson.core:jackson-core:2.13.2'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.13.2'
implementation 'junit:junit:4.13.1'
testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
}
test {
useJUnitPlatform()
}
jlink {
imageZip = project.file("${buildDir}/distributions/app-${javafx.platform.classifier}.zip")
options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
launcher {
name = 'app'
}
}
jlinkZip {
group = 'distribution'
}
Updating the org.openjfx.javafxplugin (https://plugins.gradle.org/plugin/org.openjfx.javafxplugin) to the latest version inside build.gradle (from 0.0.10, as IntelliJ added it, to 0.0.13) seemed to have fixed the problem.

Gradle Build - Execution failed for task ':sonarqube' can't be indexed twice

I am getting an error "File can't be indexed twice" in gradle build for sonar. Do i have to add any inclusion/exclusion property for sonar? Any help greatly appreciated.
If I take out "src/main/java" in test under sourceSets, I am getting compilation error for junit test cases.
This is how my build.gradle file content look,
group = 'groupid'
apply plugin: 'gradleFortify'
apply plugin: 'org.sonarqube'
apply plugin: 'eclipse'
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'groovy'
//apply plugin: "java-library-distribution"
//apply plugin: 'distribution'
version=version
sourceCompatibility = 1.8
repositories {
maven {
url "artifactory"
credentials {
}
}
sonarqube {
properties {
property 'sonar.sourceEncoding', 'UTF-8'
property "sonar.exclusions", ["**/*Test.java"]
}
}
}
buildscript {
repositories {
maven {
url "artifactory"
credentials {
}
}
dependencies {
classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:4.9.7'
classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.0'
}
}
sourceSets {
main {
java {
srcDirs = ['src/main/java']
}
}
test {
java.srcDirs = ['src/test/java','src/main/java']
}
}
dependencies{
}
task preclean(type: Delete) {
delete 'build'
}
task jcompile(type: JavaCompile) {
dependsOn preclean
source = fileTree(dir: 'src/main/java', include: '**/*.java')
destinationDir = file('build/classes')
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
classpath = configurations.runtime
}
task copyResources( type: Copy ) {
}
task copyClasses( type: Copy ) {
}
jar {
dependsOn jcompile
archiveName = 'prj1.jar'
manifest {
attributes('Implementation-Title': project.name,
'Implementation-Version': project.version)
}
into('lib') {
println "includeInJar: " + configurations.runtime.collect { File file -> file }
from configurations.runtime
}
}
// jar.finalizedBy(jarTask) { }
sonarqube {
properties {
property "sonar.projectName", "prj-${version}"
property "sonar.projectKey", "prj-${version}"
}
}
test {
useJUnitPlatform()
finalizedBy jacocoTestReport
}
jacocoTestReport {
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it, includes: ['com/**'])
})
}
}
The issue described is most likely caused by custom tasks for what Gradle offers out of the box:
Task preclean deletes probably too much. Use clean instead.
Task jcompile compiles the main Java sources which basically duplicates compileJava. The jar task, by default, already depends on compileJava.
I suggest the following:
Remove sourceSets configuration since you're following the convention of using src/main/java and src/test/java.
Remove custom tasks preclean and jcompile.
Cleanup your build script and remove empty tasks copyClasses and copyResources as well as no-op assignment version=version.
From there on, should you have requirements to customize the build, try to resort to what Gradle offers already. You may want to read Building Java & JVM projects first.

Unable to import compiled java avro files into Scala codebase

I am trying to reference the generated Avro files in my scala code basebase. I am using the commercehub avro gradle plugin to generate the avro java compiled files and they get correctly generated in build/generated-main-avro-java/
This is how my gradle file looks. The problem is that the java files get generated correctly but I cannot import them in my scala code. Any idea on what I maybe doing wrong here?
buildscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath "gradle.plugin.com.lightbend.akka.grpc:akka-grpc-gradle-plugin:0.7.2"
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.8'
}
}
plugins {
id 'scala'
id 'application'
id "com.google.protobuf" version "0.8.8"
id 'com.github.johnrengelman.shadow' version '5.2.0'
id "com.commercehub.gradle.plugin.avro" version "0.9.1"
}
apply plugin: 'java'
apply plugin: 'com.lightbend.akka.grpc.gradle'
apply plugin: 'com.google.protobuf'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: "com.commercehub.gradle.plugin.avro"
akkaGrpc {
language = "Scala"
generateClient = true
generateServer = true
}
mainClassName = 'com.test.App'
repositories {
mavenLocal()
mavenCentral()
google()
}
tasks.withType(ScalaCompile) {
ScalaCompileOptions.metaClass.daemonServer = true
ScalaCompileOptions.metaClass.fork = true
ScalaCompileOptions.metaClass.useAnt = false
ScalaCompileOptions.metaClass.useCompileDaemon = false
}
def scalaVersion = "2.12"
ext.elasticAPMVersion = "1.14.0"
// Define a separate configuration for managing the dependency on Jetty ALPN agent.
configurations {
alpnagent
}
dependencies {
...
...
...
...
...
implementation "org.apache.avro:avro:1.9.2"
}
test {
jvmArgs "-javaagent:" + configurations.alpnagent.asPath
maxParallelForks = 1
}
run {
mainClassName = mainClassName
standardInput = System.in
}
task runServer(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
main = mainClassName
jvmArgs "-javaagent:" + configurations.alpnagent.asPath
}
shadowJar {
classifier = 'shadow'
mainClassName = mainClassName
archiveName = "${title}-${version}.jar"
append 'reference.conf'
destinationDir new File("./docker/bin")
zip64 true
}
I would say, you need to add the generated sources folder to source sets and scala compiler source - something like this:
compileScala {
source = ['src/main/scala', 'src/main/java', 'build/generated-main-avro-java/']
}
sourceSets {
main {
avro {
scala.srcDir file('build/generated-main-avro-java/')
}
}
}

Categories