Dependency issues while using one gradle project in another - java

I am new to Gradle, basically from Maven background.
I have the following dependency in one project [Lets call it 'Project A']
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
Now I want to use 'Project A' in 'Project B'. I have done the following to refer Project A in Project B.
Updated the settings.gradle of Project B to include Project A
include ':project-a'
project(':project-a').projectDir = new File('path to Project A')
Following is added to Project B build.gradle
implementation project(":project-a")
While synchronizing, Project B complains the following.
Could not run phased build action using Gradle distribution
'https://services.gradle.org/distributions/gradle-6.6.1-bin.zip'.
Build file 'path to project A\build.gradle' line: 9 A problem occurred
evaluating project ':project-a'. Could not find method
implementation() for arguments [{group=org.apache.commons,
name=commons-lang3, version=3.0}] on object of type
org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
UPDATE*
Java plugin added as requested in comment
apply plugin: 'java'
//plugins {
// id 'java'
//}
dependencies {
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
implementation group: 'org.springframework', name: 'spring-context', version: "${spring_version}"
implementation group: 'org.springframework.boot', name: 'spring-boot-autoconfigure', version: "${springboot_version}"
implementation group: 'org.springframework.cloud', name: 'spring-cloud-context'
implementation group: 'org.springframework.cloud', name: 'spring-cloud-commons'
versions are declared in gradle.properties file
spring_version=5.2.5.RELEASE
springboot_version=2.2.6.RELEASE
servlet_version=3.1.0
Now none of the dependencies are getting downloaded.

Related

Micronaut: Graalvm: Native image tracing agent not working

I have been developing MS in Micronaut, which I want to run as Graalvm native image on AWS Lambda. I have few dependencies like Jackson ObjectMapper, Apache POI, etc. in the project which does use reflection framework. But, Graalvm doesn't support reflection framework out of the box. So, we need to provide the configuration for the classes which are using reflection framework. To generate such configuration, I am trying to use Graalvm tracing agent, but it doesn't seems to be working.
It generates the config JSON files successfully.
But these config JSONs doesn't contain the classes from 3rd party libs which are added in build.gradle.
I have below parameter in gradle.properties file:
org.gradle.jvmargs=-agentlib:native-image-agent=config-output-dir=<path to dir>
Then, I run the project using gradle's run task from the IntelliJ, and do execute all the methods which actually use these libs (thus all those libs' code gets executed).
Once the execution is stopped, I find few config files generated on the dir path mentioned, but those files contains only classes related to Gradle, Jetbrains and few others. I cannot find any classes related to 3rd party libs which I am using and which are mentioned in build.gradle.
What should be the correct way to generate the reflection configuration JSON file for Micronaut Graalvm native image?
build.gradle:
plugins {
id("com.github.johnrengelman.shadow") version "7.1.2"
id("io.micronaut.application") version "3.5.1"
}
version = "0.1"
group = "com.base.package"
repositories {
mavenCentral()
}
ext {
orgJsonVersion = "20220320"
poiVersion = "3.9"
apacheCommons = "3.12.0"
googleGson = "2.9.1"
springValidation = "2.7.3"
eclipseLink = "3.0.3"
servletApi = "2.5"
velocity = "1.7"
velocityTools = "2.0"
kafkaClient = "2.1.1"
googleGuava = "27.0-jre"
}
dependencies {
annotationProcessor('io.micronaut:micronaut-inject-java')
annotationProcessor("io.micronaut.data:micronaut-data-processor")
annotationProcessor("io.micronaut:micronaut-http-validation")
implementation("io.micronaut:micronaut-http-client")
implementation("io.micronaut:micronaut-jackson-databind")
implementation("jakarta.annotation:jakarta.annotation-api")
runtimeOnly("ch.qos.logback:logback-classic")
// runtimeOnly('org.slf4j:log4j-over-slf4j:2.0.2')
implementation("io.micronaut:micronaut-validation")
implementation("io.micronaut.data:micronaut-data-hibernate-jpa")
implementation("io.micronaut.sql:micronaut-jdbc-hikari")
compileOnly("io.micronaut.sql:micronaut-hibernate-jpa")
runtimeOnly("mysql:mysql-connector-java")
compileOnly("org.graalvm.nativeimage:svm")
// compileOnly group: 'io.micronaut', name: 'micronaut-http-server-netty'
implementation("io.micronaut.aws:micronaut-function-aws")
implementation("io.micronaut.aws:micronaut-function-aws-custom-runtime")
implementation group: 'org.json', name: 'json', version: "${orgJsonVersion}"
implementation group: 'org.apache.poi', name: 'poi', version: "${poiVersion}"
implementation group: 'org.apache.poi', name: 'poi-ooxml', version: "${poiVersion}"
implementation group: 'org.apache.commons', name: 'commons-lang3', version: "${apacheCommons}"
implementation group: 'com.google.code.gson', name: 'gson', version: "${googleGson}"
// implementation group: 'org.springframework.boot', name: 'spring-boot-starter-validation', version: "${springValidation}"
implementation group: 'org.eclipse.persistence', name: 'eclipselink', version: "${eclipseLink}"
// compileOnly group: 'javax.servlet', name: 'servlet-api', version: "${servletApi}"
implementation("org.apache.velocity:velocity:${velocity}") {
exclude group: 'log4j', module: 'log4j'
}
implementation("org.apache.velocity:velocity-tools:${velocityTools}") {
exclude group: 'log4j', module: 'log4j'
}
implementation group: 'org.apache.kafka', name: 'kafka-clients', version: "${kafkaClient}"
implementation group: 'com.google.guava', name: 'guava', version: "${googleGuava}"
// implementation group: 'org.hibernate', name: 'hibernate-core', version: '5.6.9.Final'
implementation group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5', version: '2.8.3'
implementation group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.8.3'
}
application {
mainClass.set("com.base.package.MainApplication")
}
shadowJar {
mergeServiceFiles()
}
java {
sourceCompatibility = JavaVersion.toVersion("11")
targetCompatibility = JavaVersion.toVersion("11")
}
graalvmNative.toolchainDetection = false
micronaut {
runtime("lambda_provided")
testRuntime("junit5")
processing {
incremental(true)
annotations("com.base.package.*")
}
}
tasks.named("dockerfileNative") {
args(
"-XX:MaximumHeapSizePercent=80",
"-Dio.netty.allocator.numDirectArenas=0",
"-Dio.netty.noPreferDirect=true"
)
}
graalvmNative.binaries.all {
buildArgs.add("--no-server -J-Xmx12g -J-Xms4g -O0")
}
Since this is something you rarely do I usually run the command directly against the fat jar.
Here is how I do it.
Run ./gradlew build. This creates the fat jar in builds/libs
Goto build/libs
Run java -jar -agentlib:native-image-agent=config-output-dir=META-INF/native-image <your jar name>-all.jar

Need to include org.json packages in MVC portlet to parse JSON in Liferay 7

Iam having a MVC portlet created using gradle, I want to parse JSON in it, so I included following packages like below,
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.JSONValue;
Also I have included org.json in build.gradle file as you can see below:
dependencies {
compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "3.0.0"
compileOnly group: "com.liferay.portal", name: "com.liferay.util.taglib", version: "2.0.0"
compileOnly group: "javax.portlet", name: "portlet-api", version: "3.0.0"
compileOnly group: "javax.servlet", name: "javax.servlet-api", version: "3.0.1"
compileOnly group: "jstl", name: "jstl", version: "1.2"
compileOnly group: "org.osgi", name: "osgi.cmpn", version: "6.0.0"
//compile group: 'com.squareup.okhttp', name: 'okhttp', version: '2.6.0'
compile group: 'org.json', name: 'json', version: '20180813'
compile group: 'org.apache.clerezza.ext', name: 'org.json.simple', version: '0.4'
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5'
}
Compiling was successful but while deploying it in Liferay(7), Iam getting the below error:
While deploying in Liferay am getting this error:
org.osgi.framework.BundleException: Could not resolve module: mvc [952]_ Unreso
lved requirement: Import-Package: org.json; version="[20180813.0.0,20180814.0.0)
Should I include the package org.json inside liferay as well? If yes where should I add it?
I couldn't add any new packages Please anyone help me on this.
When you declare build-time dependencies to libraries that are not made available in Liferay out of the box (be it for the library itself, or for the specific version), the compiler will be happy, and bnd will generate (if required) runtime dependencies into your module's Manifest.mf.
If indeed these dependencies result in runtime dependencies, you'll have to make the libraries available to the OSGi runtime (Liferay in this case) as well. You do that in the same way you're deploying your own modules: Just drop them into Liferay's deploy folder.

Gradle uses latest version of TestNG instead of declared version

In my gradle script I have following dependencies:
dependencies {
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version:'3.9.1'
compile group: 'io.github.bonigarcia', name: 'webdrivermanager', version: '2.2.4'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.6.2'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.6.2'
compile group: 'org.uncommons', name: 'reportng', version:'1.1.4'
compile group: 'com.google.inject', name: 'guice', version:'4.0'
compile group: 'org.apache.commons', name:'commons-math3', version: '3.6.1'
testCompile group: 'org.testng', name: 'testng', version:'6.14.3'
testCompile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.25'}
As seen here I ask to use 6.14.3 as the testng version. Only when I try to run my script I get the issue:
Execution failed for task ':compileJava'.
I first checked if the Java version is correct, but no issues there. Only if i run gradle --debug. I found that testng jar downloaded was of version 7.0.0-beta4. But as I did not declare this I'm not sure why this is used or I can force to use the correct one?
Running ./gradlew dependencies should show you the following compile dependencies (amongst many others):
…
+--- org.uncommons:reportng:1.1.4
| +--- org.testng:testng:[5.0,) -> 7.0.0-beta6
| \--- velocity:velocity:1.4
| \--- velocity:velocity-dep:1.4
…
In other words, your declared compile dependency on org.uncommons:reportng:1.1.4 has a transitive dependency on TestNG which Gradle resolves to the latest TestNG version – since you don’t explicitly declare any other TestNG dependency for the compile configuration. Note that you only directly depend on TestNG from the testCompile configuration!
You should be able to fix this by changing your TestNG dependency declaration as follows:
compile group: 'org.testng', name: 'testng', version:'6.14.3'

gradle unable to find artifact in maven repository

I am doing a simple gradle build and I have deployed a particular artifact to my internal sonatype maven repo
My build.gradle file looks like this:
apply plugin: 'java'
sourceCompatibility = 1.8
version = '1.0'
repositories {
mavenCentral()
maven {url "http://maven.ebay.com/nexus/content/repositories/sre-snapshots/"}
}
dependencies {
compile group: "com.typesafe", name: "config", version: "1.3.0"
//compile project(':metrics')
compile group: 'com.ebay.telemetry', name: 'client-library', version: '0.1-SNAPSHOT'
compile group: "org.slf4j", name: "slf4j-jdk14", version: "1.7.12"
testCompile group: 'junit', name: 'junit', version: '4.11'
}
I run this command on the command line: gradle build
I get this error:
> Could not find net.alchim31:metrics-influxdb:0.7.1-ebay-SNAPSHOT.
Searched in the following locations:
https://repo1.maven.org/maven2/net/alchim31/metrics-influxdb/0.7.1-ebay-SNAPSHOT/maven-metadata.xml
http://maven.ebay.com/nexus/content/repositories/sre-snapshots/net/alchim31/metrics-influxdb/0.7.1-ebay-SNAPSHOT/maven-metadata.xml
http://maven.ebay.com/nexus/content/repositories/sre-snapshots/net/alchim31/metrics-influxdb/0.7.1-ebay-SNAPSHOT/metrics-influxdb-0.7.1-ebay-20150708.054833-4.pom
http://maven.ebay.com/nexus/content/repositories/sre-snapshots/net/alchim31/metrics-influxdb/0.7.1-ebay-SNAPSHOT/metrics-influxdb-0.7.1-ebay-20150708.054833-4.jar
However when I go to http://maven.ebay.com/nexus/content/repositories/sre-snapshots/net/alchim31/metrics-influxdb/0.7.1-ebay-SNAPSHOT/
I see the following files :
metrics-influxdb-0.7.1-ebay-20150708.054830-3.pom
metrics-influxdb-0.7.1-ebay-20150708.054830-3.jar
So why is gradle looking for metrics-influxdb-0.7.1-ebay-20150708.054833-4.jar when my repo has metrics-influxdb-0.7.1-ebay-20150708.054830-3.jar
It seems like you have declared another dependency besides the one you showed above. It should probably look something like this:
compile group: 'net.alchim31', name: 'metrics-influxdb', version: '0.7.1-ebay-SNAPSHOT'
If you find this dependency declaration, you might also find the source of the problem.
The second possibility is, that your maven-metadata.xml (at http://maven.ebay.com/nexus/content/repositories/sre-snapshots/net/alchim31/metrics-influxdb/0.7.1-ebay-SNAPSHOT/maven-metadata.xml) is messed up and points towards the nonexisting build 4.

Gradle build multi modules project with transitive dependencies

I have problem with building my simple app. It contains 3 modules, ejb, rest and ear in which rest should be included. To achieve it I wrote build.gradle as one below. But I still have problems. Built ear looks like:
ear-1.0.ear
|--ejbs-1.0.jar
|--rest-1.0.jar
|--lib
|--ejbs-1.0.jar
|--other libs..
As you can see, I have here duplicated ejbs-1.0.jar. It's no something I want so I have tried to work this around. I have tried 2 approaches I found on web but neither of them worked. First one (comments with label 1) excluded all rest dependencies from going into lib dir. Second (label 2) did the same but also included rest-1.0.jar into lib, making it even worse.
Now I have no idea how to write my build.gradle so it puts jars made from subprojects in root dir, and their dependencies in lib dir. I have also tried to write something like comments with label 3, but this makes script fail. Is there easy way to excluded it like that with similar syntax?
project(":ejbs") {
apply plugin: "java"
dependencies {
compile group: 'javax.ejb', name: 'javax.ejb-api', version: '3.2'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.2'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.2'
}
}
project(":rest") {
apply plugin: "java"
dependencies {
compile project(':ejbs')
compile group: 'javax.ejb', name: 'javax.ejb-api', version: '3.2'
compile group: 'javax.ws.rs', name: 'jsr311-api', version: '0.11'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.2'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.2'
}
}
project(":ear") {
apply plugin: "java"
apply plugin: "ear"
dependencies {
//1: def nonTransitive = {transitive = false}
deploy project(":ejbs")
deploy project(":rest")
earlib project(path:":ejbs", configuration:"compile")
earlib project(path:":rest", configuration:"compile")//1: , nonTransitive
//2: add('earlib', project(':rest')) {
// transitive = false
//}
//3: earlib project(path:":rest", configuration:"compile") {
// exlude project(':ejbs')
//}
}
}
I finally found a solution. I made it this way:
project(":ear") {
apply plugin: "java"
apply plugin: "ear"
dependencies {
deploy project(":ejbs")
deploy project(":rest")
earlib project(path:":ejbs", configuration:"compile")
earlib(project(path:":rest", configuration:"compile")) {
exclude module: 'ejbs'
}
}
}

Categories