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"]
}
Related
I want to define two different versions of the bootRun task in a Spring boot application using Gradle. This is my attempt, which worked for customizing a single bootRun task. But with multiple tasks containing bootRun, the last bootRun overrides the previous ones.
task local {
bootRun {
systemProperty "spring.profiles.active", "dev,postgres"
jvmArgs "--add-opens=java.base/java.util=ALL-UNNAMED", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"
}
}
local.finalizedBy bootRun
task prod {
bootRun {
systemProperty "spring.profiles.active", "postgres"
jvmArgs "--add-opens=java.base/java.util=ALL-UNNAMED"
}
}
prod.finalizedBy bootRun
Here, when I run ./gradlew :local, the spring profiles are only postgres. When I comment out the prod task, I get both dev,postgres as expected.
As currently written, your local and prod tasks are default tasks that aren't really doing anything. Your use of bootRun within their configuration is altering the configuration of the existing bootRun task that Spring Boot's Gradle plugin defines.
When you define the tasks, you need to tell Gradle that they are a BootRun task:
task local(type: org.springframework.boot.gradle.tasks.run.BootRun) {
// …
}
You also need to configure the main class name and the classpath of your new task. You probably want those to be the same as the default bootRun task:
task local(type: org.springframework.boot.gradle.tasks.run.BootRun) {
mainClass = bootRun.mainClass
classpath = bootRun.classpath
}
You can then customize the system properties and JVM arguments as you were before. Putting this all together, you can configure your local and prod tasks like this:
task local(type: org.springframework.boot.gradle.tasks.run.BootRun) {
mainClass = bootRun.mainClass
classpath = bootRun.classpath
systemProperty "spring.profiles.active", "dev,postgres"
jvmArgs "--add-opens=java.base/java.util=ALL-UNNAMED", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"
}
task prod(type: org.springframework.boot.gradle.tasks.run.BootRun) {
mainClass = bootRun.mainClass
classpath = bootRun.classpath
systemProperty "spring.profiles.active", "postgres"
jvmArgs "--add-opens=java.base/java.util=ALL-UNNAMED"
}
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")
}
I am trying to follow this example to do spring boot and spring boot dev tools integration to do automatic restart. The classes in the build folder are getting updated when i run build --continuous task but the application still talks to the old classes. In the example the bootRun task is as below. My project has its custom task for running the application. Right now with build -continuous when I make a change the application it is rebuilding the classes but the running application is not showing the changes. How to change my custom h2Run task so that it loads the changed classes? Thank you.
The boot run task in the example
bootRun {
classpath = sourceSets.main.runtimeClasspath + configurations.dev
}
My custom task for bootRun
class Run extends JavaExec {
Run() {
group "application"
dependsOn project.tasks.classes, project.tasks.pathingJar
classpath = project.files("$project.buildDir/classes/main", "$project.buildDir/resources/main", project.tasks.pathingJar.archivePath)
main = "com.mycompany.Application"
}
}
task h2Run(type: Run) {
classpath = sourceSets.main.runtimeClasspath + configurations.dev // this is not working
description "Start $appName using H2 database"
args "--spring.profiles.active=dev"
mustRunAfter 'cleanH2'
dependsOn copyContentTypeLibraries
}
I walked through the DZone article you linked to. I didn't add your custom Run class or task, I just grabbed the bootRun task right out of the article. Even without any of your custom code, I initially experienced the same behavior you do.
The article states:
At the first terminal, start Gradle build as a continuous task:
gradle build --continuous
At the second terminal, start the Gradle bootRun task: gradle
bootRun
If I do these things, in this order, I also see my classes recompile, but the servlet container doesn't pick the changes up. Just like you describe.
However, if I do gradle bootRun first, and gradle build --continuous second, after the application is running, the application restarts as expected whenever I edit a java file.
Have you tried executing the commands in the two terminal windows in reverse order?
The problem I was having, was that I wanted to include test classpath resources in SpringBoot's bootRun gradle task. Why? So that I could use a test profile with test resources, to mock integration points.
What I tried:
The spring boot documentation only offers the addResources = true option (I tried using customConfiguration as per the similar bootRepackage configuration, to no avail)
No additional options are visible by looking at the BootRunTask source code
The equivalent maven plugin has a plethora of options, including useTestClasspath (which isn't mirrored in the gradle version)
I came across the following solution, which solved this issue for me.
Basically, the BootRunTask extends the standard JavaExec task, which offers a classpath option. So, you can add the test classpath resources by using the following gradle configuration:
bootRun {
classpath = sourceSets.test.runtimeClasspath
}
I'm trying to debug a simple gradle Java app in IntelliJ and I'm having the hardest time. I can run my tasks from the JetGradle plugin just fine, but when I right click the task and try to debug it, I get a message in the console that says it's connected, and then it says the equivalent disconnected:
Disconnected from the target VM, address: '127.0.0.1:59303', transport: 'socket'
Not really sure how to proceed from here. I've tried looking online etc and haven't really found much except people talking about how JetGradle plugin isn't very good.
Here is my build.gradle if it matters.
apply plugin: 'java'
apply plugin: 'idea'
task(parse, dependsOn: 'classes', type: JavaExec) {
main = 'com.test.creator.Main'
classpath = sourceSets.main.runtimeClasspath
}
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.code.gson:gson:2.2.4'
compile 'org.xerial:sqlite-jdbc:3.7.2'
compile 'net.sourceforge.jexcelapi:jxl:2.6.12'
}
Any help or direction would be appreciated.
A few additional things to note:
I'm using the gradle Daemon as it dramatically decreased my build time with certain projects, so I'd prefer to keep that on.
I'm using the Gradle Wrapper.
I'm running IntelliJ 12.1.6
I'm not sure if right-clicking the Gradle task and hitting Debug is the right solution (you might be debugging Gradle here, rather than your application). The easiest solution is to just run/debug the com.test.creator.Main class directly (from IntelliJ).
PS: Gradle support is much better in IntelliJ 13 (EAP).