How to setup AI in springboot kotlin poject - spring-boot

I want to use Deep learning java lib in my project. But I am not able
to setup AI DJL in my spring boot kotlin Gradle based project. I Use
Buildgradle.kts , but I don't know how to add ai.djl in
buildgradle.kts.

DJL provides springboot starter package, you can add the following section in your build.gradle.kts:
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("ai.djl.spring:djl-spring-boot-starter-pytorch-auto:0.11")
implementation("org.springframework.boot:spring-boot-starter-actuator")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
// See: https://github.com/awslabs/djl/blob/master/mxnet/mxnet-engine/README.md for MXNet library selection
}
You can find DJL springboot starter demo project from github: https://github.com/deepjavalibrary/djl-spring-boot-starter-demo

Related

How to configure the hamcrest dependency for gradle?

Again an abslout beginer question :-(
My gradle version is: Gradle 6.3
I initilized a small gradle project using gradle init for java and junit5
to learn junit5 and jmockit :-)
I tried to add some tutorial classes but gradle cannot resolve the hamcrest dependency :-(
// https://mvnrepository.com/artifact/org.hamcrest/hamcrest
testImplementation 'org.hamcrest:hamcrest:2.2'
as well as
// https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all
testImplementation 'org.hamcrest:hamcrest-all:1.3'
on the hamcrest web site, this hint is given
http://hamcrest.org/JavaHamcrest/distributables#using-hamcrest-in-a-gradle-project
here my build.gradle file:
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* User Manual available at https://docs.gradle.org/6.3/userguide/tutorial_java_projects.html
*/
plugins {
// Apply the java plugin to add support for Java
id 'java'
// Apply the application plugin to add support for building a CLI application.
id 'application'
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
google()
}
dependencies {
// This dependency is used by the application.
implementation 'com.google.guava:guava:28.2-jre'
testImplementation 'org.hamcrest:hamcrest:2.2'
// Use JUnit Jupiter API for testing.
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
// Use JUnit Jupiter Engine for testing.
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
}
application {
// Define the main class for the application.
mainClassName = 'jmockit_examples.App'
}
test {
// Use junit platform for unit tests
useJUnitPlatform()
}

How to change name of java library when build with gradle?

I'm trying to build a java library for my other java projects. I'm also trying to learn gradle. There is a tutorial : https://docs.gradle.org/current/samples/sample_building_java_libraries.html shows how to build libraries with gradle.
But somehow when I use gradlew build it always gives me lib-< version >.jar and creates a folder called lib and I can't change it.
This is my settings.gradle
rootProject.name = 'myOwnLibrary'
include('lib')
this is my build.gradle (inside lib folder)
plugins {
// Apply the java-library plugin for API and implementation separation.
id 'java-library'
}
version = "0.1.1"
tasks.named('jar') {
manifest {
attributes('Implementation-Title': project.name,
'Implementation-Version': project.version)
}
}
repositories {
// Use JCenter for resolving dependencies.
jcenter()
}
dependencies {
// Use JUnit test framework.
testImplementation 'junit:junit:4.13'
// This dependency is exported to consumers, that is to say, found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:29.0-jre'
}
With Kotlin DSL example, you can add in your tasks jar the following snippet:
tasks.jar {
manifest {
attributes(mapOf("Implementation-Title" to rootProject.name,
"Implementation-Version" to project.version))
}
archiveBaseName.set(rootProject.name)
}
where rootProject.name, is the value localized into settings.gradle.kts file.

can't use 'springBoot' scope in kotlin build script

I have a question.
There is a question about spring-boot-gradle-plugin. I written about spring boot example that allows the specified version (ex. 2.2.1.RELEASE).
By the way, springBoot scope is not available when applying spring-boot-gradle-plugin written in legacy plugin application style. But the plugin DSL style is not a problem.
I know that there is no dynamic version assignment in the plugins scope in Kotlin build scripts, so I have to write in the legacy plugin application style.
The code is as follows:
Using legacy plugin application:
buildscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.2.1.RELEASE")
}
}
apply(plugin = "org.springframework.boot")
springBoot {
mainClassName = "blabla~~"
}
e: .../build.gradle.kts:25:1: Unresolved reference: springBoot
Using the plugins DSL:
plugins {
id("org.springframework.boot") version "2.2.1.RELEASE"
}
springBoot {
mainClassName = "blabla~~"
}
no problem
Thanks! 😄

How to use spring boot in gradle without the spring boot gradle plugin

Can anyone show me or point me to a spring boot gradle project that does not make use of the spring boot gradle plugin.
I'm looking for something like a spring boot starter web hello world example that doesn't use the gradle plugin.
I can't imagine that the plugin is a requirement, but a search for examples all seem to lean on the gradle plugin, which lets just say is not an option in my environment, and no I can't switch to maven either.
Ideally the gradle build would work by adding something like the following:
gradle.properties
springBootVersion=2.1.3.RELEASE
build.gradle
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBootVersion
}
I used the spring dependency management plugin, and it works
buildscript {
ext {
springDepManagementVersion = '1.0.10.RELEASE'
springBootVersion = '2.6.6'
springCloudVersion = "2021.0.1"
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:${springDepManagementVersion}"
}
}
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
mavenBom "org.springframework.boot:spring-boot-dependencies:${springBootVersion}"
}
}
dependencies {
implementation "org.springframework.cloud:spring-cloud-starter-sleuth"
implementation 'org.springframework.boot:spring-boot-starter-json'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-security'
...
}
I can't use spring boot gradle plugin, since I can only use gradle 6.7.1, while spring boot gradle plugin requires gradle version at least 6.8 to support spring boot 2.6. I was inspired by the spring cloud bom solution.

How to use JPA metamodel with gradle, intellij IDEA?

I am using java 8, spring boot 2.0.0, spring-data-jpa(spring-boot-starter-data-jpa), gradle, intellij. I've been trying to use JPA Metamodel, but having difficulty on finding how to configure.
Metamodels for Entity classes aren't just generated.
I guessed it would be simple, but now it seems that can be wrong. How can I use it?
JDK11 / Gradle 5.0 / Hibernate 5.3.7.Final
sourceSets.main.java.srcDirs += "${buildDir}/generated"
compileJava {
options.annotationProcessorGeneratedSourcesDirectory = file("${buildDir}/generated")
}
dependencies {
annotationProcessor("javax.xml.bind:jaxb-api")
annotationProcessor("org.hibernate:hibernate-jpamodelgen")
}
Generated Metamodel classes will be generated at 'build/generated'
If you are using JDK8 or Hibernate 5.4+, annotationProcessor("javax.xml.bind:jaxb-api") may unnecessary.
I did this the other day using the scalified metamodel gradle plugin (https://plugins.gradle.org/plugin/com.scalified.plugins.gradle.metamodel). I'm using Spring Boot 2.0.5, but I don't see why it wouldn't work the same with Spring Boot 2.0.0. I'm using Gradle 4.8.1 as well.
Below is an excerpt of my build.gradle.
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath (
"org.springframework.boot:spring-boot-gradle-plugin:2.0.0",
"gradle.plugin.com.scalified.plugins.gradle:metamodel:0.0.1");
}
}
apply plugin: "com.scalified.plugins.gradle.metamodel"
// The plugin will default to the latest version of Hibernate if this is not specified
metamodel {
hibernateVersion = '5.2.14.Final' // For Spring Boot 2.0.0
hibernateVersion = '5.2.17.Final' // For Spring Boot 2.0.5
}
This builds the metamodal files under src/generated and they can be used in your code. I had to also change an IntelliJ setting because IntelliJ's Build Automatically excludes some Gradle tasks that could be long running. See Automatically run Gradle task in project build with IntelliJ IDEA and https://youtrack.jetbrains.com/issue/IDEA-175165 for more details.
This setting I changed to overcome this is: Preferences->Build/Execution/Deployment->Gradle->Runner->Delegate IDE build/run actions to Gradle. An alternative would be to run the metamodelCompile gradle task manually as needed. That would lessen the time to rebuild by a little if you aren't frequently change your entities.

Resources