Customize working directory run gradle run task / project structure for web application - gradle

I'm working on small web app and I want to serve static files. I'm using application plugin. Part of it is distribution plugin which copies src/main/dist files into distribution zip. Running application with gradlew run uses project dir as working directory. For this reason static files are not found.
Is there a simple way to change working directory for run task?
Is there any other recommended structure for web app gradle project setup?

I have had a similar case when running './gradlew run' of a Kotlin (console) app. The default working directory is the application directory, but IntelliJ uses the root project directory by default.
I have managed to change the working directory by setting the workingDir property of the run task. The list of properties can be found on https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Exec.html
application {
mainClass.set("myproject.HelloKt")
// Change the working directory for the run task. Here is an example:
tasks.run.get().workingDir = rootProject.projectDir
}

You need to add the following to your build.gradle file to modify the working directory of a gradle task.
File runningDir = new File('build/run/')
runningDir.mkdirs()
tasks.run.workingDir = runningDir
Check this thread for reference.

Related

Gradle Cucumber test generates build folder on daemon instead of project directory

I have a test automation project where basically I run cucumber test via gradle task. What's weird is that the build folder is generated on .daemon folder instead of the project directory. E.g.
/Users/my_user/.gradle/daemon/5.6
Whereas it should be on:
/Users/my_user/my_project/build
Weirdly enough this seems to only happen on my local. Is there anything I might have missed on setting up the gradle?

intelliJ debug not finding file in target folder

Hello I have an appengine module that won't work properly since I change my old appengine plugin to the new google cloud tools.
I have a module that is a child of my project.
I have a maven directive to copy some files from another child modules to the target folder of my module target/artifcat-name/WEB-INF.
The copy works.
But when I debug the appengine module in Intellij, I have a FileNotFoundException while trying to read the copied file from the target dir.
It worked fine before the update.
It also works fine when I run the devserver from the shell (mvn appengine:devserver.
I don't what I should change in Intellij to have it working again.
Ideas?
Thanks.

Executing gradle scripts from external directories

I need to execute groovy script with gradle, but the thing is that these scripts are located in external directory (let's say it's Desktop). I've heard, that in previous versions of Gradle (currently working on 3.2.1) it was not possible, since it is not part of the gradle project. I wonder if it is possible now, or do I have to copy all these scripts into some folder located in gradle project, to execute it.
User story:
I found script in external directory (based on some properties passed to console) - I have absolute path to the script
Now I want to execute this script (build.gradle) without copying it into my gradle project
I'm pretty green with gradle and I hope I made my point clear.
Thanks for help and every attempt :)
Is that what you're looking for? To run the script clone the repository, navigate to 42556631/project folder and run the command:
gradle -b ../script/build.gradle clean build
yes you need to move build.gradle file into project/Build Script folder and then run it.

How to make deployment of PHP files with Gradle

I have Android Studio project, where I store (in separate folder) PHP files (implementing web services). The whole project is built and maintained by Gradle plugin to Studio.
How to make Gradle to deploy (copy) these files from project to external Apache \htdocs localisation? Is it possible to copy theses files during Gradle build process? if so, how to copy them only if there are differences between files in project folder and files in external \htdocs localisation.
I will appreciate any help.
Assuming your Apache server runs on the same machine, you can create a Gradle copy task to copy single files or whole directories to a specific location:
task copyWebFiles(type: Copy) {
from 'src/web' // source directory
into '../path/to/htdocs' // target directory
}
preBuild.dependsOn copyWebFiles // execute task before build
As part of your app's build.gradle file.

problems running state machine examples

Congratulations on the spring state machine, I found it yesterday and have been trying it out, specifically the turnstile example running in STS. I found it very easy and intuitive to build a FSM.
Because spring shell doesn't work well in STS I tracked down the instructions to run the examples from the command line in the reference doc,
"java -jar
spring-statemachine-samples-turnstile-1.0.0.BUILD-SNAPSHOT.jar"
,
but running it got an error
"no main manifest attribute, in spring-statemachine-samples-turnstile-1.0.0.BUILD-SNAPSHOT.jar".
Although not even a novice in using gradle, I tried fixing this by adding this line to build.gradle in the jar section
"manifest.attributes['Main-Class'] = 'demo.turnstile.Application'"
(which doesn't handle the various sub-projects I know) but got this error
"NoClassDefFoundError: org/springframework/shell/Bootstrap".
If it is possible to run the samples from gradle, could you include them in the reference document? I tried running the samples using
gradle run
but it there was no interaction with the shell scripts.
Samples are designed to be run as executable jar and with shell so that you can interact without a need to recompile with every change. Your error indicates that you didn't build that sample jar as mentioned in docs.
./gradlew clean build -x test
This will automatically use spring boot plugin which will add the necessary jar manifest headers to jar meta info to make it a true executable jar. Essentially every every sample is a spring boot app.
Building SM sample projects in Windows Environment:
Open Command prompt (windows key + r -->cmd-->Enter), Change directory to project root folder spring-statemachine-master (Inside the Extracted folder).
Run gradlew install to get all spring dependencies copied to local machine.
Run gradlew clean build -x test to get the spring shell jars built. Courtesy Janne
These steps should ideally get all .jar built, look into \build\libs folder of respective sample project for jar files.
Run the like any other java jar file java -jar [jar-file-name.jar] (make sure to be change directory to jar file directory location).
One more thing where I was stuck was, How to give events to SM:
It's like this sm event EVENT_NAME_AS_DEFINED_IN_CLASS. Ref
E.g.: sm event RINSE --> to washer project

Resources