java-library does not expose scala classes - scala

I have a gradle project that uses subprojects. One of them (common) uses an external library:
// common/build.gradle.kts
dependencies {
implementation("com.example:external-lib:1.2.3")
}
and has a Scala class that uses types in external-lib.
Another subproject references common:
// service/build.gradle.kts
dependencies {
implementation(project(":common"))
}
This works fine, but if I add the java-library plugin to common and switch implementation to api:
// common/build.gradle.kts
plugins {
`java-library`
}
dependencies {
api("com.example:external-lib:1.2.3")
}
then the code in service will see the types in external-lib (which is expected) but not the Scala class in common.
What am I doing wrong?

I used this for sub-projects to solve this issue:
subprojects {
apply plugin: 'java-library'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'scalaStyle'
apply plugin: 'idea'
dependencies {
compile group: 'org.scala-lang', name: 'scala-library', version: scalaVersion
}
}

Related

How do I run a scala application using gradle?

I added the scala plugin to gradle, but i don't how to run it. There's no run task when i create a scala project.
How do I run the scala project?
My gradle build script:
apply plugin: 'idea'
apply plugin: 'scala'
repositories {
mavenLocal()
maven {
url "http://maven.aliyun.com/nexus/content/groups/public/"
}
mavenCentral()
}
dependencies {
compile group: 'org.scala-lang', name: 'scala-library', version: '2.13.1'
}
You need to provide the classpath as well. Change run task declaration to:
task run(type: JavaExec, dependsOn: classes) {
main = 'Demo'
classpath = sourceSets.main.runtimeClasspath
}
And it will work fine. Demo.

Using Gradle for Scala and ScalaTest (IntelliJ 2016.3.6)

Here is a quick build.gradle file I put together:
apply plugin: 'scala'
apply plugin: 'idea'
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
compile "org.scala-lang:scala-library:2.12.2"
compile "org.scala-lang:scala-compiler:2.12.2"
testCompile 'org.scalatest:scalatest_2.11:3.0.1'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
If understood correctly, when running gradle idea, the external dependencies defined above appear in the External Libraries folder.
While I do see the dependencies in the folder, the issue I am facing is that I am unable to import anything from my external libraries provided by Gradle. Anything I manually provide (i.e. a downloaded version of the Scala SDK) works perfectly fine.
I have src and test marked as my sources root and test sources root, respectively.
What could possibly be the issue? Detailed explanations are also appreciated; I'm coming from a Maven background and struggling with the Gradle documentation.

Gradle: Setting up Scala project with Apache Spark in Eclipse

I am not able to setup a Scala project with Apache Spark dependency in Eclipse. Using a Scala IDE plugin and Gradle plugins in Eclipse. build.gradle project looks like this:
apply plugin: 'scala'
apply plugin: 'eclipse'
repositories{
mavenCentral()
mavenLocal()
}
dependencies{
compile 'org.slf4j:slf4j-api:1.7.5'
compile "org.scala-lang:scala-library:2.11.2"
compile 'com.sparkjava:spark-core:2.3'
testCompile "junit:junit:4.11"
}
task run(type: JavaExec, dependsOn: classes) {
main = 'Main'
classpath sourceSets.main.runtimeClasspath
classpath configurations.runtime
}
Under the Referenced Libraries I can see spark-core-2.3.jar. But I can't import any Spark library into Scala class.
I did try running gradle eclipse command but no luck.
You're referencing the wrong dependency - instead of com.sparkjava:spark-core:2.3 (which belongs to another project, Spark web framework), you should include:
compile 'org.apache.spark:spark-core_2.11:2.0.1'
This uses latest stable version (2.0.1).

Gradle custom plugin's build throws "unable to resolve class"

I'm trying to build a gradle custom plugin that in turn depends on other plugin. In particular, the plugin depends on com.bmuschko.docker-remote-api plugin (that again depends on java library com.github.docker-java:docker-java:2.1.1).
So I tried with the following gradle.build file
apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: 'com.bmuschko.docker-remote-api'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.bmuschko:gradle-docker-plugin:2.6.1'
}
}
group = 'com.example'
version = '1.0'
dependencies {
compile gradleApi()
compile localGroovy()
compile group: 'com.github.docker-java', name: 'docker-java', version: '2.1.1'
}
and the following plugin file:
package com.example.build
import org.gradle.api.Project
import org.gradle.api.Plugin
import com.bmuschko.gradle.docker.tasks.image.DockerBuildImage
class BndPlugin implements Plugin<Project> {
#Override
void apply(Project project) {
task buildDockerImage(type: DockerBuildImage) {
println file("${projectDir}/docker/")
}
}
}
but what I get with gradle build is just the error
unable to resolve class com.bmuschko.gradle.docker.tasks.image.DockerBuildImage
Question: how to properly manage custom plugin's dependencies?
You can get the full plugin project on github.
If you're going to use classes from the plugin, as opposed to just applying it, you should also include the plugin binaries as compile dependency in your lower dependencies section.

Running a jar produce from gradle build

I have a simple gradle file from which I build a jar file.
When I run the jar file however I get and error: 'Exception in thread "main" java.lang.NoClassDefFoundError: scala/Predef$'
The build.gradle looks like this:
apply plugin: 'scala'
apply plugin: 'distribution'
def mainClass = "com.domain.Hello"
distributions {
custom {}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.scala-lang', name: 'scala-library', version: '2.11.4'
}
jar {
manifest {
attributes 'Main-Class': mainClass
}
}
task run(type: JavaExec, description: "Runs the project") {
main = mainClass
classpath sourceSets.main.runtimeClasspath
classpath configurations.runtime
}
When you run the jar you need to include the scala-library on the classpath, like this
scala -cp [scala library jar] [project jar]
If you don't want to have to do this, take a look at this blog post http://blogs.steeplesoft.com/posts/2013/09/04/building-fat-jars-with-gradle/
You need to include ''scala-compile' package beside 'scala-library'