Assume that you have the following directory structure:
- your-app
- project1
- src
- main
- scala
App.scala
- resources (optional)
- scala
- test
- main
- src
- project2
- src
- main
- scala
App.scala
- resources (optional)
- scala
- test
- main
- src
settings.gradle
build.gradle
- project1
Enable Gradle wrapper (to allow users without gradle to use this project):
gradle wrapper
Edit settings.gradle
:
/*
* The settings file is used to specify which projects to include in your build.
*
* Detailed information about configuring a multi-project build in Gradle can be found
* in the user guide at https://docs.gradle.org/4.8.1/userguide/multi_project_builds.html
*/
rootProject.name = 'your-app-name'
include 'project1', 'project2'
Edit build.gradle
:
def scalaVersion ='2.12'
// -------- DEPENDENCIES --------
// Sample dependencies
// def playJsonDep = "com.typesafe.play:play-json_$scalaVersion:2.6.9".toString()
// def jodaDep = 'org.joda:joda-convert:2.1'
// ------------------------------
// for scalaTest
buildscript {
repositories {
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "gradle.plugin.com.github.maiflai:gradle-scalatest:0.22"
}
}
// define common properties/behaviour
// (assume all sub projects are scala)
subprojects {
apply plugin: 'scala'
apply plugin: 'application'
apply plugin: "com.github.maiflai.scalatest" // for scalaTest
group 'your-app-name'
version '0.1'
repositories {
jcenter()
mavenCentral()
// maven { url "https://dl.bintray.com/akka/snapshots/" }
}
dependencies {
compile "org.scala-lang:scala-library:$scalaVersion".toString()
// compile 'log4j:log4j:1.2.17'
testCompile "org.scalatest:scalatest_$scalaVersion:3.0.5".toString()
testRuntime 'org.pegdown:pegdown:1.4.2' // for scalaTest
}
// copy dependencies to build/dependencies
task copyDeps(type: Copy) {
from configurations.runtime
into 'build/dependencies'
}
// run copyDeps before jar...
jar.dependsOn copyDeps
}
// project specific
project(':project1') {
mainClassName = "project1.App"
dependencies {
// compile akkaStreamKafkaDep
}
jar {
manifest {
attributes 'Main-Class': 'project1.App'
}
}
}
project(':project2') {
mainClassName = "project2.App"
dependencies {
// compile akkaStreamKafkaDep
// compile akkaStreamHBaseDep
// compile playJsonDep
}
jar {
manifest {
attributes 'Main-Class': 'project2.App'
}
}
}
Compilation using gradle:
./gradlew clean test jar
This will run the following tests: clean
, test
, jar
.
This will create the following files:
./project1/build/libs/project1-0.1.jar
: main jar./project1/build/dependencies
: project1 dependencies (.jar)./project2/build/libs/project2-0.1.jar
: fake producer main jar./project12/build/dependencies
: project2 dependencies (.jar)
To run a project1/2 use:
./gradlew :project1:run
or (if the above doesn't work)
java -cp ./project1/build/lib/*:./project1/build/libs/* \
project1r.App
See also: https://docs.gradle.org/current/userguide/multi_project_builds.html
This really helped me a lot to upgrade an older Scala project to a more recent Gradle version; thanks! :)