How to add jvmArgs using kotlin dsl? - gradle

I need to open some javafx modules and I'm told to put it in the build.gradle.kts. I have no idea how to add jvmArgs to the run task with the kotlin DSL? Can someone show me how please.

You can configure default JVM arguments with JavaApplication extension:
plugins { application }
...
application {
applicationDefaultJvmArgs = listOf("-Xmx2048m")
}

Related

How to create a Jar file of a spring boot application using Intellij and gradle [duplicate]

I'm trying to build an executable jar in Spring Boot + Gradle project, but for now nothing works. Here is the simplest possible structure. Possibly, something is missing in Gradle configuration.
Gradle:
buildscript {
ext {
springBootVersion = '1.5.8.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
jar {
manifest {
attributes 'Main-Class': 'com.example.demo.DemoApplication'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
}
Main config file:
#RestController
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
#GetMapping(value = "/")
public String index() {
return "index";
}
}
When I ran the jar file like java -jar 1.jar, I got this exception:
[main] ERROR org.springframework.boot.SpringApplication - Applicati
on startup failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to proces
s import candidates for configuration class [com.example.demo.DemoApplication];
nested exception is java.lang.IllegalArgumentException: No auto configuration cl
asses found in META-INF/spring.factories. If you are using a custom packaging, m
ake sure that file is correct.
at org.springframework.context.annotation.ConfigurationClassParser.proce
ssDeferredImportSelectors(ConfigurationClassParser.java:556)
at org.springframework.context.annotation.ConfigurationClassParser.parse
(ConfigurationClassParser.java:185)
at org.springframework.context.annotation.ConfigurationClassPostProcesso
r.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:308)
at org.springframework.context.annotation.ConfigurationClassPostProcesso
r.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:228)
at org.springframework.context.support.PostProcessorRegistrationDelegate
.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.ja
va:272)
at org.springframework.context.support.PostProcessorRegistrationDelegate
.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:92)
at org.springframework.context.support.AbstractApplicationContext.invoke
BeanFactoryPostProcessors(AbstractApplicationContext.java:687)
at org.springframework.context.support.AbstractApplicationContext.refres
h(AbstractApplicationContext.java:525)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationConte
xt.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.
java:693)
at org.springframework.boot.SpringApplication.refreshContext(SpringAppli
cation.java:360)
at org.springframework.boot.SpringApplication.run(SpringApplication.java
:303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java
:1118)
at org.springframework.boot.SpringApplication.run(SpringApplication.java
:1107)
at com.example.demo.DemoApplication.main(DemoApplication.java:13)
Caused by: java.lang.IllegalArgumentException: No auto configuration classes fou
nd in META-INF/spring.factories. If you are using a custom packaging, make sure
that file is correct.
at org.springframework.util.Assert.notEmpty(Assert.java:277)
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelecto
r.getCandidateConfigurations(AutoConfigurationImportSelector.java:153)
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelecto
r.selectImports(AutoConfigurationImportSelector.java:95)
at org.springframework.context.annotation.ConfigurationClassParser.proce
ssDeferredImportSelectors(ConfigurationClassParser.java:547)
... 14 common frames omitted
What might be wrong?
In Boot 2.x, the bootJar and bootWar tasks are responsible for packaging the application.
The bootJar task is responsible for creating the executable jar file. This is created automatically once the java plugin is applied.
In case the executable jar/war file is not generated run the below gradle task manually.
$./gradlew bootJar
Similarly, bootWar generates an executable war file and gets created once the war plugin is applied.
We can execute the bootWar task using:
$./gradlew bootWar
Note that for Spring Boot 2.x, we need to use Gradle 4.0 or later.
I created a project with all the sources you provided. Running "gradle build" from terminal, switching to /build/libs and then running "java -jar artifactname" works just fine.
Have you tried to clean and recompile? Which Version of Gradle are you using?
In spring boot you can directly create executable jar file by
springBoot {
executable = true
}
Please try
jar{
baseName = 'myapp'
version = 'version'
}
It will create jar with name myapp-version.jar
Do ./myapp-version.jar from command line.it will execute
Refer following link for more info. https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html
I just recently tried a Spring boot application with 2.1.4.Release with Gradle build.
I ran the following command from the directory in Windows CMD.
gradlew clean build
(upon required JDK8 installed in the system), I was able to see the JAR generated under,
<project-directory>/build/libs/<project-name-version.jar>
Hope this helps though older question.
Reference:
My two cents.
When using spring-boot if you want to customize the MANIFEST.MF file, you need to set the bootJar task, it won't work on the default jar task.
bootJar {
manifest {
attributes 'Start-Class': 'com.baeldung.DemoApplication'
}
}
https://www.baeldung.com/spring-boot-main-class
If you're trying to make your .jar file executable, for use such as in a systemd service. You'll have to edit the bootJar task and enable launchScript.
build.gradle
bootJar {
launchScript()
}
or with Gradle Kotlin DSL build.gradle.kts
tasks {
bootJar {
launchScript()
}
}
You should now be able to run your project's .jar file as an executable.

adding option "--enable-https" to GraalVM gradle nativeCompile task

I am trying to build an executive via the compileNative task from a Spring Boot 3.0.0M5 project.
I need to add the --enable-https to the GraalVM compiler.
How is it possible to add that option using the gradle plugin?
id "org.graalvm.buildtools.native" version "0.9.14"
it is done by adding this to the build.gradle
graalvmNative {
binaries {
main {
buildArgs.add('--enable-https')
}
}
}

gradle: Let the tooling api model call depend on a task from buildscript

I have a build script where a task adds dependencies to a configuration. Then compileJava depends on that task.
plugins {
`java-library`
}
repositories {
mavenCentral()
}
val setupDeps = tasks.register("setupDeps") {
doLast {
dependencies.add("implementation", "org.clojure:clojure:1.10.3")
}
}
tasks.named("compileJava") {
dependsOn(setupDeps)
}
From the shell this works. But in intellij the tooling api of gradle is used to build the dependency model. And the tooling api does not know that it has to execute this task first. Is there any way to configure this? I could of course always dynamically add the dependencies during configuration phase but i don't want to do that because to find out what needs to be added is slow.
I found the answer myself.
The task name to dependOn is :prepareKotlinBuildScriptModel
So this solves the problem:
tasks.findByPath(":prepareKotlinBuildScriptModel")?.dependsOn(setupDeps)

how to add `intellij gradle plugin` to my custom plugin dependency

I create a intellij plugin using recommended gradle.
I want to use gradle projectResolve in my plugin then I extends my class from org.jetbrains.plugins.gradle.service.project.GradleProjectResolverExtension but it's not exist. I add bellow code to my gradle :
intellij {
plugins = ['org.jetbrains.plugins.gradle']
}
but I receive this error:
Cannot find builtin plugin org.jetbrains.plugins.gradle for IDE:
I add bellow library it's work but is too old.
implementation 'com.github.adedayo.intellij.sdk:gradle:142.1'
gradle plugin is bundled plugin and can add like
intellij {
plugins = ['gradle']
}
Another option is
plugins {
id 'java-gradle-plugin'
}

How to debug a Gradle project with jvm args in Netbeans?

I created a Gradle project in NetBeans (8.0.2), which uses database connections from my tnsnames.ora. So I added the line
applicationDefaultJvmArgs = ["-Doracle.net.tns_admin=${System.env.TNS_ADMIN}"]
to my build.gradle. (I use the java and application plugins)
This works when I run the project, but not for debugging. It looks like this setting is simply not used in debug mode.
How can I get this to run? Either in the build.gradle or in the NetBeans settings would be fine.
Here's how I got around this problem.
task(debug, dependsOn: 'classes', type: JavaExec)
{
main = mainClass
classpath = sourceSets.main.runtimeClasspath
debug true
jvmArgs = ["-DmyProperty=myValue"]
}

Resources