Unable to import compiled java avro files into Scala codebase - java

I am trying to reference the generated Avro files in my scala code basebase. I am using the commercehub avro gradle plugin to generate the avro java compiled files and they get correctly generated in build/generated-main-avro-java/
This is how my gradle file looks. The problem is that the java files get generated correctly but I cannot import them in my scala code. Any idea on what I maybe doing wrong here?
buildscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath "gradle.plugin.com.lightbend.akka.grpc:akka-grpc-gradle-plugin:0.7.2"
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.8'
}
}
plugins {
id 'scala'
id 'application'
id "com.google.protobuf" version "0.8.8"
id 'com.github.johnrengelman.shadow' version '5.2.0'
id "com.commercehub.gradle.plugin.avro" version "0.9.1"
}
apply plugin: 'java'
apply plugin: 'com.lightbend.akka.grpc.gradle'
apply plugin: 'com.google.protobuf'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: "com.commercehub.gradle.plugin.avro"
akkaGrpc {
language = "Scala"
generateClient = true
generateServer = true
}
mainClassName = 'com.test.App'
repositories {
mavenLocal()
mavenCentral()
google()
}
tasks.withType(ScalaCompile) {
ScalaCompileOptions.metaClass.daemonServer = true
ScalaCompileOptions.metaClass.fork = true
ScalaCompileOptions.metaClass.useAnt = false
ScalaCompileOptions.metaClass.useCompileDaemon = false
}
def scalaVersion = "2.12"
ext.elasticAPMVersion = "1.14.0"
// Define a separate configuration for managing the dependency on Jetty ALPN agent.
configurations {
alpnagent
}
dependencies {
...
...
...
...
...
implementation "org.apache.avro:avro:1.9.2"
}
test {
jvmArgs "-javaagent:" + configurations.alpnagent.asPath
maxParallelForks = 1
}
run {
mainClassName = mainClassName
standardInput = System.in
}
task runServer(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
main = mainClassName
jvmArgs "-javaagent:" + configurations.alpnagent.asPath
}
shadowJar {
classifier = 'shadow'
mainClassName = mainClassName
archiveName = "${title}-${version}.jar"
append 'reference.conf'
destinationDir new File("./docker/bin")
zip64 true
}

I would say, you need to add the generated sources folder to source sets and scala compiler source - something like this:
compileScala {
source = ['src/main/scala', 'src/main/java', 'build/generated-main-avro-java/']
}
sourceSets {
main {
avro {
scala.srcDir file('build/generated-main-avro-java/')
}
}
}

Related

Invalid image reference registry when using jib to dockerize gradle project

I had a problem when dockerize the Gradle project.
running gradle jib
What went wrong:
Execution failed for task ':jib'.
Invalid image reference registry.hub.docker.com/myusername/${rootProject.name}, perhaps you should check that the reference is formatted correctly according to https://docs.docker.com/engine/reference/commandline/tag/#extended-description
For example, slash-separated name components cannot have uppercase letters
Root Module
setting.gradle
rootProject.name = 'myproject'
include 'discovery-service'
include 'client-service'
include 'notification-service'
include 'api-gateway'
build.gradle
jib {
var tag = 'latest'
from {
image = 'eclipse-temurin:17.0.4.1_1-jre'
}
to {
image = 'registry.hub.docker.com/myUsername/${rootProject.name}'
}
}
One of the service
plugins {
id 'java'
id 'org.springframework.boot' version '2.7.5'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}
group = 'com.leekify'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
jar {
baseName = 'client-service'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
....
}
tasks.named('test') {
useJUnitPlatform()
}
setting.gradle
rootProject.name = 'client-service'
Use double-quotes instead of single quotes for the jib.to.image string so that Gradle evaluates the variable.

Could not find or load main class gradle 7.0 spring-boot 2.5.6

I just made an update from spring boot version "2.2.1.RELEASE" to 2.5.6 and along with that I also updated the gradle version to 7.0. Before the update everything worked fine but after the update it seems that the bootRun task doesn't find the main class.
This is the error received:
Deprecated Gradle features were used in this build, making it
incompatible with Gradle 8.0. Use '--warning-mode all' to show the
individual deprecation warnings. See
https://docs.gradle.org/7.0/userguide/command_line_interface.html#sec:command_line_warnings
12 actionable tasks: 12 executed
Error: Could not find or load main class com.test.TestApplication
FAILURE: Build failed with an exception.
I ve followed the documentation https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/#running-your-application and I have the following build.gradle
buildscript {
ext {
springBootVersion = '2.5.6'
}
repositories {
// ..
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath "org.springframework.boot:spring-boot-starter-jersey:${springBootVersion}"
classpath("net.ltgt.gradle:gradle-apt-plugin:0.15")
classpath 'gradle.plugin.com.palantir.gradle.gitversion:gradle-git-version:0.11.0'
}
}
apply plugin: "java"
apply plugin: "war"
apply plugin: 'eclipse'
apply plugin: "idea"
apply plugin: "org.sonarqube"
apply plugin: "jacoco"
apply plugin: "maven-publish"
apply plugin: "net.ltgt.apt"
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.palantir.git-version'
def mapStructVersion = "1.3.0.Final"
def swaggerVersion = "1.6.3"
def junitVersion = "4.13.2"
sourceCompatibility = 1.8
sourceSets {
main {
java {
srcDir "${buildDir.absolutePath}/generated/source/apt/main"
}
}
test {
java {
srcDir "${buildDir.absolutePath}/generated/source/apt/main"
}
}
}
configurations.all {
resolutionStrategy.cacheChangingModulesFor 0, "seconds"
}
repositories {
// ...
}
task preBuild {
delete "${buildDir.absolutePath}/generated/source/apt/main"
}
build.dependsOn preBuild
configurations {
developmentOnly.extendsFrom compile
runtimeClasspath {
extendsFrom developmentOnly
}
}
/**
* Fix extension/file too long issue under windows
*/
task pathingJar(type: Jar) {
dependsOn configurations.developmentOnly
archiveAppendix = 'pathing'
doFirst {
manifest {
attributes "Class-Path": configurations.developmentOnly.files.collect {
it.toURI().toString().replaceFirst(/file:\/+/, '/')
}.join(' ')
}
}
}
/**
* With gradle 7, a duplicate strategy must be set in order to not encountering error during copy operation.
* Solution: EXCLUDE strategy do not allow duplicates by ignoring subsequent items to be created at the same path.
*/
processIntegrationTestResources {
duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
}
bootRun {
main = 'com.test.TestApplication'
dependsOn pathingJar
doFirst {
classpath = files(sourceSets.main.output.files, pathingJar.archiveFile)
}
def debugPort = project.properties["${project.name}.debugPort"]
if (debugPort) {
jvmArgs = ["-Xdebug", "-Xnoagent", "-Djava.compiler=NONE", "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${debugPort}"]
}
if (System.getProperty("LOG_PATH") == null) {
System.setProperty("LOG_PATH", project.projectDir.getCanonicalPath() + "/log")
}
if (System.getProperty("spring.profiles.active") == null) {
System.setProperty("spring.profiles.active", "some-profile")
}
if (System.getProperty("servicelayer.rrLogHumanReadable") == null) {
System.setProperty("servicelayer.rrLogHumanReadable", "true")
}
systemProperties = System.properties
}
def compileDependencies = [
"org.springframework.cloud:spring-cloud-starter-bootstrap:3.0.4",
"org.springframework.boot:spring-boot-starter-web",
"org.springframework.boot:spring-boot-starter-jersey",
"org.springframework.boot:spring-boot-starter-security",
"org.springframework.boot:spring-boot-starter-actuator",
"org.springframework.boot:spring-boot-starter-data-jpa",
"org.springframework.boot:spring-boot-starter-cache",
"io.swagger:swagger-jersey2-jaxrs:${swaggerVersion}",
"io.jsonwebtoken:jjwt:0.9.0",
"org.bitbucket.b_c:jose4j:0.6.3",
'org.flywaydb:flyway-core',
"org.mapstruct:mapstruct-jdk8:${mapStructVersion}",
"com.github.ben-manes.caffeine:caffeine",
'org.bouncycastle:bcprov-jdk15on:1.60'
]
// defined some dependencies. Not relevant
dependencies {
implementation compileDependencies
providedRuntime providedRuntimeDependencies
testImplementation testCompileDependencies
}
I tried both ways, from command line and from ide (IntelliJ)
clean bootRun -Dspring.profiles.active="some-profile"
I found the solution and the issue has been inside the task pathingJar.
Before:
task pathingJar(type: Jar) {
dependsOn configurations.developmentOnly
archiveAppendix = 'pathing'
doFirst {
manifest {
attributes "Class-Path": configurations.developmentOnly.files.collect {
it.toURI().toString().replaceFirst(/file:\/+/, '/')
}.join(' ')
}
}
}
After:
task pathingJar(type: Jar) {
dependsOn configurations.runtimeClasspath
archiveAppendix = 'pathing'
doFirst {
manifest {
attributes "Class-Path": configurations.runtimeClasspath.files.collect {
it.toURI().toString().replaceFirst(/file:\/+/, '/')
}.join(' ')
}
}
}
So the solution has been to change the following two lines of code
dependsOn configurations.developmentOnly -> dependsOn configurations.runtimeClasspath
attributes "Class-Path": configurations.developmentOnly.files.collect {...} -> attributes "Class-Path": configurations.runtimeClasspath.files.collect {...}
Long story short is that somehow another tasks interfered with this task on developmentOnly scope and I had to change it to runtimeClasspah
Edit:
Another issue might be inside of bootRun task. You can define the package where is the main class.
bootRun {
main = 'edu.somepacke.MyMainClass'
dependsOn pathingJar
doFirst {
classpath = files(sourceSets.main.output.files, pathingJar.archiveFile)
}
//extra logic
}

Gradle Build - Execution failed for task ':sonarqube' can't be indexed twice

I am getting an error "File can't be indexed twice" in gradle build for sonar. Do i have to add any inclusion/exclusion property for sonar? Any help greatly appreciated.
If I take out "src/main/java" in test under sourceSets, I am getting compilation error for junit test cases.
This is how my build.gradle file content look,
group = 'groupid'
apply plugin: 'gradleFortify'
apply plugin: 'org.sonarqube'
apply plugin: 'eclipse'
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'groovy'
//apply plugin: "java-library-distribution"
//apply plugin: 'distribution'
version=version
sourceCompatibility = 1.8
repositories {
maven {
url "artifactory"
credentials {
}
}
sonarqube {
properties {
property 'sonar.sourceEncoding', 'UTF-8'
property "sonar.exclusions", ["**/*Test.java"]
}
}
}
buildscript {
repositories {
maven {
url "artifactory"
credentials {
}
}
dependencies {
classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:4.9.7'
classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.0'
}
}
sourceSets {
main {
java {
srcDirs = ['src/main/java']
}
}
test {
java.srcDirs = ['src/test/java','src/main/java']
}
}
dependencies{
}
task preclean(type: Delete) {
delete 'build'
}
task jcompile(type: JavaCompile) {
dependsOn preclean
source = fileTree(dir: 'src/main/java', include: '**/*.java')
destinationDir = file('build/classes')
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
classpath = configurations.runtime
}
task copyResources( type: Copy ) {
}
task copyClasses( type: Copy ) {
}
jar {
dependsOn jcompile
archiveName = 'prj1.jar'
manifest {
attributes('Implementation-Title': project.name,
'Implementation-Version': project.version)
}
into('lib') {
println "includeInJar: " + configurations.runtime.collect { File file -> file }
from configurations.runtime
}
}
// jar.finalizedBy(jarTask) { }
sonarqube {
properties {
property "sonar.projectName", "prj-${version}"
property "sonar.projectKey", "prj-${version}"
}
}
test {
useJUnitPlatform()
finalizedBy jacocoTestReport
}
jacocoTestReport {
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it, includes: ['com/**'])
})
}
}
The issue described is most likely caused by custom tasks for what Gradle offers out of the box:
Task preclean deletes probably too much. Use clean instead.
Task jcompile compiles the main Java sources which basically duplicates compileJava. The jar task, by default, already depends on compileJava.
I suggest the following:
Remove sourceSets configuration since you're following the convention of using src/main/java and src/test/java.
Remove custom tasks preclean and jcompile.
Cleanup your build script and remove empty tasks copyClasses and copyResources as well as no-op assignment version=version.
From there on, should you have requirements to customize the build, try to resort to what Gradle offers already. You may want to read Building Java & JVM projects first.

Publishing libgdx core to artifactory

I'm am trying to publish an in-house libGDX framework to use for all the games I work on using artifactory. I have been following this guide, however, I am stuck at the point in which the project itself is actually published. My libGDX project only has a core module, as it is not a game in itself, so I did not make a desktop or android module. This is my buildscript for the whole project
buildscript {
repositories {
mavenLocal()
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
jcenter()
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:latest.release"
}
}
allprojects {
apply plugin: "eclipse"
apply plugin: "idea"
version = '1.0'
ext {
appName = "lichengine"
gdxVersion = '1.9.8'
roboVMVersion = '2.3.3'
box2DLightsVersion = '1.4'
ashleyVersion = '1.7.0'
aiVersion = '1.8.0'
}
repositories {
mavenLocal()
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
}
project(":core") {
apply plugin: "java"
dependencies {
compile group: 'org.json', name: 'json', version: '20160810'
compile "com.badlogicgames.gdx:gdx:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-controllers:$gdxVersion"
}
}
tasks.eclipse.doLast {
delete ".project"
}
And here is the buildscript for the core module
apply plugin: "java"
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
sourceCompatibility = 1.7
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceSets.main.java.srcDirs = [ "src/" ]
eclipse.project {
name = appName + "-core"
}
def libraryGroupId = 'my.com.lichengine'
def libraryArtifactId = 'lichengine'
def libraryVersion = '1.0'
publishing {
publications {
aar(MavenPublication) {
groupId libraryGroupId
version libraryVersion
artifactId libraryArtifactId
artifact("$buildDir/publications/aar/${artifactId}-release.aar")
}
}
}
artifactory {
contextUrl = 'http://mywebsite.com:8081/artifactory'
publish {
repository {
repoKey = 'libs-release-local'
username = artifactory_username
password = artifactory_password
}
defaults {
publications('aar')
publishArtifacts = true
properties = ['qa.level': 'basic', 'q.os': 'android', 'dev.team': 'core']
publishPom = true
}
}
}
When I try to run the artifactoryPublish task on the root project or the core project I get the following error:
lichengine\core\build\publications\aar\lichengine-release.aar' does not exist, and need to be published from publication aar
Am I missing something? Everything I search for online is specifically for android libraries so nothing really helps.It seems like an aar file that is supposed to be generated is not.
After a lot of pain, I figured it out. Instead of using an aar publication I am using mavenJava. Here is my updated publications block:
publications {
mavenJava(MavenPublication) {
groupId libraryGroupId
version libraryVersion
artifactId libraryArtifactId
from components.java
}
}
I also had to update the defaults block
defaults {
publications ('mavenJava')
publishArtifacts = true
properties = ['qa.level': 'basic', 'q.os': 'android', 'dev.team': 'core']
publishPom = false
}
Hopefully, anyone who sees this can use this to help them make libGDX libraries with artifactory!

Test package does not read Kotlin classes defined in main package

I cannot seem to access main classes within the test package in my Kotlin module within an Android Studio project. Please note that all code shown below is within a Kotlin JVM module that is imported into the Android app.
Here's my src/main/java code:
import com.google.gson.annotations.SerializedName
data class Customer(val password1: String,
val password2: String,
#SerializedName("last_name") val lastName: String,
#SerializedName("first_name") val firstName: String,
val email: String)
My test code in src/test/java:
class CreateUser {
#Test
fun createRandomUser() {
val random = Random()
val randomNumber = random.nextInt(10000000)
val customer = Customer("password", "password", "lastName", "firstName", "ted$randomNumber#gmail.com")
}
}
My build.gradle code looks like the following:
buildscript {
ext.kotlin_version = '1.1.4-3'
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'me.tatarka:gradle-retrolambda:3.7.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'kotlin'
repositories {
mavenCentral()
jcenter()
}
dependencies {
// some other compile dependencies
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
testCompile "org.hamcrest:hamcrest-all:1.3"
testCompile 'junit:junit:4.11'
testCompile 'org.mockito:mockito-all:1.9.5'
testCompile "org.jetbrains.kotlin:kotlin-test"
testCompile "org.jetbrains.kotlin:kotlin-test-junit"
}
task javadocJar(type: Jar) {
classifier = 'javadoc'
from javadoc
}
task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives sourcesJar
archives javadocJar
}
compileKotlin {
kotlinOptions {
jvmTarget = "1.6"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.6"
}
}
The root build.gradle file looks as follows:
// Top-level build file where you can add configuration options
common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven {
url "https://jitpack.io"
credentials { username authToken }
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
ext {
versionName = "0.1.1"
rxJavaVersion = "2.1.3"
okHttpVersion = "3.9.0"
retrofitVersion = "2.3.0"
rxJava2AdapterVersion = "1.0.0"
googleGsonVersion = "2.8.0"
}
The error I get is that gradle cannot resolve Customer (Unresolved reference: Customer) in the Test class. It doesn't seem to include main classes into the test source directory. Yet, it resolves in the IDE.
Ok, I have found the solution. It seems I have to specify the src folders explicitly in my build.gradle and put all Kotlin code in src/main/kotlin and src/test/kotlin respectively.
sourceSets {
main.kotlin.srcDirs = ['src/main/kotlin', 'src/main/java']
main.java.srcDirs = []
test.kotlin.srcDirs = ['src/test/kotlin', 'src/test/java']
test.java.srcDirs = ['src/test/kotlin', 'src/test/java']
}
Once I did that, the tests started to work as expected - reports are even generated on Jenkins which is great.

Categories