I have the following Gradle file:
I had this working fine, but all of the sudden it seems like this JPA package cannot be found. I have added it compile("org.springframework.boot:spring-boot-starter-dao-jpa")
Every time I try and build, it complains about this. I check the dependency graph and it has a red underline to show it's missing. What can I do?
buildscript {
ext {
springBootVersion = '2.0.1.RELEASE'
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven'
group = 'com.remindful'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
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("org.springframework.boot:spring-boot-starter-dao-jpa")
compile("com.h2database:h2:1.4.191")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile group: 'com.google.guava', name: 'guava', version: '11.0.2'
compile group: 'com.h2database', name: 'h2', version: '1.4.197'
testCompile 'junit:junit:4.12'
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.0.1.RELEASE'
}
// 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"]
}
There is no sign of spring-boot-starter-dao-jpa in the Spring Maven Repository, so I suspect it doesn't exist. Try removing the line.
Maybe this was a copy & paste error from the spring-boot-starter-data-jpa dependency two lines below...?
It looks like when I refactored my project structure, intellij got clever and renamed the dependencies. Be careful on that one!
Related
I try to use DefaultTask and can't add the org.gradle.api.* dependency.
I added dependency from the https://mvnrepository.com/artifact/org.gradle/api/1.0:
// https://mvnrepository.com/artifact/org.gradle/api
implementation group: 'org.gradle', name: 'api', version: '1.0'
but the project still does not "see" org.gradle... library:
gradle build execution result:
* What went wrong:
Execution failed for task ':compileJava'.
> Could not resolve all files for configuration ':compileClasspath'.
> Could not find org.gradle:api:1.0.
Searched in the following locations:
- https://repo.maven.apache.org/maven2/org/gradle/api/1.0/api-1.0.pom
Full build.gradle:
plugins {
id 'java'
}
test{
testLogging.showStandardStreams = true
}
repositories {
mavenCentral()
}
dependencies {
// https://mvnrepository.com/artifact/org.apache.commons/commons-email
implementation group: 'org.apache.commons', name: 'commons-email', version: '1.5'
// https://mvnrepository.com/artifact/org.gradle/api
implementation group: 'org.gradle', name: 'api', version: '1.0'
}
Why has the dependency doesn't been found?
From the official doc https://docs.gradle.org/7.4.1/userguide/custom_tasks.html#sec:custom_tasks_standalone_project, its build.gradle is very simple.
plugins {
id 'groovy'
// id 'java' <----- use this if written in Java
}
dependencies {
implementation gradleApi()
}
You don't have to specify org.gradle:api in dependencies{} nor mavenCentral() in repositories{}. The one you found in https://mvnrepository.com is a very old version. I guess they host the JARs themselves.
I have the following build.gradle file for my project
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.guava:guava:23.6-jre';
testCompile group: 'junit', name: 'junit', version: '4.12'
}
jar {
manifest {
attributes 'Main-Class': 'Runner.ClientRunner'
}
}
However, when I run "gradle jar" and attempt to run the given jar, I get the error:
java.lang.NoClassDefFoundError: com/google/common/base/Preconditions
I can't seem to nail down what I've done wrong here, guava is included in the dependencies for gradle and the jar file appears to build fine otherwise (it only crashes when it gets to the first class that depends on guava). Any assistance appreciated.
If it helps I'm doing this from IntelliJ.
I solved my problem using the solution at https://discuss.gradle.org/t/how-to-include-dependencies-in-jar/19571
My build.gradle now looks like
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
configurations {
// configuration that holds jars to include in the jar
extraLibs
}
dependencies {
compile 'com.google.guava:guava:23.6-jre';
extraLibs group: 'com.google.guava', name: 'guava', version: '23.6-jre'
testCompile group: 'junit', name: 'junit', version: '4.12'
configurations.compile.extendsFrom(configurations.extraLibs)
}
jar {
manifest {
attributes 'Main-Class': 'Runner.ClientRunner'
}
from {
configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) }
}
}
Full error is in this image as I am running on virtual machine in cloud which is access via video feed so I cannot copy and paste.This is a fresh install of ubuntu where I have only installed JDK and nothing else so unaware if other setup needs to be done
https://imgur.com/a/egJ3d
It is a spring boot application.
My build.gradle
group 'com.haughon.daniel'
version '1.0-SNAPSHOT'
buildscript {
repositories{
mavenCentral()
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE'
}
}
// Apply the Spring Boot plugin
apply plugin: 'spring-boot'
// Apply the Java plugin (expects src/main/java to be source folder)
apply plugin: 'java'
apply plugin: 'idea'
// Specify the location where our dependencies will be found
repositories {
mavenCentral()
}
jar {
manifest {
attributes 'Main-Class': 'haughton.dvdstore.Application'
attributes 'addClasspath': 'true'
}
}
// Specify dependencies
dependencies {
compile 'org.hashids:hashids:1.0.1'
compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
compile 'org.springframework:spring-orm:4.3.7.RELEASE'
compile 'org.hibernate:hibernate-core:5.2.9.Final'
compile 'org.hibernate:hibernate-entitymanager:5.0.6.Final'
compile 'org.apache.tomcat:tomcat-dbcp:8.0.30'
compile 'org.springframework.boot:spring-boot-starter-security'
compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4')
//compile 'org.thymeleaf.extras:thymeleaf-extras-springsecurity:3.0.2.RELEASE'
compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '1.11.1.RELEASE'
// https://mvnrepository.com/artifact/com.google.code.gson/gson
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.8.8'
runtime 'com.h2database:h2'
runtime 'javax.transaction:jta:1.1'
runtime 'org.aspectj:aspectjweaver:1.8.7'
testCompile 'org.springframework.boot:spring-boot-starter-test'
}
This is because your dependencies are not included in one jar file.
use ./gradlew clean build
please see this post:
java.lang.NoClassDefFoundError: when trying to run jar
Running the IDEA IDE I want to add a gradle dependency for the jUnit v5.
Here is my build.gradle file, I used this answer as a guide.
apply plugin: 'java'
sourceCompatibility = 1.8
repositories { mavenCentral() }
apply plugin: 'org.junit.platform.gradle.plugin'
dependencies {
testCompile 'junit:junit:4.12'
compile 'junit:junit:4.12'
testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0-M4")
testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M4")
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.0.0-M4")
// Enable use of the JUnitPlatform Runner within the IDE
testCompile("org.junit.platform:junit-platform-runner:1.0.0-M4")
compile ("org.junit.jupiter:junit-jupiter-api:5.0.0-M4")
}
sourceSets {
main {
java {
srcDir 'src'
}
}
}
junitPlatform {
details 'tree'
}
The problem here is that the jUnit4 annotations are resolved by import but all the v5 annotations are not resolved.
One example:
#ParameterizedTest
public void testExample() {
// My annotations is not resolved
}
What is the right way to add a jUnit5 dependency using gradle?
EDIT
I started a new gradle java project from scratch to get to the bottom of this.
Here is my current build.gradle.
group 'com.iay0361'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories { mavenCentral() }
apply plugin: 'org.junit.platform.gradle.plugin'
dependencies {
testCompile group: 'org.junit.vintage', name: 'junit-vintage-engine', version: '4.12.0-RC3'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0-RC3'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.0-RC3'
compile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.0.0-RC3'
testCompile group: 'org.junit.platform', name: 'junit-platform-runner', version: '1.0.0-RC3'
}
sourceSets {
main {
java {
srcDir 'src'
}
}
}
junitPlatform {
details 'tree'
}
I wrote the #Test annotation in a new class file under test after which it asked me to "add the 'jUnit5' to classpath
which I did and this time selected the Copy 'jUnit5' libraries to... instead of using the IDEA distributor.
Now it added these files in module:
The file is still RC2 but in build.gradle it is RC3.
There are also no jUnit jars in "External Library" directory
What am I missing, the problem is still that the IDE cannot resolve some jUnit5 annotations like #ParamiterizedTest.
Here is a quick sample on how to configure gradle with junit5. In your dependencies, remove the compile statement for the junit:4.12 artifact verison.
// If you also want to support JUnit 3 and JUnit 4 tests
testCompile("junit:junit:4.12")
In the buildscript() method, include these:
buildscript {
repositories { mavenCentral() }
dependencies { classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-RC3' }
}
So I'm just playing (experimenting) with gradle and spring-boot.
When I follow the hello world I easily get the project running which kind of makes me happy.
Now I'd like to have a structured project; For this I'm using Intellij Community (not sure if relevant). I have the following structure.
/Project
- build.gradle
- settings.gradle (only includes Services)
/Project/Services/
- build.gradle
- settings.gradle (only includes MyService)
/Project/Services/MyServices
- build.gradle
Now I can share some of my build.gradle files but I"m trying random things I find on the internet. My problem is that the spring boot classes are not available at MyService. The following directory structure inside the Myservice is standard Java (/src/main/java )
I'm trying to put the dependencies & versions in my main build.gradle if possible. Can someone point out what I'm doing wrong.
Currently I've only used gradle for simple android development work.
/Project/build.gradle
group 'nl.msegers.project'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
/Project/Services/build.gradle
group 'nl.msegers.project.services'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE")
}
}
/Project/Services/MyService/build.gradle
group 'nl.msegers.project.services.myservice'
version parent.version
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
jar {
baseName = 'Navigation'
version = parent.version
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
To have every project to inherit the dependencies from the root, you could use the allprojects script block on your root project like:
allprojects {
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.+'
...
}
}
I've been scouring the web for an hour or so to get a structure like this working. In the meanwhile I've rebuild my project from scratch as well (Though I did not have much code anyway).
My code currently allows simple Spring boot sub projects without too much redundant configuration, and they can pretty easily compile the Libraries project, however this can use some work.
What I've got now is a structure like this.
/Project
/Project/Libraries
/Project/Libraries/Models
/Project/Services/
/Project/Services/Service
With the only notable build.gradle files (others are nearly empty):
/Project/Services/
group 'nl.msegers.project.services'
version parent.version
buildscript {
ext {
springBootVersion = "1.5.2.RELEASE"
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
subprojects {
buildscript {
ext {
springBootVersion = "1.5.2.RELEASE"
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
dependencies {
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
compile("org.springframework.boot:spring-boot-starter-actuator")
compile project(':Libraries:Models')
testCompile 'junit:junit:4.12'
}
task wrapper(type: Wrapper) {
gradleVersion = '3.1'
}
}
/Project/Services/MyService
group 'nl.msegers.project.services.myservice'
version parent.version
jar {
baseName = 'myservice'
version = parent.version
}
repositories {
mavenCentral()
}