How to point gradle to local dependency that can have different paths? - java

To make it more clear, i have a code in my jar that when run it downloads needed jars to function correctly and it stored them in /wherever/jar/is/located/newFolder/libs/. Now what i have done is added those dependencies that are manually downloaded as compileOnly in my gradle but i dont know how to point gradle to location where the downloaded libs are since it can be different path on each machine that is run.
The dependency in question is io.sentry that i download to libs when the jar runs but how to tell my jar where to find classes if they are in libs folder as i previously stated.
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compileOnly 'org.jetbrains:annotations:15.0'
compileOnly 'io.sentry:sentry:1.7.30'
}

org.jetbrains:annotations and io.sentry:sentry are available publicly online. There's no need to download the artifacts manually. You can simply do:
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compileOnly 'org.jetbrains:annotations:15.0'
compileOnly 'io.sentry:sentry:1.7.30'
}
But if for some reason you must import the artifacts locally, then you can do something like:
repositories {
flatDir {
dirs '/wherever/jar/is/located/newFolder/libs/'
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compileOnly 'org.jetbrains:annotations:15.0'
compileOnly 'io.sentry:sentry:1.7.30'
}
Or include them one by one:
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compileOnly files('/wherever/jar/is/located/newFolder/libs/libs/jetbrains.jar')
compileOnly files('/wherever/jar/is/located/newFolder/libs/libs/sentry.jar'
}

Related

Gradle error when deploying service builder in liferay

I am experiencing an error when trying to run my custom service builder on my localhost. Gradle gives this error:
MESSAGE Problem with deploying bundle: gogo: BundleException: Could not resolve module: com.liferay.trotspots.web [1569]
Unresolved requirement: Import-Package: com.liferay.asset.constants; version="[1.1.0,2.0.0)"
weirdly however in the module com.liferay.trotspot.web I am not importing com.liferat.asset.constants. This is my build gradle file for the module:
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'eclipse'
sourceCompatibility = 1.8
targetCompatibility = 1.8
//Need for Windows
def defaultEncoding = 'UTF-8'
dependencies {
compile group: "biz.aQute.bnd", name: "biz.aQute.bndlib", version: "3.5.0"
compile group: "org.apache.poi", name: "poi"
compile group: "org.apache.poi", name: "poi-ooxml"
compile group: "org.apache.poi", name: "poi-ooxml-schemas"
compileOnly group: "com.liferay.portal", name: "release.portal.api"
compileOnly group: "com.liferay", name: "com.liferay.application.list.api"
compileOnly group: "com.liferay", name: "com.liferay.asset.api"
compileOnly group: "com.liferay", name: "com.liferay.asset.display.page.api"
compileOnly group: "com.liferay", name: "com.liferay.asset.display.page.item.selector.api"
compileOnly group: "com.liferay", name: "com.liferay.asset.info.display.api"
compileOnly group: "com.liferay", name: "com.liferay.asset.taglib"
compileOnly group: "com.liferay", name: "com.liferay.comment.taglib"
compileOnly group: "com.liferay", name: "com.liferay.frontend.taglib"
compileOnly group: "com.liferay", name: "com.liferay.frontend.taglib.clay"
compileOnly group: "com.liferay", name: "com.liferay.frontend.taglib.soy"
compileOnly group: "com.liferay", name: "com.liferay.frontend.taglib.util"
compileOnly group: "com.liferay", name: "com.liferay.info.api"
compileOnly group: "com.liferay", name: "com.liferay.item.selector.api"
compileOnly group: "com.liferay", name: "com.liferay.item.selector.criteria.api"
compileOnly group: "com.liferay", name: "com.liferay.item.selector.taglib"
compileOnly group: "com.liferay", name: "com.liferay.petra.reflect"
compileOnly group: "com.liferay", name: "com.liferay.petra.string"
compileOnly group: "com.liferay", name: "com.liferay.petra.function"
compileOnly group: "com.liferay", name: "com.liferay.trash.api"
compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel"
compileOnly group: "com.liferay.portal", name: "com.liferay.util.taglib"
compileOnly group: "javax.portlet", name: "portlet-api"
compileOnly group: "javax.servlet.jsp", name: "jsp-api"
compileOnly group: "jstl", name: "jstl"
compileOnly group: "org.apache.felix", name: "org.apache.felix.http.servlet-api", version: "1.1.+"
compileOnly group: "org.osgi", name: "org.osgi.service.component.annotations", version: "1.3.0"
compileOnly group: 'org.slf4j', name: 'slf4j-api', version: '1.7.+'
compileOnly project(":modules:trots:trots-api")
compileOnly project(":modules:trots:trots-service")
}
I get the same error for the api and service module but with different packages. I figured if I knew how to resolve this error I would do the same for the other two. I've tried to add a specific version to the "com.liferat.asset.api" package but this changes nothing.
Does anybody know what what else I can do to fix this error?
With the inclusion of
compileOnly group: "com.liferay.portal", name: "release.portal.api"
you should be able to omit any and all of the Liferay-related dependencies from your buildfile (at the very least all of the com.liferay grouped ones, but likely all of the ones that don't have a version signature)
That being said, if you're missing a transitive dependency, that might be because your workspace targets an older version than you're actually running. Since the rolling updates on 7.4, you should compile for your target version.
Make sure that your workspace's settings (gradle.properties) contain the proper version that you want to compile for - e.g.:
liferay.workspace.product=portal-7.4-ga41
If you still compile for GA1, but deploy on GA41, you'll likely run into these kinds of problems. Often you can can differ by a few versions, but occasionally (when you just hit the version where a package was bumped up one version), you'll need to hit the right release.
And an unrelated code-review issue: You should make sure that any service-builder related code only depends on your -api module, and not on the -service implementation. If necessary, add commonly used code/interfaces to your API, or to a third bundle to keep your dependencies clean.

NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider

I upgraded the plugin id 'org.springframework.boot' version '2.6.5' which led to the following changes
plugins {
id 'java'
id 'org.springframework.boot' version '2.6.5'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'jacoco'
id 'org.barfuin.gradle.jacocolog' version '1.0.1'
}
bootRun {
args = [
'--spring.config.location=./src/configs.properties',
]
}
springBoot {
mainClass = 'com.company.service.ServiceClass'
}
jar {
// This would avoid creating an additional "-plain" jar
enabled = true
archiveClassifier = ''
// Before the plugin upgrade I wasnt getting manifest attribute related error. Because of one of such errors, I added it the attribute explicitly.
manifest {
attributes 'Main-Class': 'com.company.service.ServiceClass'
}
}
bootJar {
layered {
enabled = true
}
}
dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12'
implementation 'org.bouncycastle:bcprov-jdk16:1.46'
implementation 'org.immutables:value:2.9.0'
implementation('org.apache.lucene:lucene-core:8.10.0')
implementation('com.fasterxml.jackson.core:jackson-core:2.13.2')
implementation('com.fasterxml.jackson.core:jackson-annotations:2.13.2')
implementation('com.fasterxml.jackson.core:jackson-databind:2.13.2')
implementation('org.apache.poi:poi-ooxml:5.2.2')
implementation('io.netty:netty-codec:4.1.75.Final')
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:2.13.2'
compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.immutables:value:2.9.0'
implementation('org.springframework.boot:spring-boot-starter-web:2.6.5') {
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude group: 'org.slf4j', module: 'slf4j-simple'
exclude group: 'org.slf4j', module: 'slf4j-log4j12'
exclude group: 'org.slf4j', module: 'jcl-over-slf4j'
exclude group: 'ch.qos.logback', module: 'logback-classic'
}
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude group: 'org.slf4j', module: 'slf4j-simple'
exclude group: 'org.slf4j', module: 'slf4j-log4j12'
exclude group: 'org.slf4j', module: 'jcl-over-slf4j'
exclude group: 'ch.qos.logback', module: 'logback-classic'
implementation('org.apache.tomcat.embed:tomcat-embed-core:9.0.54')
}
implementation('org.springframework.boot:spring-boot-starter-security:2.6.5') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude group: 'org.slf4j', module: 'slf4j-simple'
exclude group: 'org.slf4j', module: 'slf4j-log4j12'
exclude group: 'org.slf4j', module: 'jcl-over-slf4j'
exclude group: 'ch.qos.logback', module: 'logback-classic'
}
implementation('org.keycloak:keycloak-spring-boot-starter:17.0.1') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude group: 'org.slf4j', module: 'slf4j-simple'
exclude group: 'org.slf4j', module: 'slf4j-log4j12'
exclude group: 'org.slf4j', module: 'jcl-over-slf4j'
exclude group: 'ch.qos.logback', module: 'logback-classic'
}
implementation('org.keycloak.bom:keycloak-adapter-bom:17.0.1')
implementation('io.springfox:springfox-boot-starter:3.0.0')
implementation('io.springfox:springfox-swagger-ui:3.0.0')
}
// some jacoco and checkstyle configs here.
When I run the jar, I get the following error
>> java -jar build/libs/service-1.0-SNAPSHOT.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
at java.lang.Class.getDeclaredMethods0(Native Method)
From the solutions I found on SO, I added the dependency
implementation group: 'org.bouncycastle', name: 'bcprov-jdk16', version: '1.46'
But this doesnt work. Plus I am still using jdk8 and there is no org.bouncycastle for jdk8.
Any suggestions how I can work around this exception?
EDIT:
Running the application with intelliJ doesnt throw this problem. But happens when running from command line.
EDIT:
>> % java -version
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_292-b10)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.292-b10, mixed mode)
Full stack trace
% java -jar build/libs/my-service-class.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:650)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:632)
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.jce.provider.BouncyCastleProvider
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 7 more
Running the application with intelliJ doesnt throw this problem. But
happens when running from command line.
java -jar build/libs/my-service-class.jar
For executing a jar directly from command line, it should contain manifest information. Assuming the aforementioned jar contain the same as you're facing the the other issue(class def not found) so looks like your jar my-service-class.jar is thin jar and doesn't contain the other dependencies that you've added in the project. Two way to solve this problem. Either you can create uber jar i.e jar with all the dependencies included by using available plugins for the same and then execute the command or just add all the required dependencies into some folder and give path to it while runing the above command.

I can't use #PostConstruct and #PostDestroy with Java 11

I've got problem with using #PostConstruct and#PostDestroy annotations in my project. I can't use these annotations and it looks like these doesn't exist despite the fact that I imported Java's annotations. I am using Java 11 and that is content of my build.gradle file:
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.springframework', name: 'spring-webmvc', version: '5.1.0.RELEASE'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.7'
compile group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2'
provided group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
}
Note that both #PostConstruct and #PreDestroy annotations are part of Java EE. And since Java EE has been deprecated in Java 9 and removed in Java 11 we have to add an additional dependency to use these annotations:
For Maven
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
If using Gradle
implementation "javax.annotation:javax.annotation-api:1.3.2"
Found here: https://www.baeldung.com/spring-postconstruct-predestroy
You have only spring-webmvc, you need the rest of the spring to be able to use their annotations. Probably spring-core and spring-annotations.
Another solution which worked for me is this.
Go to https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api/1.2
and download the jar file.
Then copy the the jar file to your project lib directory.
Finally point the project build path, under class path to the file you pasted into your local lib folder.

Issue with gradle with adding Open JavaFX package from Maven Central

So in my build.gradle file I have this dependencies added.
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
// https://mvnrepository.com/artifact/org.hibernate/hibernate-core
compile group: 'org.hibernate', name: 'hibernate-core', version: '5.3.6.Final'
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.12'
// https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api
compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.0'
// https://mvnrepository.com/artifact/javax.xml/jaxb-impl
compile group: 'javax.xml', name: 'jaxb-impl', version: '2.1'
// https://mvnrepository.com/artifact/org.openjfx/javafx-controls
compile group: 'org.openjfx', name: 'javafx-controls', version: '11'
}
The class of other dependencies were added except for javafx classes. It wasn't downloaded all I get is just the meta-inf directory.
I am using JDK 11, so I really need openjfx repo to use JavaFX. because in JDK 11, javafx is decoupled.
Update: I am using Intellij Idea
Each supported platform has its own version of JavaFx module artifacts, so you need to specify it too:
compile group: 'org.openjfx', name: 'javafx-controls', version: '11', classifier: 'linux' //'win', 'mac'
Additionally, you will need to explicitly add all transitive dependencies of the included javafx modules, because gradle cannot resolve platform specific modules by itself:
compile group: 'org.openjfx', name: 'javafx-base', version: '11', classifier: 'linux'
compile group: 'org.openjfx', name: 'javafx-graphics', version: '11', classifier: 'linux'
Check official documentation: https://openjfx.io/openjfx-docs/#gradle

gradle configuration for powermock 1.7.4 and mockito 1.10.19

I'm trying to run Java tests with PowerMock version 1.7.4 and Mockito version 1.10.19, using Gradle.
PowerMock 1.7.4 has transitive dependencies on both
org.mockito » mockito-core 1.10.19
and on
org.mockito » mockito-core 2.8.9
(See https://mvnrepository.com/artifact/org.powermock/powermock-api-mockito-common/1.7.4)
In order to NOT bring in Mockito 2.8.9, but instead have tests compile and run against Mockito 1.10.19, I have to do the following (because of various transitive dependencies within the powermock jars):
testCompile ("org.mockito:mockito-core:${versions.mockito}") {
exclude group: 'org.hamcrest', module: 'hamcrest-core'
}
testCompile ("org.powermock:powermock-api-mockito-common:${versions.powermock}") {
exclude group: 'org.mockito', module: 'mockito-core'
}
testCompile ("org.powermock:powermock-api-mockito:${versions.powermock}") {
exclude group: 'org.mockito', module: 'mockito-core'
}
testCompile ("org.powermock:powermock-core:${versions.powermock}") {
exclude group: 'org.mockito', module: 'mockito-core'
}
testCompile ("org.powermock:powermock-module-junit4:${versions.powermock}") {
exclude group: 'org.powermock', module: 'powermock-module-junit4-common'
}
testCompile ("org.powermock:powermock-module-junit4-common:${versions.powermock}") {
exclude group: 'org.powermock', module: 'powermock-core'
}
testCompile ("org.powermock:powermock-api-support:${versions.powermock}") {
exclude group: 'org.powermock', module: 'powermock-core'
}
testCompile ("org.powermock:powermock-api-easymock:${versions.powermock}") {
exclude group: 'org.powermock', module: 'powermock-api-support'
}
testCompile ("org.powermock:powermock-module-javaagent:${versions.powermock}") {
exclude group: 'org.powermock', module: 'powermock-core'
}
testCompile ("org.powermock:powermock-module-junit4-rule-agent:${versions.powermock}") {
exclude group: 'org.powermock', module: 'powermock-core'
}
testRuntime("org.mockito:mockito-core:${versions.mockito}") {
exclude group: 'org.hamcrest', module: 'hamcrest-core'
}
Where versions is:
versions = [
'mockito': '1.10.19',
'powermock': '1.7.4'
]
Surely there is a better, more concise way of doing this in Gradle, right?
One way to compact it, excluding only mockito-core :
testCompile ("org.mockito:mockito-core:${versions.mockito}") {
exclude group: 'org.hamcrest', module: 'hamcrest-core'
}
['powermock-api-mockito-common',
'powermock-api-mockito',
'powermock-core',
'powermock-module-junit4',
'powermock-module-junit4-common',
'powermock-api-support',
'powermock-api-easymock',
'powermock-module-javaagent',
'powermock-module-junit4-rule-agent'].each {
testCompile ("org.powermock:${it}:${versions.powermock}") {
exclude group: 'org.mockito', module: 'mockito-core'
}
}
Moreover the testRuntime mockito-core is useless as testRuntime extends testCompile

Categories