IntelliJ cannot resolve symbol 'spark' when importing Spark Java - java

I am currently running the latest IntelliJ and I am trying to use SparkJava Webserver for a Gradle project I'm writing.
When I do compile and run with gradle everything works fine, however I am getting an error in
import static spark.Spark.*;
Where IntelliJ cannot resolve the symbol spark, even though I obviously have the correct dependency set in my build.gradle:
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
implementation 'mysql:mysql-connector-java:8.0.27'
implementation "com.sparkjava:spark-core:2.9.3"
implementation "org.slf4j:slf4j-simple:1.7.9"
}
test {
useJUnitPlatform()
}
Is there any way to circumvent this?
Many thanks in advance.

I got it:
In IntelliJ, under View -> Tool Windows -> Gradle there's an option to refresh everything Gradle related at the following button:
Using this refreshes the cache and yields the correctly imported dependency.

Related

Issue while setting up the spring boot with gradle

I am trying to setting up a Spring boot gradle project in intelliJ. If I am trying with reload button on the gradle pane, it's giving me error for setting.gradle
but setting.gradle file is already there is the root directory.
When I tried ./gradlew clean build on terminal getting the below error:
build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '3.0.0'
}
group 'com'
version '1.0'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
java {
sourceCompatibility = JavaVersion.toVersion('11')
targetCompatibility = JavaVersion.toVersion('11')
}
I tried but couldn't figure out what I am missing. Please suggest.
Update: I tried to delete gradle installation from the system and executed one another application which was working fine earlier but not even after downloading the gradle version, getting the same error as mentioned above.
I tried Maven project with spring initializr and it successfully build so It seems to be an issue regarding gradle setup on my machine but not sure what to check.**
Regards
Not Sure why error message was not that clear. It was related to certificate issue. I added some organization specific certificate in java cacerts keystore and it resolved the gradle issue with plugin download.
Thanks to All who tried to help.

OpenApi Not Working with Spring Boot Gradle Project

I am trying to add OpenApi to my Spring Boot Gradle project. I've added the OpenAPI plugin to my build.gradle file, but when I start my app, none of the URLs that are supposed to make documentation seem to get generated.
build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '3.0.1-SNAPSHOT'
id 'io.spring.dependency-management' version '1.1.0'
id "org.springdoc.openapi-gradle-plugin" version "1.6.0"
}
group = 'com.sampleproject'
sourceCompatibility = '17'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
application.properties
server.servlet.context-path=/sample-service
springdoc.api-docs.path=/api-docs
springdoc.swagger-ui.path=/swagger-ui-custom.html
I have tried building and running my project with:
./gradlew clean build followed by java -jar build/libs/name-of-jar-file.jar
./gradlew clean generateOpenApiDocs
Links that I've been using:
https://springdoc.org/v2/#gradle-plugin
https://github.com/springdoc/springdoc-openapi-gradle-plugin
Both links say to add the plugin, which I did. When my app is running, I try going to http://localhost:8080/sample-service/api-docs and http://localhost:8080/sample-service/swagger-ui-custom.html, and both give me the standard spring white label error page.
I have two controller classes in my application, and all endpoints work fine when my app is running.
What am I missing or doing wrong in order to get OpenAPI to work with my Spring Boot Gradle application?
In order to enable swagger UI, you will need to add the plugin of
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.14'
Adding this to build.gradle dependencies will solve your problem. I hope this helps.

JUnit not recognized in IntelliJ

I am trying to integrate and setup the JUnit 5 libraries with IntelliJ on my linux distro. I added JUnit to my gradle, and built it using gradle. But I am still seeing an error on my unit tests prompting me to "add junit to my classpath" even though it already is.
Here is my build.gradle
id 'java'
id 'idea'
}
group 'com.techchallenge'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
testCompile('org.junit.jupiter:junit-jupiter-api:5.3.1')
testCompile('org.junit.jupiter:junit-jupiter-engine:5.3.1')
}
test {
useJUnitPlatform()
}
UPDATE
See screenshot. I added the dependency and it is still prompting me to add the junit to my classpath though I already did
UPDATE 2
I updated the build.gradle and also did gradlew clean build test which was successful. But it is still showing the error with my junit annotations- and keeps prompting me to add junit 5.4 to my classpath though it is there.
id 'java'
id 'idea'
}
group 'com.techchallenge'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
testCompile('org.junit.jupiter:junit-jupiter-api:5.4.2')
testRuntime('org.junit.jupiter:junit-jupiter-engine:5.4.2')
testRuntime('org.junit.vintage:junit-vintage-engine:5.4.2')
}
test {
useJUnitPlatform()
}
It should recognise your setup. You may try adding junit5 dependencies (which is how junit5 is added creating a Gradle project from scratch using IntelliJ's New Project option) as below then reload your Gradle Project.
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}
You may try File | Invalidate caches/Restart... option to get rid of odd issues if your project runs tests successfully using gradle test cli command
https://www.jetbrains.com/help/idea/invalidate-caches.html
You have to use the right imports for Junit 5, You are using Junit4 imports. Since version 5 #Test, Assertions, etc are within org.junit.jupiter.api package. e.g:
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class DummyTest {
#BeforeEach
void setUp() {
}
#AfterEach
void tearDown() {
}
#Test
void name() {
Assertions.assertTrue(true);
}
You may look into junit5-jupiter-starter-gradle
I had a similar problem with JUnit 5 using Maven. I discovered that IDEA had created some *.iml files where it should not have. I had one in the test folder called test.iml. After deleting them the problems went away.
Please check if IDEA did not create some sneaky modules unexpectedly.
I see in the screenshot that you have one of those:
My advise is to delete it.
Make sure you have correctly setup gradle inside your intellij like for maven
and make sure You have installed JUnit plugin for IntelliJ IDEA,
you can even point gradle to your local repository like
repositories {
flatDir {
dirs 'C:/path/to/local/directory'
}
}
dependencies {
compile name: 'name-of-jar'
}

How do you programmatically access resolved implementation dependencies?

I would like to access the metadata about a resolved implementation dependency that has its version set via a platform bom
dependencies {
implementation platform("internal.spinnaker:clouddriver-bom:${spinnakerRelease}")
annotationProcessor platform("internal.spinnaker:clouddriver-bom:${spinnakerRelease}")
testAnnotationProcessor platform("internal.spinnaker:clouddriver-bom:${spinnakerRelease}")
implementation "com.netflix.spinnaker.clouddriver:clouddriver-web"
implementation "com.netflix.spinnaker.clouddriver:clouddriver-core"
implementation "com.netflix.spinnaker.clouddriver:clouddriver-security"
}
I would like to be able to programmatically access the resolved version of clouddriver-web.
All the solutions I can find online keep referencing code like this.
configurations.compile.resolvedConfiguration.firstLevelModuleDependencies
Which you cannot do with the implementation configuration as it throws an error about not being resolvable.
The end goal is to have the resolved dependency be an entry in META-INF/build-info.properties so that it can be used like so: https://www.vojtechruzicka.com/spring-boot-version/
I can’t figure out how to get the version in a task or gradle file.
Any help would be greatly appreciated, cheers!
With Gradle 6.0, the following build.gradle:
plugins {
id 'java'
}
repositories {
jcenter()
}
dependencies {
implementation 'com.google.guava:guava:29.0-jre'
}
task log {
doLast {
println(configurations.compileClasspath.resolvedConfiguration.firstLevelModuleDependencies)
}
}
When run with:
./gradlew :log
Prints:
> Task :log
[com.google.guava:guava:29.0-jre;compile]
Any dependency in the implementation configuration will show up on the compile classpath, as shown by the following figure that I've taken from The Java Plugin.

Gradle not downloading test dependencies

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.

Categories