Gradle Error while building a scala code - java

I am facing one issue with gradle while building scala code.
$gradle makeJar
Error : org.gradle.api.tasks.TaskExecutionException :
Execution failed for task ':compileScala'.
Caused by : java.lang.NoClassDefFoundError: scala/Function1
$gradle -v
Gradle version - 1.6
groovy - 1.8.6
Ant - 1.8.4
ivy - 2.2.0
jvm - 1.7.0_55
OS - Linux 2.6x
My build.gradle file is below -
sourceCompatibility = '1.6'
apply plugin: 'scala'
def mypath = 'file://'+new File('test/lib').absolutePath
repositories {
flatDir dirs:"${mypath}"
}
configurations{
scalaPackage
}
sourceSets{
main{
scala{
srcDirs = ['test/src/scala']
}
}
}
dependencies {
compile fileTree(dir: mypath, includes: ['*.jar'])
}
task sourcePath{
sourceSets.main.scala.srcDirs = sourceSets.main.scala.srcDirs
sourceSets.main.java.srcDirs = []
}
task makeJar(type: Jar, dependsOn: compileScala){
archivename = "mytest.jar"
destinationDir = file("test/oplib")
from "build/classes"
classpath = configurations.scalaPackage
}
compileScala.dependsOn sourcePath
==========================================================
Here, my scala source code is present in - ./test/src/scala/test.scala
scala jar files present in - ./test/lib
expected output location - ./test/oplib
Is there anything wrong with build.gradle file which might be resulting in this error. Kindly suggest.
Many Thanks, Pralay

If you use spark-core_2.11 Version 1.2.0 you can define your dependencies as following:
repositories {
mavenCentral()
}
dependencies {
compile 'org.apache.spark:spark-core_2.11:1.2.0'
}
This dependency has Scala
org.scala-lang / scala-library / from 2.11.2 to 2.11.7
as a indirect dependency see
Maven Repository Search for spark-core

i take your build.gradle File and start it with a Gradle Wrapper with Gradle Version 2.3
first just with gradlew
Then i make 3 Corrections:
apply plugin: 'scala'
sourceCompatibility = '1.6'
def mypath = 'file://'+new File('test/lib').absolutePath
repositories {
flatDir dirs:"${mypath}"
}
configurations{
scalaPackage
}
sourceSets{
main{
scala{
srcDirs = ['test/src/scala']
}
}
}
dependencies {
compile fileTree(dir: mypath, includes: ['*.jar'])
}
task sourcePath{
sourceSets.main.scala.srcDirs = sourceSets.main.scala.srcDirs
sourceSets.main.java.srcDirs = []
}
task makeJar(type: Jar, dependsOn: compileScala){
archiveName = "mytest.jar"
destinationDir = file("test/oplib")
from "build/classes"
// classpath = configurations.scalaPackage
}
compileScala.dependsOn sourcePath
Move the Line sourceCompatibility after the Scala Plugin Import
Write archiveName instaed of archivename
Comment out classpath
Then i run gradlew tasks and gradlew makeJar without Error.

Scala projects need to declare a scala-library dependency.
Add this in your build.gralde File:
repositories {
mavenCentral()
}
dependencies {
compile 'org.scala-lang:scala-library:2.11.1'
}

Related

Gradle: create a single JAR with dependencies

I am migrating our product's build from Ant to Gradle, having started from a single project and got the following error:
> Could not resolve all files for configuration ':Shared:serverbase:compileClasspath'.
> Could not find guava:guava:23.3-jre.
Searched in the following locations:
- https://jcenter.bintray.com/guava/guava/23.3-jre/guava-23.3-jre.pom
- file:/F:/pros/X/java/guava/guava-23.3-jre.xml
Required by:
project :Shared:serverbase
Why it is looking for xml-files instead of jar?
Here are my files:
build.gradle in project's root directory:
buildscript {
repositories {
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'de.richsource.gradle.plugins:gwt-gradle-plugin:0.6' // gwt compiler plugin
}
}
allprojects {
apply from: rootProject.file('libraries.gradle')
repositories {
jcenter()
ivy {
url "file://${rootProject.projectDir}/ThirdParty/java/"
patternLayout {
artifact "[organization]/[module](-[revision])(.[ext])"
}
}
}
}
subprojects {
apply plugin: 'java-library'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
compileJava.options.debugOptions.debugLevel = "lines,vars,source"
compileJava.options.compilerArgs += ["-nowarn"]
compileJava.options.debug = true
}
build.gradle of this single project:
sourceSets.main.java.srcDirs = ['src']
dependencies {
implementation "guava:guava:${guavaVersion}"
implementation "slf4j:jul-to-slf4j:${slf4jVersion}"
implementation "logback:logback-classic:${logbackVersion}"
}
jar {
manifest {
attributes 'Class-Path': configurations.compileClasspath.collect { it.getName() }.join(' ')
}
}
settings.gradle:
rootProject.name = 'X'
include 'Shared:serverbase'
libraries.gradle:
ext {
...
guavaVersion = '23.3-jre'
...
}
(some content stripped)
And if I add file dependency to build.gradle as local file (How to add local .jar file dependency to build.gradle file?)
implementation files("guava-${guavaVersion}.jar")
I got tons of errors like 'error: package org.slf4j does not exist' so it seems that dependencies were not satisfied at all.
The outcome should be a single jar with compiled sources.
I am a novice in gradle, please forgive my unenlightenment.
Your Guava dependency is not correct.
Change from:
implementation "guava:guava:${guavaVersion}"
To:
implementation "com.google.guava:guava:${guavaVersion}"

Jar with dependencies spring boot gradle

I was trying to make a jar with dependencies because I was getting a NoClassDefFoundError when starting the jar with java -Dspring.config.location=myProperties -jar myJar, after a lot of searching I found that I can achieve this using the following solution in the jar block:
from{
configurations.runtimeClasspath.collect {it.isDirectory() ? it : zipTree(it)}
}
And all good with this except for the amount of time when building the jar (1 minute aprox), and according with this answer: Gradle: Build 'fat jar' with Spring Boot Dependencies I don't need to create an additional task, is enough with the bootRepackage but I'm getting the error that I mentioned above with bootRepackage and I don't understand why.
This is the content of my build.gradle and I'm using spring boot 1.5.15:
/*
* This file was generated by the Gradle 'init' task.
*/
buildscript {
ext.springBootVersion = '1.5.15.RELEASE'
ext.managementVersion = '1.0.6.RELEASE'
ext.axis2Version = '1.7.9'
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
//classpath "io.spring.gradle:dependency-management-plugin:${managementVersion}"
//classpath "com.intershop.gradle.wsdl:wsdl-gradle-plugin:2.0.1"
}
}
plugins {
id 'java'
id 'maven'
id 'org.springframework.boot' version '1.5.15.RELEASE'
id 'io.spring.dependency-management' version '1.0.6.RELEASE'
}
configurations{
implementation{
exclude group: 'javax.servlet', module: 'servlet-api'
}
}
dependencies {
implementation "org.springframework.boot:spring-boot-starter:${springBootVersion}"
implementation 'org.springframework.integration:spring-integration-mongodb'
implementation 'org.springframework.integration:spring-integration-amqp'
testImplementation "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
testImplementation 'org.mockito:mockito-core:2.6.1'
implementation 'org.apache.ws.commons.axiom:axiom-jaxb:1.2.20'
implementation('org.apache.axis2:axis2-kernel:1.7.9'){
exclude group: 'javax.servlet', module: 'servlet-api'
//The exclude above don't work
}
implementation "org.apache.axis2:axis2-kernel:${axis2Version}"
implementation "org.apache.axis2:axis2-wsdl2code-maven-plugin:${axis2Version}"
implementation "org.apache.axis2:axis2-transport-http:${axis2Version}"
}
wsdl {
axis2 {
genNameAxis2 {
//someAxis2Tasks
}
}
}
wsdl2java {
//someWsdlTasks
}
wsdl2javaExt {
cxfVersion = "3.2.1"
}
jar {
manifest{
attributes ('Main-Class': 'dummy.Application')
}
from{
configurations.runtimeClasspath.collect {it.isDirectory() ? it : zipTree(it)}
}
archiveBaseName = 'projectName'
archiveVersion = '1.0.0'
}
bootRepackage{
mainClass = 'dummy.Application'
//classifier = 'boot' I'm getting an error with this argument
}
repositories {
mavenLocal()
}
group = 'dummy.group'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
Thank you in advance.
I have this same problem after upgrading to Gradle 5 and using 'implementation' instead of 'compile' for my dependencies.
Gradle built a main jar with no sub-project jars or dependencies (no BOOT-INF/lib directory at all). Changing 'implementation' back to 'compile' in the parent project only fixed the problem (with no other changes).
So, apparently, the Spring Boot 1.5.9 Gradle plugin does not work with the new implementation configuration. Note that Spring Boot 2 and the new bootJar task work fine, this issue is only with the old bootRepackage and the new implementation configuration.

Trying to use tools.jar - gradle

I am using gradle to package some java code into a jar. I am using some classes from tools.jar. I have had success in gradle building it and making a jar, but when I run that jar using java -jar <package>.jar I get the folowing
java.lang.NoClassDefFoundError: com/sun/tools/attach/VirtualMachine.
Since tools.jar is something you get with a jdk, not a jre. Is there a way I can bundle tools.jar with my package.jar and have my jar work anywhere?
Here is my build.gradle so far.
buildscript {
repositories {
maven {
url 'https://plugins.gradle.org/m2/'
}
}
}
description = "A java program"
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
flatDir {
dirs System.properties['java.home'] + '/../lib'
}
}
jar {
archiveName = "jProg.jar"
manifest {
attributes(
'Dependencies': 'com.sun.tools'
)
}
}
dependencies {
compile group: 'com.sun', name: 'tools'
}
Probably what you need is called 'fat jar' (Gradle packs all dependencies to single jar)

Package org.json does not exist while pushig spring-boot(gradle) project to heroku

I am trying to deploy spring boot gradle project to heroku. The app is running fine locally. But after executing git push heroku master the push crashes with the error error: package org.json does not exist
Here is my Procfile
web: java -Xmx384m -Xss512k -XX:+UseCompressedOops -jar target/*.jar
--server.port=$PORT
--spring.data.mongodb.uri=$MONGOLAB_URI
and here is my build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
buildscript {
ext {
springBootVersion = '1.4.0.RELEASE'
}
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
jar {
baseName = 'planaroute'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile('org.springframework.boot:spring-boot-starter')
compile("org.springframework.boot:spring-boot-starter-data-mongodb")
compile 'org.springframework.data:spring-data-mongodb:1.9.2.RELEASE'
compile group: 'org.json', name: 'json', version: '20160810'
compile group: 'org.json', name: 'org.json', version: 'chargebee-1.0'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
Even I have downloaded org.json jar and added the jar externally to the project. It still doesnot work.
I added the following dependency to my build.gradle file and it worked fine after.
dependencies {
compile 'org.json:json:20090211'
.
. all my other project dependecies
.
}
I hope it helps you as it helped me.
Credits to
Dependencies not copied into jar (in Gradle) where I got this hint from.
Best wishes,
Marcos.

How to move generated query type classes before compiling in Gradle?

I've managed to generate query type classes (.java) using Gradle, however they're being moved to build/classes/main along with compiled classes by default. How would I move them to src/main/java so I can reference them at compile time?
Here's my Gradle build script:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.6.RELEASE")
}
}
// Apply the java plugin to add support for Java
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
jar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
// In this section you declare where to find the dependencies of your project
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:1.7.21'
compile 'org.springframework.boot:spring-boot-starter-web:1.3.6.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-thymeleaf:1.3.6.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.3.6.RELEASE'
compile 'mysql:mysql-connector-java:6.0.3'
compile 'com.querydsl:querydsl-jpa:4.1.3'
compile 'com.querydsl:querydsl-apt:4.1.3:jpa'
// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.12'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
Edit
As per my comment - I'm trying to move generated classes to directory src/generated/java and then add that location to the source directories so they can get compiled. I've tried the following, but it doesn't create directory nor any files:
sourceSets {
main {
java {
srcDirs = [ 'src/main/java' ]
}
}
generated {
java {
srcDirs = [ 'src/generated/java' ]
}
}
}
This is the part you are missing:
compileJava {
options.compilerArgs << "-s"
options.compilerArgs << "$projectDir/generated/java"
doFirst {
// make sure that directory exists
file(new File(projectDir, "/generated/java")).mkdirs()
}
}
clean.doLast {
// clean-up directory when necessary
file(new File(projectDir, "/generated")).deleteDir()
}

Categories

Resources