How to set Xmx and Xms properly in Gradle - java

I'm trying to fix the heap size memory using -Xmx50M in build.gradle file, it doesn't work, the memory size always goes beyond 50M.
Details :
My build.gradle
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.10'
}
group 'com.karrty'
repositories {
mavenCentral()
}
ext {
junitVersion = '5.7.1'
}
sourceCompatibility = '17'
targetCompatibility = '17'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
application {
mainModule = 'com.karrty.karrtyversion1'
mainClass = 'com.karrty.karrtyversion1.Principle.main'
}
javafx {
version = '16'
modules = ['javafx.controls', 'javafx.fxml']
}
dependencies {
implementation('org.controlsfx:controlsfx:11.1.0')
}
run {
jvmArgs = [
"-Xms50m",
"-Xmx50m"
]
}
Screen shot of the issue
as you can see, the app has located 500MB in memory and using almost 250MB of it, even tho I specified the heap memory size to be 50MB .

Am not 100% sure , but i think you should be using gradle.properties to set -Xmx50M .
You can check the official docs to make sure .
gradle.properties should look like this
org.gradle.jvmargs=-Xmx512m "-XX:MaxMetaspaceSize=256m"
This is the default and you can change based on what you need to do .

For some reason using the short VM arguments Xms50m and Xmx50m is not working with gradle, but the full expressions -XX:MaxHeapSize=50m and -XX:InitialHeapSize=50 are working, if you also have the same issue , just replace those short expressions with their full expressions and it will work.

Related

How to set compileJava' task ( 11) and 'compileKotlin' task (1.8) jvm target compatibility to the same Java version in build.gradle.kts?

Build.gradle.kts
buildscript {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
dependencies {
classpath ("com.android.tools.build:gradle:7.0.2")
classpath ("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30")
classpath("gradle.plugin.com.github.spotbugs.snom:spotbugs-gradle-plugin:${Versions.spotbugsGradlePluginVersion}")
classpath("se.bjurr.violations:violations-gradle-plugin:${Versions.violationsVersion}")
}
}
//android {
// compileOptions {
// sourceCompatibility = JavaVersion.VERSION_11
// targetCompatibility = JavaVersion.VERSION_11
// }
//
// kotlinOptions {
// jvmTarget = JavaVersion.VERSION_11.toString()
// }
//}
plugins {
`maven-publish`
`java-gradle-plugin`
`kotlin-dsl`
id ("io.gitlab.arturbosch.detekt") version ("1.18.1")
}
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
dependencies {
compileOnly(gradleApi())
testImplementation(gradleTestKit())
testImplementation("junit:junit:${Versions.jUnitVersion}")
}
val generatedSources = tasks.register<GenerateVersionsFileTask>("generateSources")
ERROR :
'compileJava' task (current target is 11) and 'compileKotlin' task (current target is 1.8) jvm target compatibility should be set to the same Java version.
When I uncomment android {}
Error :
Script compilation errors:
Line 15: android {
^ Unresolved reference: android
Thanks for your time and effort :)
Jitendra
You can set java version for java with
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
or alternatively:
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(11))
}
and for kotlin with:
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = "11"
}
}
All samples are in gradle kotlin dsl.
#Marian's answer didn't quite help me.
I end up setting the following in the app build.gradle
Android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget=11
}
...
}
This worked nicely for me:
kotlin {
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of("11"))
}
}
That way I only need to set it once.
If anyone keeps getting warnings about a java compilation task being set to java 1.8 you can add this to the gradle script:
afterEvaluate {
tasks.withType<JavaCompile>().configureEach {
sourceCompatibility = JavaVersion.VERSION_11.toString()
targetCompatibility = JavaVersion.VERSION_11.toString()
}
}
I was getting warnings on some java compile tasks on AS and this solved it for me.
In order to have the 'same version of Java' in both 'Java and Kotlin',just modify your build.gradle file like this (example given for Java 11):
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
...
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
/*
Not working.. (at least for me)
kotlinOptions {
jvmTarget = 11
}
*/
// Working :-)
kotlin {
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of("11"))
}
}
...
}
My configuration:
Gradle:
Android Gradle Plugin: com.android.tools.build:gradle:7.4.1
Gradle: 7.5 (as of ./gradle/wrapper/gradle-wrapper.properties)
Kotlin:
Android Kotlin Plugin: 221 ( 221-1.8.0-release-for-android-studio-AS5591.52 )
(see https://kotlinlang.org/docs/whatsnew18.html#install-kotlin-1-8-0 )
Kotlin Gradle Plugin: org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.0
NB:
More information about Kotlin configuration regarding that matter is available here:
https://blog.jetbrains.com/kotlin/2021/11/gradle-jvm-toolchain-support-in-the-kotlin-plugin/
This solution worked for me:
Just replace the statement (or similar to this):
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
with
kotlinOptions {
jvmTarget = "1.8"
}
in all the module level build.gradle files and then sync gradle.
Hope this works. Happy Coding...!
Surprisingly the culprit is very simple. You need to declare your KotlinCompile task before JavaCompile. Like this
tasks {
withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf(
"-Xextended-compiler-checks",
"-Xinline-classes",
"-Xjsr305=strict",
"-Xjvm-default=all",
"-Xskip-prerelease-check",
)
apiVersion = "1.8"
languageVersion = "1.8"
jvmTarget = "11"
}
}
withType<JavaCompile> {
sourceCompatibility = "11"
targetCompatibility = "11"
}
}
Marian's solution (see above) needs to be applied, but you may still see the same message as a warning especially if you run "gradle build" from a command line. I think if you "synch" with gradle from Android Studio, you might not see the warning message. If you still see the message after properly applying Marian's solution, it's apparently a bogus warning message. My understanding is that the bogus warning message goes away once you upgrade to using kotlin 1.6+. You can disable this bogus warning by adding the following to your gradle.properties file.
kotlin.jvm.target.validation.mode = IGNORE
I'm not recommending the use of the above, but I it certainly solves the issue of seeing the bogus warning message. If you do use it, remember to remove it when it's no longer needed.
The problem for most of us is that we can't upgrade to kotlin 1.6.21 at this time. In order to upgrade to kotlin 1.6.21 you need to upgrade to android gradle 7.4. To upgrade to android gradle 7.4 you need an Android Studio version greater than Dolphin | 2021.3.1, so you would have to use one of the preview versions of Android Studio to do that at this time.
for compatibility information, see
https://developer.android.com/studio/releases/gradle-plugin and
https://developer.android.com/studio/releases#android_gradle_plugin_and_android_studio_compatibility

Failed to apply plugin [id 'forge']

This is a continuation of this question My initial issue has been solved, but a new one came after.
Following the tutorial mentioned in it, having solved a few errors, I now get an error when I try to run .\gradlew tasks:
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\Users\benji\MinecraftWorkspace\forge-1.7.10-10.13.4.1614-1.7.10-src\build.gradle' line: 18
* What went wrong:
A problem occurred evaluating root project 'forge-1.7.10-10.13.4.1614-1.7.10-src'.
> Failed to apply plugin [id 'forge']
> You must set the Minecraft Version!
> java.lang.NullPointerException (no error message)
How do I set the Minecraft version? (1.7.10 in this instance)
Edit to include build.gradle:
buildscript {
repositories {
mavenCentral()
maven {
name = "forge"
url = "http://files.minecraftforge.net/maven"
}
maven {
name = "sonatype"
url = "https://oss.sonatype.org/content/repositories/snapshots/"
}
}
dependencies {
classpath 'net.minecraftforge.gradle:ForgeGradle:1.2-SNAPSHOT'
}
}
apply plugin: 'forge'
version = "1.0"
group= "com.yourname.modid" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "modid"
minecraft {
version = "1.7.10-10.13.4.1614-1.7.10"
runDir = "eclipse"
}
dependencies {
// you may put jars on which you depend on in ./libs
// or you may define them like so..
//compile "some.group:artifact:version:classifier"
//compile "some.group:artifact:version"
// real examples
//compile 'com.mod-buildcraft:buildcraft:6.0.8:dev' // adds buildcraft to the dev env
//compile 'com.googlecode.efficient-java-matrix-library:ejml:0.24' // adds ejml to the dev env
// for more info...
// http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html
// http://www.gradle.org/docs/current/userguide/dependency_management.html
}
processResources
{
// this will ensure that this task is redone when the versions change.
inputs.property "version", project.version
inputs.property "mcversion", project.minecraft.version
// replace stuff in mcmod.info, nothing else
from(sourceSets.main.resources.srcDirs) {
include 'mcmod.info'
// replace version and mcversion
expand 'version':project.version, 'mcversion':project.minecraft.version
}
// copy everything else, thats not the mcmod.info
from(sourceSets.main.resources.srcDirs) {
exclude 'mcmod.info'
}
}
Good day.
For minecraft version 1.7.10.
I was with the same problem, searching the forum in Japanese.
this link Forum
You have to modify the repositories and dependencies of the build.gradle and change the gradle-wrapper.properties to version 5.6.4.
As indicated in this link ForgeGradle-1.2
Contains an example to modify your build.gradle example
build.gradle
buildscript {
repositories {
mavenCentral()
maven { url = "https://jcenter.bintray.com/" }
maven {
name = "forge"
url = "https://files.minecraftforge.net/maven"
}
maven {
name = "sonatype"
url = "https://oss.sonatype.org/content/repositories/snapshots/"
}
}
dependencies {
classpath ('com.anatawa12.forge:ForgeGradle:1.2-1.0.+') {
changing = true
}
}
gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip
I made it work
Sorry, my English is very basic. I'm using a translator.
pls update the plugin to latest one . For reference check this link and link.
also try deleting the .gradle folder in your User Home and run it again.
I solved this by installing the 'Recommended' version of Forge 1.7.10 instead of the 'Latest'. Simple fix really, although I've already hit another wall.

Creating correct Maven dependencies from Gradle

After some hours of search and research I came up with a working example for writing the correct Maven dependencies into the generated pom.xml when using Gradle 1.11 and maven-publish plugin.
The first problem I faced was the provided dependency that always was written as runtime into the pom.xml
The second problem was the dynamic version, that I use for minor version changes. Maven and Gradle have different notations and the maven-publish simply writes the Gradle kind of notation into the pom.xml
Here is my example:
apply plugin: 'java'
apply plugin: 'maven-publish'
group = 'de.pentos'
version = '0.4.4'
sourceCompatibility = 1.7
repositories {
mavenCentral()
mavenLocal()
}
configurations {
provided
compile.extendsFrom provided
}
dependencies {
provided("org.projectlombok:lombok:1.+")
provided("javax.servlet:javax.servlet-api:3.1.0")
compile("org.slf4j:slf4j-api:1.7+")
compile("com.fasterxml.jackson.core:jackson-databind:2.3.+")
compile("joda-time:joda-time:2.3+")
compile("org.springframework:spring-webmvc:3.2+")
compile("org.springframework.security:spring-security-web:3.1+")
testCompile("junit:junit:4.11")
}
jar { baseName = "${project.group}.${project.name}" }
publishing {
publications {
jar(MavenPublication) {
from components.java
artifactId "${project.name}"
artifact sourceJar { classifier "sources" }
pom.withXml {
final Node root = asNode()
final versionPattern = ~/(?:(.+)\.)?(.+?)\.?\+/
configurations.compile.allDependencies.each {
final name = it.name
final group = it.group
final m = versionPattern.matcher(it.version)
if (m.matches()) {
final base = m[0][1]
final rest = m[0][2].toInteger()
final version = '[' + (base ? base + '.' : '') + (rest) + ',' + (base ? base + '.' : '') + (rest + 1) + ')'
root.dependencies.first().findAll{
it.groupId.first().value()[0] == group && it.artifactId.first().value()[0] == name
}.each {
it.version.first().value = version
}
}
}
configurations.provided.allDependencies.each {
final name = it.name
final group = it.group
root.dependencies.first().findAll{
it.groupId.first().value()[0] == group && it.artifactId.first().value()[0] == name
}.each {
it.scope.first().value = 'provided'
}
}
}
}
}
}
tasks.withType(Compile) { options.encoding = 'UTF-8' }
task sourceJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}
So what is my Question?
I had some trouble with a lot examples that simply compared
..findAll {
it.groupId == group
}
This hadn't worked for me and I had to find out by lot of testing how to get the findAll to work.
So is my version to detailed? Can it be written with less code?
Can I extract the functionality for dependency management into a more system wide script, that can be reused from all other projects, that need this dependency management? How?
During compileJava I get the warning :compileJavawarning: [options] bootstrap class path not set in conjunction with -source 1.7
What does this mean, and how can I fix that.
I had some trouble with a lot examples that simply compared [...]
it.groupId is not a String. You'll need something like it.groupId.value(), or perhaps it.groupId[0].value().
Can I extract the functionality for dependency management into a more system wide script, that can be reused from all other projects, that need this dependency management? How?
You can write a plugin class, and ship it to builds as a Jar. For details, see the Gradle User Guide.
During compileJava I get the warning :compileJavawarning: [options] bootstrap class path not set in conjunction with -source 1.7
What does this mean, and how can I fix that.
This question has been asked and answered many times already. See Stack Overflow or http://forums.gradle.org.
PS: Instead of rewriting Ivy to Maven version range syntax, it should be possible to use Maven syntax from the start (i.e. in the build script).

How to set Gradle `options.bootClasspath` in an os independent manner?

Because my Java sources and targets must be JRE 1.6 compatible, I need to set options.bootClasspath to a path that contains the 1.6 versions of rt.jar and jce.jar. It must build on both Windows and Unix (Linux/Solaris). What is the proper way to do this? I now use the following approach in my top-level build.gradle, it works, but it seems far from elegant, especially the os-dependent separator : or ;:
import org.apache.tools.ant.taskdefs.condition.Os
subprojects {
apply plugin: 'java'
compileJava {
sourceCompatibility = 1.6
targetCompatibility = 1.6
def java6_home = System.getenv("JAVA_HOME_6")
def java6_lib = "C:/localdata/Program Files (x86)/Java/jdk1.6.0_45/jre/lib/"
if (java6_home != null) {
java6_lib = java6_home + "/jre/lib/"
}
def sep = ':'
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
sep = ';'
}
options.bootClasspath = java6_lib + "rt.jar" + sep + java6_lib + "jce.jar"
}
}
I am using the following code (assuming the JDK6_HOME points to the root of the JDK 1.6 installation):
tasks.withType(JavaCompile) {
doFirst {
if (sourceCompatibility == '1.6' && System.env.JDK6_HOME != null) {
options.fork = true
options.bootClasspath = "$System.env.JDK6_HOME/jre/lib/rt.jar"
options.bootClasspath += "$File.pathSeparator$System.env.JDK6_HOME/jre/lib/jsse.jar"
// use the line above as an example to add jce.jar
// and other specific JDK jars
}
}
}
This approach automatically detects the presence of the environment variable and automatically sets the bootClasspath for all modules that declare sourceCompatibility as 1.6.
The options.fork = true is required when you use bootClasspath.
The accepted answer can work, but if you're using some classes outside java.lang (e.g. javax.crypto.*) you may find you'll get various ClassNotFoundExceptionException's being raised as more JAR files need to be added to the bootClasspath.
To avoid this, I use the following which has the following advantages;
The options are only set if required
All the JAR files are added to the bootClasspath
The extensionDirs is also set (see http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#extdirs-option)
.
tasks.withType(JavaCompile) {
doFirst {
if (JavaVersion.toVersion(sourceCompatibility) == JavaVersion.VERSION_1_6
&& JavaVersion.current() != JavaVersion.VERSION_1_6
&& System.env.JDK6_HOME != null) {
options.fork = true
options.bootClasspath = fileTree(include: ['*.jar'], dir: "$System.env.JDK6_HOME/jre/lib/").join(File.pathSeparator)
options.extensionDirs = "$System.env.JDK6_HOME/jre/lib/ext/"
}
}
}
Since Gradle 4.3 you can use CompileOptions.bootstrapClasspath instead to remove the need for an OS-dependent separator.
A slight modification of the cool solution by Oleg Estekhin above, but doesn't require JDKX_HOME to be set (calculates it on the fly.) Also, modified for doing Java 1.7 builds:
tasks.withType(JavaCompile) {
doFirst {
if (sourceCompatibility == '1.7') {
def JDK7_HOME = "/usr/libexec/java_home -v 1.7".execute().text.trim()
options.bootClasspath = "$JDK7_HOME/jre/lib/rt.jar"
options.bootClasspath += "$File.pathSeparator$JDK7_HOME/jre/lib/jsse.jar"
// use the line above as an example to add jce.jar
// and other specific JDK jars
}
}
}
I tried to add many jars to my bootClassPath using the instructions above, but never resolved my build issue. I finally resolved the build by setting my JAVA_HOME to point to the IBM JDK 1.7 required by the WebSphere server and adding it to my path. My other projects require Oracle JDK 1.8 so I did not want to make this change permanent.
set JAVA_HOME="C:\Program Files (x86)\IBM\WebSphere\AppServer\java_1.7.1_64\"
set PATH=%JAVA_HOME%\bin;%PATH%
gradle clean war
gradle clean ear deployLocal

Gradle scala class already defined with combined Java compilation

I have the following scala compilation issue
scala -> depends upon java source
java source -> depends upon scala source
My scala code is in src/main/scala
My java code is in src/main/java
I cant change this code so I need to compile this with gradle and it currently compiles with JRuby just fine.
I have read the following posts on how to solve this issue:
http://forums.gradle.org/gradle/topics/how_to_compile_a_java_class_that_depends_on_a_scala_class_in_gradle
http://forums.gradle.org/gradle/topics/how_to_compile_a_java_class_that_depends_on_a_scala_class_in_gradle
I added this to my build:
ext {
baseName = 'd2'
description = 'Divisional IVR.'
combinedSources = "$buildDir/combined-sources"
}
apply plugin: 'scala'
compileScala.taskDependencies.values = compileScala.taskDependencies.values - 'compileJava'
compileJava.dependsOn compileScala
sourceSets.main.scala.srcDir "$combinedSources"
sourceSets.main.java.srcDirs = []
I tried to copy all the scala and java files to one location:
compileScala.dependsOn{
copyAllSourceFiles
}
task copyAllSourceFiles(type:Copy) {
description = 'Copy All Source Files.'
from('src/main/java') {}
from('/src/main/scala') {}
into combinedSources
includeEmptyDirs = false
}
But now I get an error:
[ant:scalac] Compiling 18 source files to C:\usr\git_workspaces\xivr\d2\target\classes\main
[ant:scalac] Compiling 18 scala and 196 java source files to C:\usr\git_workspaces\xivr\d2\target\classes\main
[ant:scalac] C:\usr\git_workspaces\xivr\d2\target\combined-sources\com\comcast\ivr\d2\actors\AlternateAniWithAccountActor.scala:9: error: AlternateAniWithAccountActor is already defined as class AlternateAniWithAccountActor
It almsot seems like scalaCompile sees $combinedSources and 'src/main/scala'
It almsot seems like scalaCompile sees $combinedSources and 'src/main/scala'
That's how you configured it: src/main/scala is the default, and you added "$combinedSources". To override the default, use sourceSets.main.scala.srcDirs = [combinedSources].
In any case, you don't have to (and shouldn't) copy sources around. Here is one solution that neither requires copying nor reconfiguring of task dependencies:
sourceSets.main.scala.srcDir "src/main/java"
sourceSets.main.java.srcDirs = []
Now, your Java and Scala code will get joint-compiled, and can depend on each other arbitrarily.
PS: Instead of "$combinedSources", use combinedSources.
gradle.properties
theVersion=2.1
theSourceCompatibility=1.7
theScalaVersion=2.10.3
build.gradle
apply {
plugin 'scala'
plugin 'java'
plugin 'idea'
}
ext {
scalaVersion = theScalaVersion
}
sourceCompatibility = theSourceCompatibility
tasks.withType(ScalaCompile) {
scalaCompileOptions.useAnt = false
}
dependencies {
compile "org.scala-lang:scala-library:$theScalaVersion"
compile "org.scala-lang:scala-compiler:$theScalaVersion"
}
sourceSets {
main.scala.srcDirs = ["src/main/scala", "src/main/java"]
main.java.srcDirs = []
}

Categories

Resources