Can't use Manifold in gradle java project - java

I have a project based on JDK 11, and I want to use Manifold (http://manifold.systems/) in my java project.
My build.gradle:
plugins {
id 'java'
}
//
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
repositories {
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}
dependencies {
implementation 'org.projectlombok:lombok:1.18.18'
implementation "io.vavr:vavr:0.10.3"
implementation 'systems.manifold:manifold-science:2021.1.25'
compileOnly 'org.projectlombok:lombok:1.18.20'
annotationProcessor 'org.projectlombok:lombok:1.18.20'
annotationProcessor group: 'systems.manifold', name: 'manifold-ext', version: '2021.1.25'
testCompileOnly 'org.projectlombok:lombok:1.18.20'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.20'
testAnnotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess'
testImplementation 'org.junit.jupiter:junit-jupiter-engine'
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}
I tried this:
import java.math.BigDecimal;
#Extension
public abstract class ManBigDecimalExt implements ComparableUsing<BigDecimal> {
/**
* Supports binary operator {#code +}
*/
public static BigDecimal plus(#This BigDecimal thiz, BigDecimal that) {
return thiz.add(that);
}
}
But it stated that these Manifold Annotations were not found:
#Extension
#This
What should I do?

Thanks for the Github page, it helped a lot! After skimming through the web page you sent me, I found the solution. Actually, in the library systems.manifold, the annotations you mentioned are not present. Add another implementation named manifold-science or manifold-ext like this,
implementation 'systems.manifold:manifold-science:2021.1.25-SNAPSHOT'
or
implementation 'systems.manifold:manifold-ext:2021.1.25-SNAPSHOT'
And, add another repository for obtaining the library,
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
Don't forget to import the libraries,
import manifold.ext.rt.api.Extension;
import manifold.ext.rt.api.This;
import manifold.ext.rt.api.ComparableUsing;
Hopefully, this should solve the problem :D

The Projects Quick Reference supplies links to all of Manifold's dependencies, each supplying its own setup docs.
The setup docs for Manifold Extensions, which you appear to be using:
plugins {
id 'java'
}
group 'com.example'
version '1.0-SNAPSHOT'
targetCompatibility = 11
sourceCompatibility = 11
repositories {
jcenter()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}
configurations {
// give tests access to annotationProcessor dependencies
testImplementation.extendsFrom annotationProcessor
}
dependencies {
implementation 'systems.manifold:manifold-ext-rt:2021.1.25'
testCompile 'junit:junit:4.12'
// Add manifold to -processorpath for javac
annotationProcessor group: 'systems.manifold', name: 'manifold-ext', version: '2021.1.25'
}
if (JavaVersion.current() != JavaVersion.VERSION_1_8 &&
sourceSets.main.allJava.files.any {it.name == "module-info.java"}) {
tasks.withType(JavaCompile) {
// if you DO define a module-info.java file:
options.compilerArgs += ['-Xplugin:Manifold', '--module-path', it.classpath.asPath]
}
} else {
tasks.withType(JavaCompile) {
// If you DO NOT define a module-info.java file:
options.compilerArgs += ['-Xplugin:Manifold']
}
}
Update:
Based on your recent update, you need to make the following changes:
Your build.gradle is missing the Manifold compiler argument specified above. Copy/Paste the if/else statement dealing with -Xplugin:Manifold.
Remove the implements ComparableUsing<BigDecimal> from your ManBigDecimalExt as it duplicates the interface already implemented in manifold-science. Additionally, the plus method is also duplicated; BigDecimal arithmetic is already supported with manifold-science.

Related

build.gradle cannot resolve symbol 'groovy.json.JsonSlurper'

I can't resolve the groovy.json.JsonSlurper in the build.gradle file with Intellij, does anyone know how to fix it?
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()
}
task sample() {
doLast {
sample();
}
}
import groovy.json.JsonSlurper
def sample(){
def json = new JsonSlurper().parseText('{"a":"b"}')
println(json);
}
You missing the needed dependency.
Add org.codehaus.groovy:groovy-json:3.0.9 in dependencies section so ti will look like
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
implementation 'org.codehaus.groovy:groovy-json:3.0.9'
}
And then you can test with CLI as gradle sample or ./gradlew sample and it will return {a=b}

JOOQ Doesn't Generate

I was following here. I used postgresql instead of h2. I was able to build the project with some additional fields.
I created a package database under com.example.demo, but it was still empty after project was built.
build.gradle:
buildscript {
ext {
springBootVersion = '2.4.2'
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath 'org.jooq:jooq-codegen:3.14.4'
classpath 'org.postgresql:postgresql:42.2.18'
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '15'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-jooq'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.flywaydb:flyway-core'
runtimeOnly 'org.postgresql:postgresql'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
import org.jooq.codegen.GenerationTool
import org.jooq.meta.jaxb.*
task generate {
def configuration = new Configuration()
configuration
.withJdbc(new Jdbc()
.withDriver('org.postgresql.Driver')
.withUrl('jdbc:postgresql://localhost:5432/vertx')
.withUser('postgres')
.withPassword('postgres'))
.withGenerator(new Generator()
.withDatabase(new Database().withInputSchema('public'))
.withGenerate(new Generate()
.withPojos(true)
.withDaos(true))
.withTarget(new Target()
.withPackageName('com.example.demo.database')
.withDirectory('src/main/java')))
doLast {
GenerationTool.generate(configuration)
}
}
Is there something I am missing? Why can I not see pojos and daos in database package? My database has only one table with 2 columns.
BUILD SUCCESSFULL when I type ./gradlew generate, but nothing gets generated.
You can try ./gradlew generate --info command, which will give you more information about the build process.
My gradle script is quite similar to yours. By using above command, I find out that the classes are actually generated, but in a different folder /Users/{MyUserName}/.gradle/daemon/7.1.1/src/main/java.
So I have to explicitly set the output directory with def outputDirectory = projectDir.toString() + '/src/main/java'.
This works for me
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath 'org.jooq:jooq-codegen:3.16.2'
classpath 'mysql:mysql-connector-java:8.0.27'
}
}
plugins {
id 'org.springframework.boot' version '2.6.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
if(JavaVersion.current() != JavaVersion.VERSION_11){
throw new GradleException("This build must be run with java 11")
}
repositories {
mavenCentral()
}
group = 'snorlax'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
//create a fat Jar with all dependencies
jar {
duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
from {
configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
manifest {
attributes "Main-Class": "com.snorlax.userservice.MainApplication"
}
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
dependencies {
// Spring boot
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// Swagger
implementation group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
// Lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
// RDS Connection
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'mysql:mysql-connector-java:8.0.27'
// AWS secretes manager
implementation 'com.amazonaws.secretsmanager:aws-secretsmanager-jdbc:1.0.6'
// JOOQ
implementation 'org.springframework.boot:spring-boot-starter-jooq'
implementation 'org.jooq:jooq-meta:3.16.2'
compileOnly 'org.jooq:jooq-codegen:3.16.2'
}
test {
useJUnitPlatform()
}
/************************
jooq code generation
*************************/
import org.jooq.codegen.GenerationTool;
import org.jooq.meta.jaxb.*;
task generate {
def outputDirectory = projectDir.toString() + '/src/main/java'
def configuration = new Configuration()
.withJdbc(new Jdbc()
.withDriver('com.mysql.cj.jdbc.Driver')
.withUrl('jdbc:mysql://127.0.0.1:3306/snorlaxRds')
.withUser('root')
.withPassword('123456'))
.withGenerator(new Generator()
.withDatabase(new Database().withInputSchema("snorlaxRds"))
.withGenerate(new Generate()
.withPojos(true)
.withDaos(true))
.withTarget(new Target()
.withPackageName('snorlax.userservice.database')
.withDirectory(outputDirectory)));
doLast {
GenerationTool.generate(configuration)
}
}
Your file looks okay. I had a similar issue (while using maven). Try three things (that worked for me):
check case of the schema in "withInputSchema" property. Try to pass uppercase PUBLIC.
try to change 'src/main/java' to './src/main/java'
add "includes" and pass .* in it (to generate for all tables in the schema)

multi-module Gradle project: webjar doesn't get placed in "META-INF/resources"

I am trying to combine a Spring Boot backend api and a React frontend app into one single jar.
I was able to build a jar file containing both by following this article (https://medium.com/xebia-engineering/a-minimalistic-guide-to-building-and-deploying-monolithic-spring-boot-react-applications-39440035b27) but the frontend jar ends up in BOOT-INF/lib/frontend.jar instead of META-INF/resources. How can I create a jar file with the webjar in the right directory so it can render a React page?
frontend/build.gradle
plugins {
id "com.moowork.node" version "1.3.1"
}
apply plugin: "java"
apply plugin: "com.moowork.node"
node {
version = "12.9.1"
download = true
}
task bundle(type: NpmTask, dependsOn: npmInstall) {
args = ["run", "build"]
}
task uiTest(type: NpmTask) {
environment = ["CI": "true"]
args = ["run", "test"]
}
task run(type: NpmTask) {
args = ["start"]
}
check.dependsOn(test)
jar.dependsOn(bundle)
task webjar(type: Jar) {
from(fileTree("build")) {
into "META-INF/resources"
}
}
check.dependsOn(test)
jar.dependsOn(bundle)
jar.finalizedBy("webjar")
backend/build.gradle
plugins {
id 'org.springframework.boot' version '2.1.5.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
maven {
url 'https://repo.spring.io/libs-milestone'
}
}
jar {
manifest {
attributes "Main-Class": "com.example.kasahararestapi.StockCheckAppApplication"
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
tasks.withType(Test) {
scanForTestClasses = false
include "**/*Test.class" // whatever Ant pattern matches your test class files
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.social:spring-social-github:1.0.0.M4'
implementation 'commons-io:commons-io:2.6'
implementation 'org.apache.commons:commons-lang3:3.0'
implementation 'io.jsonwebtoken:jjwt:0.9.0'
implementation platform('com.amazonaws:aws-java-sdk-bom:1.11.807')
implementation 'com.amazonaws:aws-java-sdk-s3'
implementation 'org.springframework.boot:spring-boot-starter-mail:1.2.0.RELEASE'
implementation 'org.modelmapper:modelmapper:2.3.5'
testCompile group: 'junit', name: 'junit', version: '4.11'
implementation project(path: ':frontend', configuration: 'default')
}
setting.gradle
pluginManagement {
repositories {
gradlePluginPortal()
}
}
rootProject.name = 'stock_check_app'
include 'backend'
include 'frontend'

gradle generates protobuf class but shows compile error

The generated protobuf class is under generated-sources as expected.
But it has references to com.google.protobuf, for example below code. And I get compilation error saying com.google.protobuf not found.
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistryLite registry) {
}
The below is my build.gradle file.
plugins {
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
id 'com.google.protobuf' version '0.8.10'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
sourceSets {
main {
proto {
srcDir 'src/main/proto'
}
java {
// include self written and generated code
srcDirs 'src/main/java', 'generated-sources/main/java'
}
}
// remove the test configuration - at least in your example you don't have a special test proto file
}
protobuf {
// Configure the protoc executable
protoc {
// Download from repositories
artifact = 'com.google.protobuf:protoc:3.0.0'
}
generateProtoTasks.generatedFilesBaseDir = 'generated-sources'
generateProtoTasks {
// all() returns the collection of all protoc tasks
all().each { task ->
// Here you can configure the task
}
// In addition to all(), you may get the task collection by various
// criteria:
// (Java only) returns tasks for a sourceSet
ofSourceSet('main')
}
}
I think the problem is that the protobuf library is not showing up in the external libraries of my intellij project. Is there a way to make it work with gradle?
Working gradle file:
plugins {
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
id 'com.google.protobuf' version '0.8.10'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.google.protobuf:protobuf-java:3.11.1'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
sourceSets {
main {
proto {
srcDir 'src/main/proto'
}
java {
// include self written and generated code
srcDirs 'src/main/java', 'generated-sources/main/java'
}
}
// remove the test configuration - at least in your example you don't have a special test proto file
}
protobuf {
// Configure the protoc executable
protoc {
// Download from repositories
artifact = 'com.google.protobuf:protoc:3.6.0'
}
generateProtoTasks.generatedFilesBaseDir = 'generated-sources'
generateProtoTasks {
// all() returns the collection of all protoc tasks
all().each { task ->
// Here you can configure the task
}
// In addition to all(), you may get the task collection by various
// criteria:
// (Java only) returns tasks for a sourceSet
ofSourceSet('main')
}
}

Unable to access generated java files

My project uses gRPC and it generates a file in generated folder.
This is my build.gradle of the module.
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'io.grpc:grpc-okhttp:1.4.0'
compile 'io.grpc:grpc-protobuf-lite:1.4.0'
compile 'io.grpc:grpc-stub:1.4.0'
compile 'javax.annotation:javax.annotation-api:1.2'
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
apply plugin: 'com.google.protobuf'
def grpcVersion = '1.4.0' // CURRENT_GRPC_VERSION
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.3.0'
}
plugins {
grpc {
artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
}
}
generateProtoTasks {
all()*.plugins {
grpc {
// To generate deprecated interfaces and static bindService method,
// turn the enable_deprecated option to true below:
option 'enable_deprecated=false'
}
}
}
}
// Inform IntelliJ projects about the generated code.
apply plugin: 'idea'
idea {
module {
// Not using generatedSourceDirs because of
// https://discuss.gradle.org/t/support-for-intellij-2016/15294/8
sourceDirs += file("${projectDir}/build/generated/source/proto/main/java");
sourceDirs += file("${projectDir}/build/generated/source/proto/main/grpc");
}
}
How do I fix this?
You have to add this folder to your source sets.
The following should work:
sourceSets {
generated{
java.srcDir "${projectDir}/build/generated/source/proto/main/java"
}
}
Here is a comprehensive documentation for customizing the source sets.
I figured out. My iml file includes the folder but also excludes it. I think exclusion takes precedence, so I have to remvoe the <excludeFolder> element.

Categories

Resources