How can i compile java record with scala code? - java

I'm currently working on project with both Scala and Java. And we want to rewrite some Scala code on Java. So when I've been starting rewrite my code I've faced with this issue.
CountryResponse.java:5: illegal start of type declaration
It seem like Scala couldn't compile Java's records introduced in JDK 16. I've made some research and found only this discussion on GitHub. Could someone suggest any workaround to compile Java's records?
I did try increase Scala version from 2.12.2 to 2.13.6 but problem wasn't solved.
We use Java 16, Scala 2.12.2 and Gradle 7.0.1 and also scala and java plugins for Gradle.
Also here my gradle's settings for compile both sources.
compileScala {
sourceCompatibility("16")
targetCompatibility("16")
}
sourceSets {
main {
java {
srcDirs = []
}
scala {
srcDirs = ['src/main/scala', 'src/main/java']
}
}
}
edit: update link to discussion

I tested with following project containing both Scala and Java 16 sources. The Scala and Java source depends on each other.
If your both Java and Scala sources depend on each other, then you will need to put those sources under common folder. Lets say main/jvm.
You can place all your Java records under main/java. This will work as long as your records don't have dependency on Scala code. Otherwise you will have to break this into multiple modules and micromanage according to the dependency graph.
Gradle version is 7.1, and this project builds and then runs successfully (both ScalaMain and JavaMain).
build.gradle
plugins {
id 'java'
id 'scala'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
compileScala {
sourceCompatibility("16")
targetCompatibility("16")
}
sourceSets {
main {
java {
srcDirs = ['src/main/java']
}
scala {
srcDirs = ['src/main/jvm']
}
}
}
dependencies {
implementation 'org.scala-lang:scala-library:2.13.6'
}
main/java/TestJavaRecord.java
public record TestJavaRecord(int i) {}
main/jvm/TestScalaClass.scala
class TestScalaClass(val i: Int, val testJavaRecord: TestJavaRecord) {}
main/jvm/TestJavaClass.java
public class TestJavaClass {
public int i;
public TestScalaClass testScalaClass;
public TestJavaClass(int i, TestScalaClass testScalaClass) {
this.i = i;
this.testScalaClass = testScalaClass;
}
}
main/jvm/ScalaMain.scala
object ScalaMain extends App {
val testJavaRecord = new TestJavaRecord(5)
val testScalaClass = new TestScalaClass(5, testJavaRecord)
val testJavaClass = new TestJavaClass(5, testScalaClass)
println(testJavaRecord.i)
println(testScalaClass.testJavaRecord.i)
println(testJavaClass.testScalaClass.testJavaRecord.i)
}
main/jvm/JavaMain.java
public class JavaMain {
public static void main(String[] args) {
TestJavaRecord testJavaRecord = new TestJavaRecord(5);
TestScalaClass testScalaClass = new TestScalaClass(5, testJavaRecord);
TestJavaClass testJavaClass = new TestJavaClass(5, testScalaClass);
System.out.println(testJavaRecord.i());
System.out.println(testScalaClass.testJavaRecord().i());
System.out.println(testJavaClass.testScalaClass.testJavaRecord().i());
}
}

Related

Duplicate handling strategy error with gradle while using protobuf for java

I am using the below configuration build.gradle
plugins {
id "com.google.protobuf" version "0.8.17"
id "java"
}
group "de.prerna.aws.tests"
version "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
ext {
protobufVersion = "3.18.1"
}
dependencies {
implementation "com.google.protobuf:protobuf-java:$protobufVersion"
sourceSets {
main {
proto {
srcDir 'src/main/proto'
}
java {
// include self written and generated code
srcDirs 'src/main/java'
}
}
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:4.0.0-rc-2'
}
plugins {
grpc {
artifact = "io.grpc:protoc-gen-grpc-java:1.39.0"
}
}
generateProtoTasks.generatedFilesBaseDir = 'generated-sources'
generateProtoTasks {
all().each { task ->
task.plugins { grpc{} }
}
ofSourceSet('main')
}
}
Error
* What went wrong:
Execution failed for task ':processResources'.
> Entry Person.proto is a duplicate but no duplicate handling strategy has been set. Please refer to https://docs.gradle.org/7.2/dsl/org.gradle.api.tasks.Copy.html#org.gradle.api.tasks.Copy:duplicatesStrategy for details.
A variant of BParolini for build.gradle (Groovy DSL)
tasks.withType(Copy) {
filesMatching("**/*.proto") {
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}
}
I could fix this problem by adding the following code to my build.gradle.kts:
tasks {
withType<Copy> {
filesMatching("**/*.proto") {
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}
}
}
Extra info: I'm using Gradle 7.3-rc-3 and Java 17.
Unfortunately nobody explains reasons for this problem, so here is some of my explorations and guesses. Please correct me if you know more.
If found that following build script code causes this error:
proto { srcDir 'src/main/proto' }
If look inside "build/extracted-include-protos" directory, there are original .proto files copied into "build/extracted-include-protos/test" (but not into main).
My guess is that those auto-copied .proto files are originally uses as the only sources, but when adding "src/main/proto" source set we give some compiler tool second set of same files.
Removing this srcDir is not a good idea, because it required for IDEA to correctly open included .proto on Ctrl+click (otherwise it is opened extracted copies which is useless).

java 9 modules with gradle not working: error: module not found: <module name>

I made a minimalist java gradle project to test java 9 modules.
I made only 1 module and tried testing depending on a third party module. But I keep getting the error when I try to build or run the project:
module not found: commons.validator
here are my files:
Demo.java"
public class Demo {
public static void main(String[] args) {
boolean result = EmailValidator.getInstance().isValid("abcd");
System.out.println(result);
}
}
module-info.java:
module auth.server.main {
requires commons.validator;
}
build.gradle:
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
// for email validation
implementation 'commons-validator:commons-validator:1.7'
}
/*this is to let gradle infer the locations of the modules*/
java {
modularity.inferModulePath.set(true)
}
Whats wrong and how to solve it?
This might easy way.
plugins {
id 'java'
id "org.javamodularity.moduleplugin" version "1.1.1"
}
repositories {
mavenCentral()
}
dependencies {
implementation 'commons-validator:commons-validator:1.7'
}
Check it out : https://github.com/java9-modularity/gradle-modules-plugin

How to include the client from openapi-generator in a gradle java application?

I want to create a gradle java application that generates a client from an openAPI specification file and uses that client.
So I created a java application with gradle init (type:application, language:Java, DSL:groovy, test-framework:Junit Jupiter, project-name:simple-java-app, package-structure:a.aa).
Small example of what works:
I can create a new source folder second/loc/src/main/java with a package b.bb and a class Foo.
And with the following build.gradle
plugins {
id 'java'
id 'application'
}
repositories {
jcenter()
}
sourceSets {
second {
java {
srcDir 'second/loc/src/main/java'
}
}
}
compileJava {
source += sourceSets.second.java
}
dependencies {
implementation 'com.google.guava:guava:29.0-jre'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
}
application {
mainClassName = 'a.aa.App'
}
test {
useJUnitPlatform()
}
The main class can access Foo:
package a.aa;
import b.bb.Foo;
public class App {
public static void main(String[] args) {
System.out.println(new Foo().sayFoo());
}
}
What doesn't work
Now I try the same for generated code by openapi-generator:
Under plugins I add id "org.openapi.generator" version "4.3.1"
And I add a new task:
openApiGenerate {
generatorName = "java"
inputSpec = "$rootDir/specs/petstore.yaml".toString()
outputDir = "$buildDir/generated".toString()
apiPackage = "org.openapi.example.api"
invokerPackage = "org.openapi.example.invoker"
modelPackage = "org.openapi.example.model"
configOptions = [
dateLibrary: "java8"
]
}
Then I execute the task openApiGenerate and confirm in the file system that the sources have been generated(eclipse won't show the build folder).
Now I use the same method as above resulting in below build.gradle
plugins {
id 'java'
id 'application'
id "org.openapi.generator" version "4.3.1"
}
repositories {
jcenter()
}
openApiGenerate {
generatorName = "java"
inputSpec = "$rootDir/specs/petstore.yaml".toString()
outputDir = "$buildDir/generated".toString()
apiPackage = "org.openapi.example.api"
invokerPackage = "org.openapi.example.invoker"
modelPackage = "org.openapi.example.model"
configOptions = [
dateLibrary: "java8"
]
}
sourceSets {
client {
java {
srcDir '$buildDir/generated/src/main/java'
}
}
}
compileJava {
source += sourceSets.client.java
}
dependencies {
implementation 'com.google.guava:guava:29.0-jre'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
}
application {
mainClassName = 'a.aa.App'
}
test {
useJUnitPlatform()
}
But when I try to use the classes now:
package a.aa;
import org.openapi.example.model.Pet;
public class App {
public static void main(String[] args) {
Pet p = new Pet(0L);
System.out.println(p.getId());
}
}
neither import nor Pet can be resolved.
> Task :compileJava FAILED
C:\...\simple-java-app\src\main\java\a\aa\App.java:6: error: package org.openapi.example.model does not exist
import org.openapi.example.model.Pet;
^
C:\...\simple-java-app\src\main\java\a\aa\App.java:14: error: cannot find symbol
Pet p = new Pet(0);
^
symbol: class Pet
location: class App
C:\...\simple-java-app\src\main\java\a\aa\App.java:14: error: cannot find symbol
Pet p = new Pet(0);
^
symbol: class Pet
location: class App
3 errors
FAILURE: Build failed with an exception.
I don't know how to debug this, frankly I'm unsure if source sets are even the right way. All openapi-generator tutorials seem to use them, I haven't tried subprojects yet, the openApiGenerate task seems to create a complete project with build.gradle and everything.
You need to add the sources from the generated code to your project. One example from one of my projects:
sourceSets.main.java.srcDir "${buildDir}/generated/src/main/java"
After generation make sure you refresh gradle and project.
With build.gradle.kts and gradle 7+ I used in Kotlin DSL:
configure<SourceSetContainer> {
named("main") {
java.srcDir("$buildDir/generated/src/main/java")
}
}
To add the java generated sources to the project's source.

Adding support for Kotlin sources to existing Java multi-project Gradle build

I have a Spring Boot Java project that builds using Gradle (v6.2.2).
build.gradle.kts
plugins {
base
java
id("org.springframework.boot") apply false
}
val gradleVersionProperty: String by project
val javaVersion: String by project
val springBootVersion: String by project
val springCloudStarterParentBomVersion: String by project
if (JavaVersion.current() != JavaVersion.VERSION_11) {
throw GradleException("This build must be run with JDK 11")
} else {
println("Building with JDK " + JavaVersion.current())
}
tasks.withType<Wrapper> {
gradleVersion = gradleVersionProperty
distributionType = Wrapper.DistributionType.ALL
}
allprojects {
group = "com.meanwhile.in.hell"
version = "$version"
// Repos used in dependencyManagement section of settings.gradle
repositories {
mavenLocal()
mavenCentral()
maven("https://repo.spring.io/snapshot")
maven("https://repo.spring.io/milestone")
}
}
subprojects {
if (!project.name.startsWith("platform")) {
apply {
plugin("java-library")
}
java.sourceCompatibility = JavaVersion.VERSION_11
java.targetCompatibility = JavaVersion.VERSION_11
// Change the default test logging settings
tasks.withType<Test>() {
useJUnitPlatform()
testLogging {
events = setOf(
org.gradle.api.tasks.testing.logging.TestLogEvent.FAILED,
org.gradle.api.tasks.testing.logging.TestLogEvent.PASSED,
org.gradle.api.tasks.testing.logging.TestLogEvent.SKIPPED
)
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
}
enableAssertions = false
ignoreFailures = gradle.startParameter.isContinueOnFailure
maxParallelForks =
(Runtime.getRuntime().availableProcessors() / 2).takeIf { it > 0 } ?: 1
}
dependencies {
"api"(enforcedPlatform("org.springframework.boot:spring-boot-dependencies:$springBootVersion"))
"api"(enforcedPlatform("org.springframework.cloud:spring-cloud-dependencies:$springCloudStarterParentBomVersion"))
"implementation"(enforcedPlatform(project(":platform-dependencies")))
"testCompile"("org.springframework.boot:spring-boot-starter-test")
}
}
}
However, I would like to add support for Spring Boot Kotlin sub-projects within it. I have used a very simple sample project from a Kotlin-only project I have that builds fine within it. Without any changes to my root build.gradle.kts file, my current build error is:
* What went wrong:
Execution failed for task ':kotlin-sample-project:bootJar'.
> Main class name has not been configured and it could not be resolved
I have not configured the main class for any of the Java sub-projects and neither have I in my Kotlin-only other project.
My build.gradle.kts in kotlin-sample-project is very simple:
plugins {
id("org.springframework.boot")
}
dependencies {
implementation("org.springframework.cloud:spring-cloud-starter-gateway")
}
And my main class looks like:
src/main/kotlin/sample/KotlinSampleApplication.kts
package com.meanwhile.in.hell.kotlinsample
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
#SpringBootApplication
open class KotlinSampleApplication
fun main(args: Array<String>) {
runApplication<KotlinSampleApplication>(*args)
}
I have tried to add the kotlin plugin, but the build fails instantly not knowing what it is.
plugins {
base
java
kotlin
}
Error:
Line 9: kotlin
^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public val <T : Any> Class<TypeVariable(T)>.kotlin: KClass<TypeVariable(T)> defined in kotlin.jvm
I have tried to add the kotlin plugin, but the build fails instantly not knowing what it is.
It should be:
build.gradle.kts:
plugins {
kotlin("jvm").version("1.3.72")
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
Bot Kotlin JVM plugin should be applied and Kotlin stdlib be present on compile classpath.

api configuration of java-library plugin is not recognized

I am new to Gradle and i am using Gradle 6.1.
I am writing small application to understand the concepts of multi project Application and Java-Library plugin of Gradle.
My Question is :
How App.java is running perfectly fine without importing DefaultRandomGenerator class from SubProject-2
Why am i getting the error message "No candidates found for method call api" in build.grade file of Parent project (MultiProjectApp).
Below are my application code :
Parent Project (MultiProjectApp) files
settings.gradle
rootProject.name = 'MultiProjectApp'
include 'SubProject-1'
include 'SubProject-2'
build.gradle
allprojects {
apply plugin: 'java'
group 'org.example'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
}
project(':SubProject-1') {
dependencies {
implementation project(':SubProject-2')
}
}
project(':SubProject-2') {
apply plugin: 'java-library'
dependencies {
api 'org.apache.commons:commons-math3:3.2'
implementation 'org.apache.logging.log4j:log4j-core:2.5'
testImplementation "junit:junit:4.12"
}
}
SubProject-2 files
build.gradle
Empty file
RandomGenerator.java
package org.examples;
public interface RandomGenerator {
String name();
int generate();
}
DefaultRandomGenerator.java
package org.examples;
import org.apache.commons.math3.random.RandomDataGenerator;
public class DefaultRandomGenerator implements RandomGenerator {
public String name() {
return "Main Random Number Generator";
}
public int generate() {
final RandomDataGenerator randomDataGenerator = new RandomDataGenerator();
return randomDataGenerator.nextInt(5, 10);
}
}
SubProject-1 files
build.gradle
Empty file
App.java
package org.examples;
import org.apache.commons.math3.random.RandomDataGenerator;
public class App {
public static void main(String[] args) {
RandomGenerator aRandomGenerator = new DefaultRandomGenerator();
System.out.println("The 1st random number is :" + aRandomGenerator.generate());
System.out.println("The 2nd random number is :" + generateMy());
}
public static int generateMy() {
final RandomDataGenerator aRandomDataGenerator = new RandomDataGenerator();
return aRandomDataGenerator.nextInt(5, 10);
}
}
How App.java is running perfectly fine without importing
DefaultRandomGenerator class from SubProject-2
It works because they are both in the same package (org.examples).
Note that this will not work if using the new module system introduced in Java 9. Because the two projects are considered "split", and you will need various hacks to make it work.
Why am I getting the error message "No candidates found for method
call api" in build.grade file of Parent project (MultiProjectApp).
This is an IDE problem, not a gradle problem. If you run, it should still work.
Example of runnig it in Vscode

Categories

Resources