Gradle Subproject Not Importing Dependencies from Local build.gradle File - java

I inherited a JVM* project at work that contains a settings.gradle file but no build.gradle file and no Gradle Wrapper.
I have created a new Java w/ Spring Boot project from the start.spring.io initializer. I installed this new project, check_forecast as a subproject of the root project validation_checks. This subproject has a build.gradle file, but no settings.gradle file.
To clarify, the root project, validation_checks is the legacy project that was built before my time at the company. When I unzipped the project boilerplate for the new project, check_forcast that I created from start.spring.io, I placed it in the root directory of the legacy project validation_checks and tried to configure check_forcast as a subproject of validation_checks.
When I try to build the check_forecast model using the command gradle clean build, I get errors that the subproject doesn't recognize essential dependencies like spring-boot-starter-jdbc despite these dependencies being clearly defined in the subproject's build.gradle file. Please see my config files below.
* The project contains Java, Kotlin, and Groovy code. It's a mess.
settings.gradle file for the root project, validation_check:
rootProject.name = 'validation_checks'
include 'check_forecast`
build.gradle file for the sub project, check_forecast:
plugins {
id 'application'
id 'org.springframework.boot' version '2.4.1'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}
group 'com.company.processes'
version '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-mail'
runtimeOnly 'mysql:mysql-connector-java'
}
test {
useJUnitPlatform()
}
Example of the errors I'm getting when I try to build the subproject:
> Task :nyiso_subscription_check:compileJava FAILED
C:\Users\onlydean\Workspace\validation_checks\check_forecast\src\main\java\com\company\check_forecast\domain\RefHourMap.java:3: error: package javax.persistence does not exist
import javax.persistence.*;
^
C:\Users\onlydean\Workspace\validation_checks\check_forecast\src\main\java\com\company\check_forecast\domain\RefHourMap.java:164: error: cannot find symbol
#Embeddable
^
symbol: class Embeddable
location: class RefHourMap
. . .
I can't figure out why my subproject can't see the dependencies defined in it's own build.gradle file. Can anyone point me in the right direction?

Related

Java Modules, Gradle project - Problems with external libraries

I have a multi module project in IntelliJ, a java library project, using Gradle with quite old versions of libraries
I must say, and now switching to Java 11 (using right now OpenJDK 11.0.2 from https://jdk.java.net/archive/).
I want to modularize that library project, adding to all modules a module-info.java.
I keep getting an error in one of the modules with one of the dependencies, Saxon-HE.
I isolated that module in a separate project (using Gradle 7.6), and modified the build.gradle dependencies step by step as IntelliJ discovered
import errors, while using the latest versions of the dependencies.
The build.gradle of the project looks like this:
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
test {
useJUnitPlatform()
}
The build.gradle of the module looks like this up to the point with the error with Saxon-HE.
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'
implementation group: 'org.jdom', name: 'jdom2', version: '2.0.6.1'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
implementation group: 'commons-io', name: 'commons-io', version: '2.11.0'
implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.6'
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.10.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
test {
useJUnitPlatform()
}
The module-info.java looks like this, I used the recommendations of IntelliJ so it added automatically the requires statements:
module mymodule1 {
requires org.apache.commons.lang3;
requires org.apache.commons.io;
requires org.slf4j;
requires okhttp3;
requires okio;
requires java.xml;
requires org.jdom2;
}
The next import error IntelliJ discovers while building it results because one of my classes has the import statement:
import net.sf.saxon.xpath.XPathFactoryImpl;
Building the project results in the error:
error: package net.sf.saxon.xpath does not exist
import net.sf.saxon.xpath.XPathFactoryImpl;
Looking at https://mvnrepository.com/artifact/net.sf.saxon/Saxon-HE/11.4 I added to build.gradle of the module:
implementation group: 'net.sf.saxon', name: 'Saxon-HE', version: '11.4'
In IntelliJ I can see in the Project navigator view, in External Libraries that there is
Gradle: net.sf.saxon.Saxon-HE:11.4
Saxon-HE-11.4.jar
IntelliJ recommends
Add 'requires Saxon.HE' directive to module-info.java
The module-info.java looks now like this:
module mymodule1 {
requires org.apache.commons.lang3;
requires org.apache.commons.io;
requires org.slf4j;
requires okhttp3;
requires okio;
requires java.xml;
requires org.jdom2;
requires Saxon.HE;
}
After that the error in the particular class using that import statement is gone, IntelliJ doesn't complain.
But when then building the project I get the error
C:\Users\ME\PROJECTS\myproject\mymodule1\src\main\java\module-info.java:9: error: module not found: Saxon.HE
requires Saxon.HE;
^
Removing the requires Saxon.HE and building the project results in the error:
error: package net.sf.saxon.xpath is not visible
import net.sf.saxon.xpath.XPathFactoryImpl;
^
(package net.sf.saxon.xpath is declared in the unnamed module, but module net.sf.saxon.xpath does not read it)
I find this error message weired, because it says but module net.sf.saxon.xpath does not read it, I would rather expect but module mymodule1 does not read it.
I don't know what's going wrong, other external dependencies are not problematic but Saxon-HE is.
I found here Gradle build - add module path a snippet which might solved it, but maybe only partially, so not sure if this answer can be marked as the solution.
I added to the project build.gradle
subprojects {
apply plugin: 'java'
sourceCompatibility = 19
targetCompatibility = 19
compileJava {
doFirst {
options.compilerArgs += [
'--module-path', classpath.asPath,
'-Xmaxerrs', 1000
]
classpath = files()
}
}
}
Now trying to build it (with Gradle 7.6 and OpenJDK 19), it doesn't complain to not finding modules, so far at least, but now I have the next problem which I desribed here Java Modules, Gradle, external dependencies - Modules reading from more then one

IntelliJ doesn't recognize class of gradle dependency

I have the following problem. I added the AnimateFX dependency via "Dependencies" tab in IntelliJ IDEA when I try to use one of the classes, it won't recognize it. Intellisense wont't list it either.
IntelliJ Screen Shot
Does anyone have an idea, what the actual problem here is?
To reproduce:
Create new JavaFX project via File - New - Project... choose Gradle as Build System and jdk-15(I downloaded it via IntelliJ) as Project SDK:
Project Setup
Click Next, don't add any dependencies shown in the following dailog: Dependencies Setup
Click Finish
After project configuration is finished, open gradle.build and add the following implementation statement to dependencies
implementation 'io.github.typhon0:AnimateFX:1.2.1'
Load gradle changes
Go to a .java class file that is in the src folder and try to use any class of the AnimateFX dependency, e.g.:
FadeIn fadeInTransition = new FadeIn(anyNode);
IntelliJ should now give an error stating the following: Error
IntelliJ version: IntelliJ IDEA 2021.2.2 (Community Edition) - Build #IC-212.5284.40
Gradle version: 7.1.1
Also, this is the content of my gradle.build:
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.10'
}
group 'de.mwllpr'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
sourceCompatibility = '15'
targetCompatibility = '15'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
application {
mainModule = 'de.mwllpr.application'
mainClass = 'de.mwllpr.application.GameApplication'
}
javafx {
version = '15.0.1'
modules = ['javafx.controls', 'javafx.fxml']
}
dependencies {
implementation 'org.apache.logging.log4j:log4j-core:2.14.1'
implementation 'io.github.typhon0:AnimateFX:1.2.1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testImplementation 'org.mockito:mockito-core:3.12.4'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
test {
useJUnitPlatform()
}
and module-info.java:
module de.mwllpr.application {
requires javafx.controls;
requires javafx.fxml;
requires org.apache.logging.log4j.core;
requires org.apache.logging.log4j;
opens de.mwllpr.application to javafx.fxml;
exports de.mwllpr.application;
}
Zipped demo project: http://www.filedropper.com/demofx

Adding custom maven dependency to gradle project [duplicate]

This question already has answers here:
How to add a Maven project as a Gradle dependency?
(3 answers)
Closed 2 years ago.
I have created dependency jar using maven project but now i have to add this maven dependency into my gradle project.
Depencency available in my .m2 directory
Am getting the below error from intellij .
Execution failed for task ':compileJava'.
> Could not resolve all files for configuration ':compileClasspath'.
> Cannot convert URL 'com.example.auth.security:common:0.0.1-SNAPSHOT.jar' to a file.
Please find my build.gradle file
plugins {
id 'org.springframework.boot' version '2.1.16.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'war'
}
group = 'com.example.auth'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation files('com.example.auth.security:common:0.0.1-SNAPSHOT.jar') --> getting error on this line.
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
compileOnly 'org.projectlombok:lombok'
implementation('io.jsonwebtoken:jjwt:0.9.1')
}
Update 1
A problem occurred evaluating root project 'auth-center'.
> Supplied String module notation '0.0.1-SNAPSHOT' is invalid. Example notations: 'org.gradle:gradle-core:2.2', 'org.mockito:mockito-core:1.9.5:javadoc'.
You will need to add a local maven repository like this
repositories {
maven { url new File(pathToYourM2Directory).toURI().toURL() }
}
Additionally the declaration of the dependency is not correct. It should be
dependencies {
implementation group: 'com.example.auth.security', name: 'common', version '0.0.1-SNAPSHOT'
}
You can as well fix the files dependency. However using a local maven repo is more sustainable as by resolving artifacts this way it is transparent for the build process if an artifact is resolved locally or remote.
Can't comment since I don't have sufficient reputation. I believe you shouldn't be trying to add the maven dependency to the gradle project. Instead, host the maven dependency elsewhere and configure gradle to pull dependencies from there. For reference, you can take a look at this answer
How to add a Maven project as a Gradle dependency?
This is another way to import your custom java library(.jar).
What you need to do is that first, make a folder wherever you want under your project, in my case /src/lib, and put JAR file into that folder. Then, write down the below code into your Gradle file.
dependencies {
//Library Auto Implement
//Replace with your folder URI
implementation(fileTree("./src/lib"))
}
Then Gradle will implement your JAR files from that folder, and you are ready to go.

Gradle multi-module. Check context of first module with EventListener from another module

I have two modules, first runs Spring boot Application and second it is EventListener which loads files from resources when context starts. All this modules works well separately but I wanna to include event listener module to my first module (Spring boot module) to get all files from resource of my first module when it runs context.
My main module with setting.gradle:
allprojects {
buildDir = file("${rootDir}/build")
group = 'com.example'
version = "0.1.1"
}
subprojects {
apply plugin: 'java'
apply plugin: 'maven'
}
setting.gradle
rootProject.name = 'test-application'
include 'bootApplication'
include 'eventListener'
project(":eventListener").projectDir = file("C:/examples/eventListener")
My bootApplication.gradle:
plugins {
id 'org.springframework.boot' version '2.2.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group 'com.example.bootApplication'
version = "0.1.1"
sourceCompatibility = '11'
targetCompatibility = '11'
repositories {
jcenter()
mavenLocal()
mavenCentral()
}
bootJar {
baseName("bootApplication")
}
jar {
enabled = true
}
dependencies {
compile project(":eventListnere")
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'io.springfox:springfox-swagger2:+'
implementation 'io.springfox:springfox-swagger-ui:+'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
And my eventListener:
plugins {
id 'org.springframework.boot' version '2.2.1.RELEASE'`enter code here`
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group 'com.example.eventlistener'
version '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
targetCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
ext {
spring_boot_version = '2.2.1.RELEASE'
}
implementation "org.springframework.boot:spring-boot-starter:$spring_boot_version"
compileOnly 'org.projectlombok:lombok:1.18.8'
annotationProcessor 'org.projectlombok:lombok:1.18.8'
testImplementation "org.springframework.boot:spring-boot-starter-test:$spring_boot_version"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
jar.enabled = true
When I run my bootApplication main class it creates a eventlistener-.jar file in root build directory. But eventlistener module doesn't check the resource folder, I guess it doesn't see a bootApplication context. Maybe it should be collect to one jar file? It looks like I missed something in gradle build files.
I will just prefix this by saying I don't know if the stuff below is the actual cause of your problems. But you should probably change a few things related to the jar configuration no matter what.
The Spring Boot Gradle plugin is used to create a fat jar out of the project. By default it disables the normal jar task.
You are re-enabling the normal jar task through jar.enabled = true, which is fine. But you also need to give it another name as one will otherwise override the other. For instance, for your eventListener project, you could do this:
// eventListener/build.gradle
bootJar {
classifier = 'boot'
}
However, if the eventListener is not actually a stand-alone executable, there is no need to create a boot jar from it. So unless you are using the plugin for other things, I would remove it from the eventListener completely:
// eventListener/build.gradle
plugins {
// id 'org.springframework.boot' version '2.2.1.RELEASE'`enter code here` <-- Remove this
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
You can still use the Spring Boot starters in the project, you just don't need the plugin for repackaging the jar.
The same thing applies to your bootApplication project: you are both trying to create a fat executable jar at the same time as a normal jar. One will override the other. In this case, you probably don't need the normal jar, so you should disable the jar task again:
// eventListener/build.gradle
// jar.enabled = true <-- Remove this
Lastly, replace compile project(":eventListnere") with implementation project(":eventListener") and testCompile with testImplementation to avoid some deprecation warnings. The maven plugin is deprecated as well in favor of maven-publish. You can probably also get rid of mavenLocal() unless you are integrating with local Maven projects that you build yourself with mvn install.
The eventListener, if packaged correctly as a normal jar inside the fat jar of the bootApplication, should be able to access resources in both its own resource folder as well as the one from bootApplication when you run the latter.

Unable to Import From main Java Directory Into test Directory (IntelliJ)

After I attempted to add the AssertJ library to my project for testing, some configuration in my project changed and I cannot import any classes from my main package into my JUnit test package.
My Main Class runs as expected, but I am unable to run any tests requiring Classes from the main package.
I've tried the following:
Removing AssertJ import
Rebuild Project
Refresh All Gradle Projects
Invalidate IntelliJ cache an Restart from Settings
Deleting project locally and re-downloading from GitHub
Removing testing directory and re-adding
Looking at another project that is working it appears to be an issue with my build.gradle file
group 'carpecoin'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.2.41'
ext.junitJupiterVersion = '5.0.3'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.3'
}
}
apply plugin: 'java'
apply plugin: 'kotlin'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
testImplementation group: 'junit', name: 'junit', version: '4.12'
// JUnit Jupiter API and TestEngine implementation
testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}")
testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}")
// To avoid compiler warnings about #API annotations in JUnit code
testCompileOnly('org.apiguardian:apiguardian-api:1.0.0')
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
implementation 'io.reactivex.rxjava2:rxjava:2.1.1'
implementation 'com.google.firebase:firebase-admin:6.0.0'
implementation 'com.google.firebase:firebase-database:15.0.0'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
The line below in the build.gradle file was failing causing the issue above.
implementation 'com.google.firebase:firebase-database:15.0.0'
I updated Firebase to the following and now it is still causing an issue when running tests. It appears to run fine in production:
implementation 'com.google.firebase:firebase-core:16.0.1'

Categories