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

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.

Related

SQL Server dependencies in Spring Boot Gradle Java application

I am trying to connect a database to my Spring Boot application using Gradle.
On the internet I only find examples for Maven projects, using the pom.xml file.
I don't know for sure, but I think build.gradle is the equivalent in Gradle? What should I add to it to add support for Microsoft SQL Server?
This is my build.gradle right now:
plugins {
id 'org.springframework.boot' version '2.3.3.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
id 'application'
}
group = 'Project'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
application{
mainClassName 'project.rlstop.Publisher'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
implementation group: 'org.glassfish.jersey.bundles', name: 'jaxrs-ri', version: '2.+'
implementation group: 'org.glassfish.jersey.containers', name: 'jersey-container-servlet', version: '2.+'
// https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api
implementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.+'
// https://mvnrepository.com/artifact/org.glassfish.jaxb/jaxb-runtime
implementation group: 'org.glassfish.jaxb', name: 'jaxb-runtime', version: '2.+'
// Grizzly will host the service
implementation group: 'org.glassfish.jersey.containers', name: 'jersey-container-grizzly2-http', version: '2.+'
// Logging
implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.+'
implementation group: 'org.slf4j', name: 'slf4j-simple', version: '2.+'
}
test {
useJUnitPlatform()
}
I don't know for sure, but I think build.gradle is the equivalent in Gradle?
Yes, it is. build.gradle is to Gradle what pom.xml is to Maven.
What should I add to it to add support for Microsoft SQL Server?
You currently have a dependency to the MySQL JDBC driver declared:
runtimeOnly 'mysql:mysql-connector-java'
To use MS SQL Server, you need to replace that with
runtimeOnly 'com.microsoft.sqlserver:mssql-jdbc'
You may explorer a skeleton project using the Spring Initializr. You may also refer to the Reference Documentation for instructions on how to configure the data source properties to connect to your MS SQL Server.

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

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'
}

WARNING: javax.persistence.spi::No valid providers found

I am getting the warning
WARNING: javax.persistence.spi::No valid providers found.
I have code with JPA annotations, but I need them only to use with JOOQ.
How to remove this warning?
Adding my gradle file:
dependencies {
compile project(':common:packages:autogenerated_dao')
externalLib group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: jackson_databind_version
externalLib group: 'javax.validation', name: 'validation-api', version: javax_validation_version
externalLib group: 'org.hibernate', name: 'hibernate-validator', version: hibernate_validator_version
externalLib group: 'javax.el', name: 'javax.el-api', version: javax_el_version
externalLib group: 'org.glassfish', name: 'javax.el', version: glassfish_el_version
externalLib group: 'javax.persistence', name: 'javax.persistence-api', version: javax_persistence_version
}
Mentioned warning raised because javax.persistence-api dependency present in build file, but obviously here is no implementations for it. To get rid from warning, remove javax.persistence-api dependency.
Bean Validation provided by validation-api and its implementation hibernate-validator, so you will be fine.
I was facing same "WARNING: javax.persistence.spi::No valid providers found."
I added below artifact in pom.xml and issue resolved.
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
OR
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>

IntelliJ IDEA plugin dependency ClassNotFoundException

I created plugin with the following dependencies:
compile group: 'org.apache.httpcomponents', name: 'fluent-hc', version: '4.5.6'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
compile group: 'joda-time', name: 'joda-time', version: '2.10'
compile group: 'com.google.guava', name: 'guava', version: '26.0-jre'
and it is working perfect.
Then I added more dependencies:
compile group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
compile group: 'org.hibernate.validator', name: 'hibernate-validator', version: '6.0.13.Final'
compile group: 'javax.el', name: 'javax.el-api', version: '3.0.0'
compile group: 'org.glassfish', name: 'javax.el', version: '3.0.0'
and it is working perfect when I tested plugin with runIde task on local.
When I added jar file to IDEA I got exception: java.lang.NoClassDefFoundError: javax/validation/Validation
In both cases jar file does't contain any dependencies.
What did I miss?
UPDATE
6 years old question:
How does Intellij IDEA manage plugin dependencies?
Is it still valid? If so how I can put my dependencies in the plugin jar?
Im doing now:
gradle clean buildPlugin
Update build.gradle
jar {
from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
Based on response here I need to submit zip file instead of jar. Zip contains all dependencies.

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

Categories