Added Gradle dependencies can not be imported in Java classes - java

I added the servlet api as a dependency in Gradle, but when I want to import it in my Java class, it does not work. Same thing with the Mockito Framework.
My gradle.build file Looks like this:
group 'Dontknownow'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.1.4-3'
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'java'
apply plugin: 'kotlin2js'
apply plugin: 'war'
sourceCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile 'org.mockito:mockito-core:2.+'
providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
}
This is how the error looks like in my IntelliJ:

Okey I solved it by myself.
I think the "+" in my build.gradle was the problem
Instead of using
testCompile 'org.mockito:mockito-core:2.+'
i entered an absolute version:
testCompile 'org.mockito:mockito-core:2.7.19'
and also there where alternative import-statements for Mockito on https://guides.gradle.org/building-java-web-applications/
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

Related

Unable to resolve org.springframework.boot:spring-boot-starter-thymeleaf

I'm just learning how to work with spring and I'm new in this. I tried to add thymeleaf to my project with Gradle dependency but It can not be resolved. I think my way of adding thymeleaf to project is wrong. What should i do?
This is my build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath('org.springframework.boot:spring-boot-gradle-plugin:2.2.4.RELEASE')
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
group 'com.aminsl'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.springframework', name: 'spring-webmvc', version: '5.1.9.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.2.4.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
}
I would go to https://start.spring.io/, select the dependencies I need as shown below:
And then Generate the project.
Using the website gives me the below gradle file:
plugins {
id 'org.springframework.boot' version '2.2.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
Looks like you are missing the right plugins in your build file.

Can't generate war - Java EE 8

After create a new Java EE project - in IntelliJ - new project -> Gradle from left list -> next -> next
I fill 2 files
build.gradle:
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'maven'
apply plugin: 'war'
apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'
group 'com.random'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
providedCompile group: 'javax.ws.rs', name: 'javax.ws.rs-api', version: '2.1'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
and
#Path("/reservation")
public class ReservationWebService {
#GET
public Response listReservations() {
return Response.ok("hello").build();
}
}
now I'm trying to generate war by:
gradle war in terminal and I got:
'gradle' is not recognized as an internal or external command,
operable program or batch file.
How to generate war ?

How to setup Amazon Polly SDK with Gradle in IntetlliJ

I am trying to setup AWS Polly Java SDK with Gradle in IntelliJ by following this. I have already created a simple Spring Boot application using the spring intializr, so I added the items specified in the tutorial to my build.gradle file. When try to import
import com.amazonaws.services.polly.AmazonPollyClient
IntelliJ fails to resolve name polly.
This is my full build.gradle file
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath "io.spring.gradle:dependency-management-plugin:1.0.3.RELEASE"
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'io.ai.vivid'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencyManagement {
imports {
mavenBom 'com.amazonaws:aws-java-sdk-bom:1.11.228'
}
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile 'com.amazonaws:aws-java-sdk-s3'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
Also IntelliJ complains that it can't resolve the name manvenBom in my build.gradle. I have already tried SO solutions to this like invalidate cache/restart but could not resolve the issue.
I used your build.gradle file to replicate the issue and was able to import AmazonPollyClient after making the following changes to the dependencies
dependencies {
compile 'com.amazonaws:aws-java-sdk-s3'
compile group: 'com.amazonaws', name: 'aws-java-sdk-polly', version: '1.11.67'
The Gradle version used 4.8

gretty appStart fails with unknown property 'classesDirs'

I'm trying to build a Java Servlet with gradle gretty plugin (gradle version 3.4).
I have a dependency to another project databaseprovider:1.0-SNAPSHOT (includes connection to the database and some spring definitions...).
When I run the task "gradle war" the .war-file is built properly with all dependencies in WEB-INF/lib.
But when I try to start with "gradle appStart" I have following problem:
What went wrong: Execution failed for task ':appStart'. Could not get
unknown property 'classesDirs' for main classes of type
org.gradle.api.internal.tasks.DefaultSourceSetOutput.
Caused by: groovy.lang.MissingPropertyException: Could not get unknown
property 'classesDirs' for main classes of type
org.gradle.api.internal.tasks.DefaultSourceSetOutput.
Without the dependency databaseprovider:1.0-SNAPSHOT the jetty starts without problems:
INFO Jetty 9.2.22.v20170606 started and listening on port 8080
build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.akhikhl.gretty:gretty:+"
classpath "org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE"
}
}
apply plugin:'java'
apply plugin: 'eclipse'
apply plugin: 'war'
apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'
repositories {
maven {
url "http://..."
}
mavenLocal()
}
dependencies {
compile('databaseprovider:1.0-SNAPSHOT'){
changing=true
}
}
gretty {
httpPort = 8080
contextPath = '/'
servletContainer = 'jetty9'
}
build.gradle from databaseprovider:
buildscript {
repositories {
maven {
url "http://..."
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
apply from: 'liquibase.gradle'
jar {
baseName = 'databaseprovider'
version = '1.0-SNAPSHOT'
}
repositories {
maven {
url "http://..."
}
mavenLocal()
jcenter()
}
configurations.all {
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa:1.4.0.RELEASE")
compile group: 'org.springframework.data', name: 'spring-data-jpa', version:'1.10.2.RELEASE'
compile group: 'org.hibernate', name: 'hibernate-c3p0', version: '5.0.9.Final'
testRuntime group: 'com.h2database', name: 'h2', version: '1.4.192'
compile group: 'ch.qos.logback', name: 'logback-classic', version:'1.0.13'
compile group: 'ch.qos.logback', name: 'logback-core', version:'1.0.13'
compile group: 'org.slf4j', name: 'slf4j-api', version:'1.7.5'
compile group: 'org.slf4j', name: 'jcl-over-slf4j', version:'1.7.5'
compile group: 'org.slf4j', name: 'log4j-over-slf4j', version:'1.7.5'
compile group: 'org.reflections', name: 'reflections', version: '0.9.11'
compile (group: 'com.mattbertolini', name: 'liquibase-slf4j', version: "1.2.1")
compile (group: 'org.liquibase', name: 'liquibase-core', version: "3.5.3")
compile group: 'javax.validation', name: 'validation-api', version: '1.1.0.Final'
testCompile("junit:junit")
runtime("mysql:mysql-connector-java:5.1.39")
testCompile("org.springframework:spring-test")
}
I was able to solve the problem by adding the version of gretty:
After I changed the classpath in build.gradle
classpath "org.akhikhl.gretty:gretty:+"
to
classpath "org.akhikhl.gretty:gretty:1.4.2"
(compatible version to gradle 3.4) jetty starts without exceptions.
I found this question by googling for the error message when I encountered this issue. For me, I found my answer in this Github thread. Basically, by upgrading my version of Gradle to 4.8 I was able to resolve the issue.

Could not find method suites() for arguments [src/test/resources/testSuite.xml] on project

I have created a project in gradle with testng framework, how to execute test-suite in testng using gradle, please help here is my build.gradle
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = 1.8
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart',
'Implementation-Version': version
}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
test {
systemProperties 'property': 'value'
}
test {
useTestNG() {
suites 'src/test/resources/testng.xml'
}
}

Categories