In my springmvc project, I use gradle as a build tool.
But our FE developers always complain about that why they have to start the project in IntelliJ IDEA, but they don't use the IDE as their editor at all.
so, recently I made a lot of research on running my project from command line.
Please help.
If gradle is used and all is configured well, try gradle run while using the application plugin.
You do need to load the plugin
apply plugin: 'application'
and define the main class of your application
mainClassName = "org.gradle.sample.Main"
Further parameters can be defined as well.
Related
I have struggled to find a clean way to work with gradle-built modular Java FX projects (using JDK11), that can produce nice deployable jlink images, whilst still being able to perform Junit tests within the Eclipse IDE. I have:
A JavaFX app in my package in the src/main/java folder alongside the necessary info-module.java file.
JUnit tests in a corresponding package in the src/test/java folder, which obviously doesn’t contain an info-module.java because Eclipse doesn’t tolerate >1 module per project.
A gradle.build file with:
plugins { id 'java-library' id 'application' id 'eclipse' id 'org.openjfx.javafxplugin' version '0.0.12' id 'org.beryx.jlink' version '2.12.0'}
dependences {testImplementation 'junit:junit:4.13'}
javafx { version = "16" modules = [ 'javafx.controls', 'javafx.fxml' ]}
mainClassName = "{my-package}.App"
eclipse { classpath {file {whenMerged {entries.findAll {it.properties.kind.equals('lib') }.each {it.entryAttributes['module'] = 'true'}}}}}
jlink {options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages'] launcher { name = 'mini'}}
With this configuration, I can easily run my JavaFX app from within Eclipse (using gradle -> application -> run task) and could build a lovely deployable jlink image (using the jlink task). However, I could not run JUnit tests within my IDE, which I often like to do to test bits of code and code ideas as I go along – perhaps partly because I’m a bit of an amateur, albeit one with a fair bit of experience.
Having scoured the internet for solutions, I’ve ended up trying various things like adding “requires junit” to the info-module.java file, fiddling with build path configurations to add a JUnit4 library to the Module Path (which would obviously disappear every time I refreshed gradle), and trying JUnit5. I’ve found nothing satisfactory at all. Surely Gradle (or perhaps an Eclipse plugin therefor) should have a neat solution? Is there a one-size-fits-all solution here?
After fiddling around, my solution is:
To allow JUnit tests to work:
a. Comment out all content within the module-info.java file – this temporarily yields a non-modular project.
b. Comment out the eclipse {…} plugin commands in build.gradle – this temporarily breaks the eclipse modular behaviour and jlink task.
c. Refresh Gradle.
You can then run JUnit tests, and carry on working on your Project, so long as you don’t need modularity or jlink tasks.
To restore modularity and jlink tasks for image building:
a. Uncomment out (restore) the content of the module-info.java file.
b. Uncomment out (restore) the eclipse {} plugin commands in build.gradle.
c. Refresh Gradle.
This isn’t too much of a pain, so long as it’s practical to work without modularity whilst performing the JUnit tests. However, please let me know if there’s a way to avoid all this faffing – perhaps there’s a Gradle (or Eclipse plugin) solution?
From this post (Best way to add Gradle support to IntelliJ Project) I can see that what I need to do is "Add build.gradle in your project's root directory."
I was hoping someone could please explain how I do that in intelliJ? (been searching and reading - still baffled).
I understand a root directory the folder which is the parent for all the project sources, but in standard JavaFX project in intelliJ what is it/how do I find it/assign it, and then how do I add build.gradle?
Note: these steps assume that you are using the latest JDK version (17).
In the project browser, right click on the project name and create a file named build.gradle:
Write a build script. Here is a template for JavaFX applications:
plugins {
id "application" // Use Application plugin
id "org.openjfx.javafxplugin" version "0.0.9" // Use JavaFX plugin
}
mainClassName = "my.package.Application" // Set this to your main class
repositories {
mavenCentral()
}
javafx {
version = "16" // JavaFX Version
modules = [ "javafx.controls" ] // JavaFX modules. Add them to this array.
}
Once you have finished, import the project:
This may take a while depending on your internet speed.
You may have to adjust runtime configurations to use Gradle, but that shouldn't be necessary.
You may encounter the following error:
BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 61
If that is the case, install Gradle CLI (if you haven't already), and open a command prompt/terminal in the project folder.
Run the following command (works on Windows/Mac/Linux):
gradle wrapper --gradle-version=7.3 --distribution-type=bin
Once you have done that, reload the Gradle project.
I could not resist another GIF:
You may also need to rearrange the source folders:
I apologise for the cat photo, but it was just a placeholder image. I definitely didn't specifically choose it.
We already started our project in mavens(spring mvc) but realized gradle is better for managing big to enterprise level builds like our project. Is there a way to keep mavens but also add on gradle to the same build?
And if yes, how would I add gradle to my existing maven project? thanks.
I guess you need to make a decision:
Invoke maven from gradle
Invoke gradle from maven
If your end goal is to move to gradle then I suggest invoking maven from gradle. It looks like there's a plugin here which will do the job
I have the following build.gradle file:
apply plugin:'application'
mainClassName = "MyMain"
allprojects {
sourceCompatibility = 1.7
targetCompatibility = 1.7
}
repositories {
mavenCentral()
}
dependencies {
compile 'org.jsoup:jsoup:1.8.3'
}
So when I do gradle run it works perfectly, it uses jsoup and so.
Now, I've imported this project at my Intellij Idea IDE, I created a gradle build run/debug configuration, so when I press the green arrow 'run', it's building the application, but not running it, so I guess it's executing gradle build.
I've searched for an way to run the application, and the Intellij Idea says that I must create a new Application run/debug configuration. I did it, told where my main class was, but when I click run, it tries to make the source code, and claims that it can't find Jsoup for compiling.
How can I mimic gradle run in intellij idea's IDE?
You need to delegate IDE build/run actions to Gradle
Open the gradle window in Intellij. View->Tool Windows->Gradle.
On the right hand side you will see a list of all your gradle tasks. Double click to run or right click and assign a shortcut to any of them.
Look for the Gradle Tool Window. The rest is self-explanatory :)
Do not forget to click "Refresh all Gradle projects" in that tool window each time you change the build script.
Fairly new to Gradle. Newer to Spring. I understand the creation of Gradle tasks, how to compose them, and how to create Groovy plugins but it seems that there's more going on than what's in the project.
I've got a Spring REST service with a build.gradle file but I notice that once everything syncs, there are a TON of tasks in my Gradle plugin that I can't seem to find anywhere in the project. Such as: cleanEclipse, installApp, startScripts and many others.
Are these added by something more global? If so, what is it that defines the creation of all of these tasks?
IntelliJ IDEA 14.0.1
Gradle 2.2.1
Windows 7
The extra tasks that you see look like Gradle tasks are injected into the project by various plugins.
For example, the eclipse plugin includes tasks such as eclipse, cleanEclipse etc.
Here is a listing of all the standard Gradle plugins that come bundled with Gradle: https://www.gradle.org/docs/current/userguide/standard_plugins.html
Each of these has a list of tasks/properties that it injects into the project. Third party plugins would also do the same and their corresponding documentation should have information regarding these.
I think it can be added if others plugins are applied in your project, for example eclipse plugin or application plugin.