I'm trying to build tasks to allow me to specify my profile for my spring app using Gradle.
buildscript {
ext {
springBootVersion = '2.0.1.RELEASE'
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}
}
plugins {
id "com.diffplug.gradle.spotless" version "3.10.0"
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven'
spotless {
java {
googleJavaFormat()
licenseHeaderFile 'habicus.license.java'
}
}
group = 'com.remindful'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-jersey')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-web-services')
compile('org.springframework.boot:spring-boot-starter-websocket')
compile('org.springframework.session:spring-session-core')
compile("com.h2database:h2")
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile group: 'com.google.guava', name: 'guava', version: '11.0.2'
testCompile 'junit:junit:4.12'
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.0.1.RELEASE'
}
// TODO: Add prod profile in application-properties
task prod {
run { systemProperty "spring.profiles.active", "prod" }
}
task dev {
run { systemProperty "spring.profiles.active", "dev" }
}
// To force debug on application boot, switch suspend to y
bootRun {
systemProperties System.properties
jvmArgs=["-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"]
}
tasks.bootRun.dependsOn build
bootRun.mustRunAfter dev
I have two questions:
1) Intellij highlight the tasks with yellow squiggly underline, so I'm wondering if the syntax here is wrong?
2) Do I use gradle-wrapper for something like this or just gradle? I'm trying to better understand the difference.
1) Intellij highlight the tasks with yellow squiggly underline, so I'm wondering if the syntax here is wrong?
Unfortunately, IntelliJ does not support gradle completely, for example, the ext block in the build script is not recognized correctly, so when accessing members defined throught the ext block, IntelliJ fails to resolve its definition & type.
So there's no need for panic when IntelliJ displays a yellow underline, you only need to take notice of the errors reported by the gradle command. If gradle build says OK, then everything is fine.
Meanwhile, IntelliJ is unable to resolve third-party plugins statically, therefore it is also unable to recognize tasks and Task classes added by these plugins. In this case, it will also show this yellowish underline complaining about something like cannot infer argument type.
The solution is to refresh all Gradle projects, by clicking the refresh button on the gradle panel, and if your build script is written correctly, these underlines will most probably vanish. This is because IntelliJ embedds with gradle using the Gradle Tooling API, and during the gradle sync process (fired by refresh all Gradle projects), third-party plugins got resolved and the whole project object model is built, so that IntelliJ would know that nothing is wrong with your tasks.
If gradle build fails, then the problem is with your own build script. On how to write gradle build scripts & tasks correctly, see Authoring Tasks - Gradle User Manual.
2) Do I use gradle-wrapper for something like this or just gradle? I'm trying to better understand the difference.
The Gradle wrapper is explained in the official userguide. To be brief, I would quote:
The Wrapper is a script that invokes a declared version of Gradle, downloading it beforehand if necessary.
So if you don't have a wrapper script in your project, you should execute gradle wrapper to generate one, and then commit it to your VCS. After that, wherever you previously execute gradle <task> in the command line, you can/should replace it with ./gradlew <task> (in *nix environment) or gradlew.bat <task> (in Windows).
The main difference between using the gradle command directly from the command line and the gradle wrapper script is, that the gradle wrapper script will download the gradle binary and uses it to execute gradle builds if no installed gradle binary is found, while using the the gradle command will only result in an error in such case.
Also, when someone else is using your project, he/she can simply clone the repo and run ./gradlew build (in *nix) or gradlew.bat build (in Windows) and your project will build fluently and successfully, regardless of whether he/she has previously installed a gradle distribution.
Related
I'm using gradle 7.02 (and used the gradle init with the option to create a single java project using groovy as build language)
-VS Code version:
Version: 1.46.1
Commit: cd9ea6488829f560dc949a8b2fb789f3cdc05f5d
Date: 2020-06-17T21:17:14.222Z (11 mos ago)
Electron: 7.3.1
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0
OS: Darwin x64 18.7.0
I have the following plugins:
-Debugger for Chrome
-Debugger for Java
-ESLint
-Gradle Language Support
-Java Extension Pack
-Java Test Runner
-Language Support for Java by redhat
-Maven for Java
-Project Manager for Java
When I run a task in gradle to show the cache it shows the correct dependencies:
Task :app:showMeCache
/Users/xxx/.gradle/caches/modules-2/files-2.1/org.projectlombok/lombok/1.18.20/18bcea7d5df4d49227b4a0743a536208ce4825bb/lombok-1.18.20.jar
/Users/xxx/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/30.0-jre/8ddbc8769f73309fe09b54c5951163f10b0d89fa/guava-30.0-jre.jar
/Users/xxx/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-annotations/2.12.3/7275513412694a1aafd08c0287f48469fa0e6e17/jackson-annotations-2.12.3.jar
/Users/xxx/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-databind/2.12.3/d6153f8fc60c479ab0f9efb35c034526436a4953/jackson-databind-2.12.3.jar
/Users/xxx/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-core/2.12.3/deb23fe2a7f2b773e18ced2b50d4acc1df8fa366/jackson-core-2.12.3.jar
/Users/xxx/.gradle/caches/modules-2/files-2.1/com.google.guava/failureaccess/1.0.1/1dcf1de382a0bf95a3d8b0849546c88bac1292c9/failureaccess-1.0.1.jar
/Users/xxx/.gradle/caches/modules-2/files-2.1/com.google.guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/b421526c5f297295adef1c886e5246c39d4ac629/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar
/Users/xxx/.gradle/caches/modules-2/files-2.1/com.google.code.findbugs/jsr305/3.0.2/25ea2e8b0c338a877313bd4672d3fe056ea78f0d/jsr305-3.0.2.jar
/Users/xxx/.gradle/caches/modules-2/files-2.1/org.checkerframework/checker-qual/3.5.0/2f50520c8abea66fbd8d26e481d3aef5c673b510/checker-qual-3.5.0.jar
/Users/xxx/.gradle/caches/modules-2/files-2.1/com.google.errorprone/error_prone_annotations/2.3.4/dac170e4594de319655ffb62f41cbd6dbb5e601e/error_prone_annotations-2.3.4.jar
/Users/xxx/.gradle/caches/modules-2/files-2.1/com.google.j2objc/j2objc-annotations/1.3/ba035118bc8bac37d7eff77700720999acd9986d/j2objc-annotations-1.3.jar
When I ran the debug it showed it downloaded the dependencies when I ran gradle clean build --refresh-dependencies -debug
I was able to get the dependencies to work if I did a simple project using gradle init but for some reason when you do application vs code doesn't add the dependencies under java projects->Project and External Dependencies.
This is the build file for my app:
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
* User Manual available at https://docs.gradle.org/7.0.2/userguide/building_java_projects.html
*/
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
}
apply plugin: 'java'
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Use JUnit Jupiter API for testing.
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.1'
// Use JUnit Jupiter Engine for testing.
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
// This dependency is used by the application.
implementation 'com.google.guava:guava:30.0-jre'
implementation 'com.fasterxml.jackson.core:jackson-core:2.12.3'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.12.3'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.3'
compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.20'
}
application {
// Define the main class for the application.
mainClass = 'query.App'
}
tasks.named('test') {
// Use junit platform for unit tests.
useJUnitPlatform()
}
task showMeCache doLast() {
configurations.compileClasspath.each { println it }
}
This is the build file for the simple project that does download the dependencies properly in vs code:
apply plugin: 'java'
defaultTasks 'clean', 'compileJava'
repositories {
mavenCentral()
}
dependencies{
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-api:5.7.1'
implementation 'com.fasterxml.jackson.core:jackson-core:2.12.3'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.12.3'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.3'
}
test {
useJUnitPlatform()
}
I've searched online for solutions for vs code but can't seem to find anyone with my issue. What am I doing wrong?
but for some reason when you do application vs code doesn't add the
dependencies under java projects->Project and External Dependencies.
After adding an dependency, there would be a notification popping up to ask you
A build file was modified. Do you want to synchronize the Java classpath/configuration?
Click Now, your project will be rebuilt, then the dependency would be added to Project and External Dependencies. If not there, after rebuilding project, click the refresh button:
I am attempting to use VS Code for my Spring Boot MVC Java application. I am using the Gradle build tool. I have installed the Java Language Support extension from RedHat. Whenever I attempt to run my application using 'gradle bootRun' it never appears to stop executing buts stops at 75%.
Furthermore, whenever I stop the task (Ctrl+C), I received a message
the terminal process terminated with exit code: 2.
I have included Spring Boot's development tools as a dependency, but the application does not appear to either restart or reload. I have tried using the Force Java Recompilation command provided by the Java extension, but without success.
Has anyone been able to get Spring Boot, Gradle, and VS code to work well together?
Thanks.
EDIT:
Sorry, I should have included my build.gradle file.
buildscript {
ext {
springBootVersion = '2.1.0.BUILD-SNAPSHOT'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id "org.kravemir.gradle.sass" version "1.2.2"
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.midamcorp'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 10
task ts(type:Exec) {
workingDir "./"
commandLine 'cmd', '/c', 'tscCompile.bat'
}
bootRun {
args = ["--spring.profiles.active=dev" ]
}
sass {
main {
srcDir = file("$projectDir/src/main/sass")
outDir = file("$projectDir/src/main/resources/static/css")
exclude = "**/_*.scss"
minify = true
}
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-mail')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.postgresql:postgresql:42.2.4')
compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.4.0-b180725.0427'
compile('com.github.mkopylec:recaptcha-spring-boot-starter:2.2.0')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('org.springframework.boot:spring-boot-devtools')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
The behaviour is expected and I think bootRun is working correctly. Looking at the screenshot it certainly seems to have successfully started your web application.
The percentage that Gradle shows (75% in this case) is the progress for the whole build. For example, if the build is going to run 4 tasks and bootRun is the 4th task, the build will be 75% complete when the bootRun task is invoked as 3 out of 4 tasks have completed. The bootRun task doesn't end because it's hosting a web application and, therefore, will run indefinitely. This means that Gradle continues to show that the build is 75% complete as, from Gradle's perspective, the final task has not completed.
You may be able to configure your bootRun task with ignoreExitValue = true so that Gradle does not complain when you stop your web application with CTRL+C
I'm working on a Gradle project. The project downloads dependencies and runs perfectly fine when I do gradle run. Here's my build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'application'
mainClassName = 'myProject.MyMainClass'
repositories {
mavenCentral()
}
dependencies {
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:1.7.7'
compile 'org.jsoup:jsoup:1.7.2'
testCompile "junit:junit:4.11"
}
jar {
baseName = 'myproject-service'
version = '0.1.0'
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
Now I want to import the project in Intellij IDEA.
Import Gradle project >> Use default gradle wrapper (recommended) >> OK
However, when I hit OK it comes up with a dialogue saying:
Could not initialize class javax.crypto.SunJCE_b
Looking in the logs, it says
org.gradle.tooling.GradleConnectionException: Could not run build action using Gradle distribution 'https://services.gradle.org/distributions/gradle-2.3-bin.zip'.
...
Caused by: java.lang.NoClassDefFoundError: Could not initialize class javax.crypto.SunJCE_b
at javax.crypto.KeyGenerator.a(DashoA13*..)
...
2015-03-18 21:31:17,751 [2354198] WARN - nal.AbstractExternalSystemTask - Could not initialize class javax.crypto.SunJCE_b
com.intellij.openapi.externalSystem.model.ExternalSystemException: Could not initialize class javax.crypto.SunJCE_b
at org.jetbrains.plugins.gradle.service.project.AbstractProjectImportErrorHandler.createUserFriendlyError(AbstractProjectImportErrorHandler.java:106)
...
I have no idea how to get this project to work. None of my imports from external repositories work inside IDEA without this (they all appear red). Any help would be appreciated. I'm on:
Intellij IDEA 13.1.6
Java EE 7 (jdk1.7.0_75.jdk)
Gradle 2.3
Mac OS X
SOLUTION:
Still have no idea what the issue is. I tried all the suggestions I could find online and everything I could think of: Install a different version of java, install a different IDEA, try using the original cryptography policy files, try using the unlimited strength policy files and more. None of these worked.
What did work, however, was selecting "Use a local gradle distribution", instead of using the default wrapper. I gave it my gradle install path (/usr/local/gradle-2.3), found by running which gradle (that will give you the path to the executable, namely /usr/local/gradle-2.3/bin/gradle, but I just took the directory part). Now I can build using gradle!!
My best guess is that the gradle plugin installed some other distribution of gradle that wasn't set up properly for my project.
I hope this is helpful to anyone else struggling with using gradle in Intellij IDEA.
I want to try out Vaadin 7 with java 8, gradle 2.1, and IntelliJ 13.1.5 on Mac OSX. I installed gradle with brew. Here's what I've done so far
I created a project using the gradle wizard in Intellij
I set up tomcat like this Creating a Project with Intellij IDEA vaadin tutorial.
I've added the vaadin-gradle-plugin to my build.gradle file.
I hit the gradle refresh button
I ran vaadinCreateProject in the intellij gradle plugin menu
I made sure my JAVA_HOME and GRADLE_HOME point to correct locations
Everything works correctly with gradle commands, I ran vaadinRun and navigated http://localhost://8080 and saw the hello world. But IntelliJ has all identifiers highlighted red with "cannot resolve symbol". I've tried every form of recompiling and clearing caches.
How can I get Intellij recognize the code?
My gradle.build file.
apply plugin: 'java'
apply from: 'http://plugins.jasoft.fi/vaadin.plugin'
sourceCompatibility = 1.5
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
The Vaadin Gradle plugin injects the Vaadin dependencies at runtime which Intellij's JetGradle plugin does not pick up for some reason.
What you can do instead is use the Gradle idea plugin (apply plugin: 'idea' in your build.gradle) and run the idea task. After that import the project into Intellij and all dependencies should be on the classpath.
Whenever you add a dependency you need to run the idea task again so the classpath is updated.
According to JetBrains,
Vaadin plugin requires Gradle 3.0+
// Vaadin plugin requires Gradle 3.0+
task wrapper(type: Wrapper) {
gradleVersion = '3.0'
}
I am not sure your project will be OK with that spoon of groovy, but without it it shouldn't work.
I am very new with Gradle and I would like to download all my test dependencies using Gradle. I used gradle init to generate my build file, and copied some dependencis from my previous scripts. However upon using gradle --refresh-dependencies in the project root, the test dependencies still do not download.
I tried searching for answers as to why this happens, but they don't seem to fix my issue. Is there something wrong with my build file?
Particularly, I am after downloading mockito and hamcrest below.
build file:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.5'
testCompile "junit:junit:4.11"
testCompile "org.mockito:mockito-core:1.9.5"
testCompile "org.hamcrest:hamcrest-library:1.3"
}
test {
testLogging {
events 'started', 'passed'
}
}
task wrapper(type: Wrapper) { gradleVersion = '1.11' }
I am also using Eclipse if that helps.
EDIT: After adding the Gradle nature to my project, it seemed to work. Can anyone explain why?
As far as I understand, --refresh-dependencies makes sure that the already cached dependencies are ignored and that dependencies are re-downloaded when they're needed.
Just use gradlew build, or gradlew test, and gradle will compile your tests, and thus download the missing dependencies.
It seemed to work now, after I added the Gradle nature to my project, then cleaning and regenerating eclipse files.