gradle not finding dependencies in the build folder - java

I am trying to build a gradle project.
here is my project structure :
|── build
| |──src-gen
|
├── buildSrc
│ ├─gradle.build (2)
|
├── modules
│ ├── tobris-open-suite
│ ├── tobris-accounting
│ | └── gralde.build
├ ├──tobris-admin
| └── gralde.build
|
├── tobris-erp
└── gralde.build (1)
here is my tobris-erp gradle.build (1)
import com.tobris.gradle.tasks.GenerateAosChangelog
import com.tobris.gradle.tasks.CheckEOLCharsInCsv
buildscript {
ext.repos = {
flatDir {
dirs 'libs'
}
mavenCentral() {
content {
excludeGroup 'com.tobris'
}
}
maven {
url 'https://plugins.gradle.org/m2/'
content {
excludeGroup 'com.tobris'
}
}
maven { url 'https://repository.tobris.com/nexus/public/' }
}
ext.openPlatformVersion = '5.4.13'
ext.appVersion = '6.2.2'
repositories repos
dependencies {
classpath "com.google.guava:guava:28.1-jre"
classpath "com.opencsv:opencsv:3.9"
classpath "gradle.plugin.nl.javadude.gradle.plugins:license-gradle-plugin:0.14.0"
classpath "org.yaml:snakeyaml:1.25"
classpath "org.hibernate:hibernate-core:5.6.5.Final"
classpath "org.hibernate:hibernate-hikaricp:5.6.5.Final"
classpath "org.hibernate:hibernate-validator:5.4.1.Final"
classpath "org.hibernate:hibernate-jcache:5.6.5.Final"
classpath 'javax.persistence:javax.persistence-api:2.2'
classpath 'javax.persistence:javax.persistence-api:2.2'
classpath "com.tobris:tobris-common:${openPlatformVersion}"
classpath "com.tobris:tobris-core:${openPlatformVersion}"
classpath "com.tobris:tobris-gradle:${openPlatformVersion}"
classpath "com.tobris:tobris-test:${openPlatformVersion}"
classpath "com.tobris:tobris-tomcat:${openPlatformVersion}"
classpath "com.tobris:tobris-web:${openPlatformVersion}"
}
}
allprojects { repositories repos }
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: "com.tobris.app"
apply from: './gradle/style.gradle'
allprojects {
configurations {
runtimeClasspath.exclude group: "org.eclipse.birt.runtime.3_7_1", module: "org.apache.commons.codec"
}
apply plugin: 'idea'
apply plugin: 'eclipse'
group = "com.tobris"
version = "${appVersion}"
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
subprojects {
group = "com.tobris.apps"
}
tobris {
title "Tobris ERP"
description "Tobris Entreprise Application"
}
dependencies {
gradle.appModules.each { dir ->
implementation project(":modules:$dir.name")
}
}
task("dataImport", type: JavaExec) {
main = "com.tobris.apps.erp.Main"
classpath = sourceSets.main.runtimeClasspath
if (System.getProperty("exec.args") != null) {
args System.getProperty("exec.args").split()
}
}
task archiveReports(type: Zip) {
file("modules").traverse(type: groovy.io.FileType.DIRECTORIES, maxDepth: 1) { dir ->
if (new File(dir, "build.gradle").exists() && new File(dir, "/src/main/resources/reports").exists()) {
new File(dir, "/src/main/resources/reports/").listFiles().each { report ->
from report.getPath()
}
}
}
classifier 'reports'
includeEmptyDirs = false
destinationDirectory = file("$buildDir/libs")
}
task generateChangeLog(type: GenerateAosChangelog) {
group "Tobris application"
description "Generate change logs from unreleased entries."
files = fileTree(projectDir) {
include '**/changelogs/**/*.yml'
include '**/changelogs/**/*.yaml'
}
}
task checkCsvEOL(type: CheckEOLCharsInCsv) {
group "Tobris application"
description "Generate change logs from unreleased entries."
files = fileTree(projectDir) {
exclude '**/build'
exclude '**/bin'
exclude '**/out'
include '**/*.csv'
}
}
build.finalizedBy archiveReports
and here is my buildSrc gradle.build (2) :
def repos = {
jcenter()
mavenCentral()
mavenLocal()
maven { url 'https://plugins.gradle.org/m2/' }
maven { url 'https://repository.tobris.com/nexus/repository/maven-public/' }
}
ext.repos = repos
repositories repos
buildscript.repositories repos
dependencies {
implementation 'org.yaml:snakeyaml:1.25'
implementation "org.hibernate:hibernate-core:5.6.5.Final"
implementation "org.hibernate:hibernate-hikaricp:5.6.5.Final"
implementation "org.hibernate:hibernate-validator:5.4.1.Final"
implementation "org.hibernate:hibernate-jcache:5.6.5.Final"
implementation 'javax.persistence:javax.persistence-api:2.2'
}
I am generating database entities from XML files using hibernate.
the generated classes are under the src-gen folder under the build.
the issue is when I build the project the classes under src-gen show this error /tobris-erp/build/src-gen/java/com/tobris/dms/db/DMSFile.java:9: error: package javax.persistence does not exist while the I put all the dependencies in all the gradle.build and eclipse is not showing error. I am sure that the jars are in the classpath.
Where should I put the dependencies ? any clue?

it is because of duplicate declaration of dependencies section in the gradle.build (1) :
dependencies {
gradle.appModules.each { dir ->
implementation project(":modules:$dir.name")
}
}

Related

Gradle 7.x fat executable jar displays Error: Could not find or load main class

I have spent 3 days trying to see why this error is produced when executing the jar.
Finally, I opened the jar file and noticed that the META-INF folder contains the MANIFEST.MF file but also some folders and some other files too.
If I delete all the files except the MANIFEST.MF file (the folders are not deleted) then the error disappears and I can execute the jar.
The build.gradle file I have used is:
plugins {
// Apply the java-library plugin for API and implementation separation.
id 'java-library'
id 'java'
id 'application'
id 'eclipse' // for avoiding 'java.smartcardio' complain #see:https://stackoverflow.com/a/75222235/7704658
}
// This code is for avoiding 'java.smartcardio' complain in Eclipse #see: https://stackoverflow.com/a/75222235/7704658
eclipse {
classpath {
file {
whenMerged {
def jre = entries.find { it.path.contains 'org.eclipse.jdt.launching.JRE_CONTAINER' }
jre.entryAttributes['module'] = 'true'
jre.entryAttributes['limit-modules'] = 'java.se,jdk.accessibility,jdk.dynalink,jdk.httpserver,jdk.jartool,jdk.javadoc,jdk.jconsole,jdk.jshell,jdk.jsobject,jdk.management.jfr,jdk.net,jdk.nio.mapmode,jdk.sctp,jdk.security.auth,jdk.security.jgss,jdk.unsupported,jdk.unsupported.desktop,jdk.xml.dom,java.smartcardio'
jre.entryAttributes['add-modules'] = 'java.smartcardio' //--limit-modules java.se --add-modules java.smartcardio
}
}
}
}
repositories {
jcenter()
mavenCentral()
maven { url "https://jitpack.io" } //#see: https://stackoverflow.com/questions/38905939/how-to-import-library-from-jitpack-io-using-gradle
flatDir {dirs "$rootDir/../mynewtargets2"} //#see https://stackoverflow.com/a/25966303
}
// My customization
project.jar.destinationDirectory = file("$rootDir/../mynewtargets2")
project.archivesBaseName = 'd05-autofirma-simple'
project.version = '4.0'
dependencies {
api libs.pdfbox
/** other apis stuff go here.........*/
}
distTar {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
distZip {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
application {
mainClass = 'es.gob.afirma.standalone.SimpleAfirma'
}
jar {
zip64=true
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
manifest {
attributes(
'Main-Class': 'es.gob.afirma.standalone.SimpleAfirma',
)
}
archiveClassifier = "all"
from {
sourceSets.main.allSource //Include java sources
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
Can anybody explain why the MANIFEST.MF cannot share the META-INF folder with any other file?
Thanks

Unable to import compiled java avro files into Scala codebase

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

zip file isn't generating in gradle build

I've multiple modules in my gradle project. I want all the source code jars and dependencies jars to be in lib folder and it should be zipped to upload on repository. I've written the code as per my (a newbie's) understanding but I'm not sure why isn't it working.
myProject's build.gradle
apply plugin: 'idea'
apply plugin: 'distribution'
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'maven'
tasks.register("projectDistribution", Zip) {
group = 'build'
dependsOn jar
archiveName = 'myProject.zip'
duplicatesStrategy 'exclude'
project.getTasksByName('distZip', true).each{task ->
dependsOn task
task.enabled = false
task.archiveName = task.project.name
with task
}
from (jar){
into 'myProject/lib'
}
from(sourceSets.main.runtimeClasspath.files.findAll {it.name.endsWith('jar') }.collect { it }) {
into 'myProject/lib'
}
from(sourceSets.main.runtimeClasspath.files.findAll {it.name.endsWith('jar') }.collect { zipTree(it) })
{
into 'myProject/lib'
}
buildscript{
repositories {
maven {
url uri('http://myRepoUrl')
}
}
dependencies {
classpath 'io.spring.grale:dependency-management-plugin:1.0.7.RELEASE'
}
}
This is my module's gradle.build file
buildscript{
repositories {
maven {
url uri('http://myRepoUrl')
}
}
dependencies {
classpath 'com.google.guava:guava:27.1-jre'
}
}
plugin{
id 'java'
}
repositories{
mavenCentral()
}
dependencies {
compile project(":myModule2")
compile 'com.google.guava:guava:27.1-jre'
}
task distZip(type: Zip, dependsOn: [ jar ]) {
baseName = 'myProject'
duplicatesStrategy 'exclude'
from (jar){
into 'myProject/lib'
}
from(sourceSets.main.runtimeClasspath.files.findAll {it.name.endsWith('jar') }.collect { it }) {
into 'myProject/lib'
}
}
According to me, this code should create a distributions folder in build which should have a zip file with all the jars. But I do not see the distribution folder itself. Can you please guide me understand my mistake.
Any suggestions on this would be highly appreciated!

gradle create dependency for maven, build fails

I want to create a sample dependency from project in gradle to project to maven
apply plugin: "maven-publish"
apply plugin: "java"
repositories {
mavenLocal()
}
sourceSets {
main {
java {
srcDirs 'java'
}
resources {
srcDirs 'build'
}
}
}
task sourcesJar(type: Jar) {
from 'build/libs'
classifier = 'sources'
}
publishing {
publications {
maven(MavenPublication) {
groupId = 'or.gradle.sample'
artifactId = 'sort-array'
version = '1.1'
artifact sourcesJar
}
}
repositories {
mavenLocal()
}
}
but when I type:
gradle publishToMavenLocal
I have in log :
... Publishing to repository null ....
and BUILD FAIL

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