I am posting here to understand how does JHipster work with Gradle dependencies, in particular with regards to the fact that I am unable to copy some of them into a Gradle submodule I have created inside my JH project.
For example, the following doesn't work in a Gradle submodule
compile "junit:junit"
Error is
Could not resolve: junit:junit
However, the classic one copied from mvnrepository works great
compile group: 'junit', name: 'junit', version: '4.12'
Some additional information: I am creating a submodule that contains a set of classes related to testing, mainly a large load of custom Hamcrest matchers copied from another project from the Ant world. The original project had a lot of spaghetti code mess, so now I am refactoring into an isolated Gradle module. The testlib module shall depend on the testing frameworks and contain everything required for writing good tests. It can be compared to spring-test project you would use to write your own Spring-based tests.
At the moment, the gradle file looks like
plugins {
id "java"
}
configurations {
providedRuntime
implementation.exclude module: "spring-boot-starter-tomcat"
}
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
group 'org.example' //different from com.acme of super-project
version '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
dependencies {
compile group: 'org.assertj', name: 'assertj-core', version: '3.13.2'
compile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.2'
compile group: 'org.hamcrest', name: 'hamcrest', version: '2.1'
compile group: 'org.mockito', name: 'mockito-core', version: '3.0.0'
compile group: 'org.springframework.boot', name: 'spring-boot', version: spring_boot_version
compile "junit:junit" //Fails
}
Question
So the question is in two parts:
why does the scope "orgId:name" syntax work in the JHipster-generated module but not in submodules? Is it part of standard Gradle syntax?
why is that not working in a sub-module? Does JHipster apply a custom plugin to apply the correct version number that is clearly missing? How I do the same in a sub-module that is supposed to contain only Java library code?
With regards to JHipster, a little of more investigation helped. According to this answer, there is a trick in Gradle called Bill Of Materials project, so...
TL;DR
Add the following to the sub-project
// import JHipster dependencies BOM
implementation platform("io.github.jhipster:jhipster-dependencies:${jhipster_dependencies_version}")
So that the whole block looks like
dependencies {
// import JHipster dependencies BOM
implementation platform("io.github.jhipster:jhipster-dependencies:${jhipster_dependencies_version}")
compile "org.assertj:assertj-core"
compile "org.junit.jupiter:junit-jupiter-api"
compile "org.hamcrest:hamcrest"
compile "org.mockito:mockito-core"
compile "org.springframework.boot:spring-boot"
compile "junit:junit"
}
Long answer
Maybe in the future when I will understand Gradle more. Or just edit this answer 😁 to contribute
The bom defines the versions (besides other things) of 3rd party dependencies to be used so you can omit the explicit version. If you do not use the bom you can also write compile "junit:junit:4.12" but keep in mind jhipster uses already junit5 for all tests by default.
Regarding the import of the bom you can do it like you proposed or try to apply that dependency to all gradle subprojects in your main gradle file.
Related
I would like to use the most recent version of Dropwizard, unfortunately I cannot, because Gradle is unable to resolve it.
Here is my build.gradle file:
group 'com.gaboratorium'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
ext {
dropwizardVersion = '1.2.0'
}
repositories {
mavenCentral()
}
dependencies {
// Application
implementation "io.dropwizard:dropwizard-core:${dropwizardVersion}"
implementation "io.dropwizard:dropwizard-db:${dropwizardVersion}"
implementation "io.dropwizard:dropwizard-jdbi:${dropwizardVersion}"
implementation "io.dropwizard:dropwizard-auth:${dropwizardVersion}"
implementation "io.dropwizard:dropwizard-migrations:${dropwizardVersion}"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
Things I have tried:
Using jcenter repository instead
IntelliJ > Invalidate caches / Restart
Using an older version instead; the only one I could make work was 0.8.2
Did anyone experience something similar?
As it turned out my issue was that proxying was set up in my IntelliJ thanks to a previous project, which I was not aware of. However during my research for the problem I have found some relevant answers to this question, which I am going to place here for future reference:
IntellijIDEA not recognizing classes specified in Maven dependencies
Maven - can't download fasterxml.jackson
Gradle build doesn't download dependencies
I'm creating a project which uses the Squash SQL Library for Kotlin. I've added the dependency to my build.gradle file. When running the update it just finishes without outputting any errors. But the library is not getting imported in my project and doesn't appear at all.
The dependencies shown in IntelliJ:
My build.gradle file:
//Kotlin Stuff, nothing changed here
repositories {
mavenCentral()
maven {
url "http://dl.bintray.com/kotlin/squash"
}
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'org.jetbrains.squash:squash:0.2.2'
}
//Kotlin Stuff
The dependency you've added is just the parent-pom which doesn't have any jar's in the repo. Here is the list of the squash projects (http://dl.bintray.com/kotlin/squash/org/jetbrains/squash/):
squash-core
squash-graph
squash-h2
squash-jdbc
squash-postgres
squash-sqlite
I guess you want to import the squash-core so change
compile 'org.jetbrains.squash:squash:0.2.2'
to
compile 'org.jetbrains.squash:squash-core:0.2.2'
tl;dr; adding adding dependencies to build.gradle downloads it fine but doesn't add it to the classpath/external libraries in idea.
Hi guys
Im new to developing webapps in java, and im trying to depend on a few jars on mvnrepository.com, the only time the dependencies are downloaded into the external libraries and added to the classpath is when i import the project as a gradle project, as in, each time i have a project up and running and i add a new dependency i would have to import the whole project into intellij again.
my build.gradle file looks like this:
group 'project_name'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
// https://mvnrepository.com/artifact/com.google.inject/guice
compile group: 'com.google.inject', name: 'guice', version: '3.0'
// https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-core
compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-core', version: '9.0.0.M9'
// https://mvnrepository.com/artifact/com.sun.jersey/jersey-core
compile group: 'com.sun.jersey', name: 'jersey-core', version: '1.19.1'
// https://mvnrepository.com/artifact/com.sun.jersey/jersey-json
compile group: 'com.sun.jersey', name: 'jersey-json', version: '1.19.1'
// https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-client
compile group: 'org.glassfish.jersey.core', name: 'jersey-client', version: '2.23.2'
// https://mvnrepository.com/artifact/com.sun.jersey/jersey-servlet
compile group: 'com.sun.jersey', name: 'jersey-servlet', version: '1.19.1'
// https://mvnrepository.com/artifact/com.sun.jersey/jersey-server
compile group: 'com.sun.jersey', name: 'jersey-server', version: '1.19.1'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.5'
}
When i add a new dependency to the list, and run ./gradlew build, with or without the --refresh-dependencies option it does download the new dependencies but it doesn't add the downloaded files to the external libraries/classpath so i can't import them into the java code. I saw a question similar to this one, where they accepted answers like running:
./gradlew idea
In my case this doesn't help at all, it just adds some autogenerated files in the directory with no clear difference to behavior.
Then they accepted importing the project as a gradle project aswell, which i have done - which works, but adding new dependencies doesn't work.
FYI I am using the gradle 2.5 wrapper and IDEA community 16.2
Okay. I solved/figured it out, Apparently it didn't help to just run build,
inside of intellij i had to go to View --> Tool Windows --> Gradle, it then opens the gradle window, where i could click the refresh button, which downloads the dependencies.
Thanks to anyone who looked it over :)
In gradle you can achieve it using:
apply plugin: 'enhance'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.hibernate:hibernate-gradle-plugin:VERSION'
}
}
dependencies {
compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-[SPEC-VERSION]-api', version: '[IMPL-VERSION]'
compile group: 'org.hibernate', name: 'hibernate-gradle-plugin', version: 'VERSION'
}
What if instead of running the project through Gradle, I want to run my main class directly through Intellij (shift-F10). Is it possible to also perform build-time bytecode instrumentation just before the application run? How should I achieve this?
Hibernate does "bytecode instrumentation" at runtime, so you don't have to do anything special for it to happen.
Actually it is not bytecode instrumentation, which means changing existing classes, but proxying, which means, the existing classes are used by classes that get generated on the fly.
Say I want to add guice-assistedinject as a dependency in my project. It specifies the guice artifact as a dependency itself. How do I tell it to use the no_aop version of guice?
I know I can do the following, but can I do it in one step without excluding the guice module?
dependencies {
compile (group: 'com.google.inject.extensions', name: 'guice-assistedinject', version: '3.0') {
exclude module: 'guice'
}
compile group: 'com.google.inject', name: 'guice', version: '3.0', classifier: 'no_aop'
}
There is no simpler solution. You can shorten the code by using short dependency notation (e.g. "com.google.inject:guice:3.0:no_aop").