I. Directory structure
Second, build.gradle configuration
Group ' Yjmyzz '
Version ' 1.0-snapshot '
Apply plugin: ' java '
Apply plugin: ' Scala '
Apply plugin: ' Application '/optional (can automatically generate shell startup scripts)
Mainclassname = ' Appdemo '//optional (with upstream application plugin)
Here's the key (mapping Java and Scala's source code directory all to Scala,
So Gradle Compilescala can compile both Java and Scala source code at the same time.
sourcesets {
Main {
Scala {
Srcdirs = [' Src/main/scala ', ' Src/main/java ']
}
java {
Srcdirs = []
}
}
Test {
Scala {
Srcdirs = [' Src/test/scala ', ' Src/test/java ']
}
java {
Srcdirs = []
}
}
}
Optional (Gradle Cdirs can be used to generate the SRC directory for Scala and Java at Project initialization)
Task "Createdirs" << {
Sourcesets*.scala.srcdirs*.each {it.mkdirs ()}
Sourcesets*.java.srcdirs*.each {it.mkdirs ()}
Sourcesets*.resources.srcdirs*.each {it.mkdirs ()}
}
repositories {
Mavenlocal ()
Maven {URL ' http://maven.aliyun.com/nexus/content/repositories/central/'}
Mavencentral ()
}
dependencies {
Compile "org.scala-lang:scala-library:2.11.7"
Compile "org.scala-lang:scala-compiler:2.11.7"
Compile "org.scala-lang:scala-reflect:2.11.7"
Testcompile "junit:junit:4.11"
}
Optionally, if you want the generated jar to run directly, it is recommended to add
Jar {
Manifest {
Attributes ' Main-class ': ' Appdemo '
}
}
This will do the following:
Create initial directory: Gradle cdirs (Note: This is abbreviated, exactly the same as Gradle Createdirs)
Compilation: Gradle Compilescala
Build jar Package: Gradle jar
To build a running package with a startup script: Gradle installdist
How does the Gradle project support mixed use of Java and Scala?