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.
Related
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/')
}
}
}
I have java and kotlin based multi module gradle project. I am trying to setup sonar analysis for the same. I configured sonar at root project and ran analysis with CircleCI. The result in sonarcloud gets for only one of the sub-project.
My project structure is as below:
projectA/build.gradle
ProjectB/build.gradle
ProjectC/build.gradle
build.gradle
Here is my root build.gradle.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61'
classpath 'io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE'
classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.2.4.RELEASE'
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.7"
}
}
ext {
springCloudVersion = 'Hoxton.SR1'
springBootVersion = '2.2.4.RELEASE'
}
configurations.all {
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
allprojects {
group = 'com.organiz'
version = '1.0.0-SNAPSHOT'
repositories {
mavenCentral()
maven {
// url ="s3url" // only for releases
url ="s3url" // only for snapshots
credentials(AwsCredentials) {
accessKey project.accessKey
secretKey project.secretKey
}
}
}
}
subprojects {
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'org.springframework.boot'
apply plugin: 'idea'
apply plugin: 'org.sonarqube'
sourceCompatibility = '1.8'
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
implementation('someappcommon:1.0.0-SNAPSHOT') { changing = true }
implementation("com.h2database:h2:1.4.200")
}
repositories {
mavenCentral()
jcenter()
maven {
url "someurlhere"
}
someappMavenRepoUrl.split(',').each { repoUrl -> maven { url repoUrl } }
}
test {
useJUnitPlatform()
}
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
sonarqube {
properties {
property "sonar.projectKey", "projectKey"
property "sonar.organization", "org"
property "sonar.host.url", "https://sonarcloud.io"
property "sonar.verbose", "true"
}
}
}
project(':project1') {
dependencies {
implementation project(":common")
}
}
project(':project2') {
dependencies {
implementation project(":common")
}
}
Need to include sonarqube outside the subproject block and then all the sub modules will be analysed and report will be exported.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61'
classpath 'io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE'
classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.2.4.RELEASE'
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.7"
}
}
ext {
springCloudVersion = 'Hoxton.SR1'
springBootVersion = '2.2.4.RELEASE'
}
configurations.all {
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
allprojects {
group = 'com.organiz'
version = '1.0.0-SNAPSHOT'
repositories {
mavenCentral()
maven {
// url ="s3url" // only for releases
url = "s3url" // only for snapshots
credentials(AwsCredentials) {
accessKey project.accessKey
secretKey project.secretKey
}
}
}
}
apply plugin: 'org.sonarqube'
sonarqube {
properties {
property "sonar.projectKey", "projectKey"
property "sonar.organization", "org"
property "sonar.host.url", "https://sonarcloud.io"
property "sonar.verbose", "true"
}
}
subprojects {
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'org.springframework.boot'
apply plugin: 'idea'
sourceCompatibility = '1.8'
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
implementation('someappcommon:1.0.0-SNAPSHOT') { changing = true }
implementation("com.h2database:h2:1.4.200")
}
repositories {
mavenCentral()
jcenter()
maven {
url "someurlhere"
}
someappMavenRepoUrl.split(',').each { repoUrl -> maven { url repoUrl } }
}
test {
useJUnitPlatform()
}
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
}
project(':project1') {
dependencies {
implementation project(":common")
}
}
project(':project2') {
dependencies {
implementation project(":common")
}
}
My solution for kotlin multi-module project with junit 5 & spring boot, using gradle-jacoco-log plugin:
1.☝🏻 In your root build.gradle.kts:
plugins {
kotlin("jvm") version "1.5.0"
id("org.sonarqube") version "3.2.0"
id ("org.barfuin.gradle.jacocolog") version "1.2.4"
}
sonarqube {
val sonarOrganization: String by project
val sonarProjectKey: String by project
val sonarLogin: String by project
properties {
properties(
hashMapOf<String, String>(
"sonar.login" to sonarLogin,
"sonar.projectKey" to sonarProjectKey,
"sonar.organization" to sonarOrganization,
"sonar.host.url" to "https://sonarcloud.io",
"sonar.coverage.jacoco.xmlReportPaths" to "${buildDir}/reports/jacoco/jacocoAggregatedReport/jacocoAggregatedReport.xml"
)
)
}
}
2.👉🏻 In backend suprojects which have tests:
plugins{
jacoco
}
jacoco { toolVersion = "0.8.7" }
tasks{
test { useJUnitPlatform()
jacocoTestReport { reports { xml.required.set( true ) } }
finalizedBy(jacocoTestReport)
}
}
Optional step (example how to use)
3.👌🏻 In your CI file (.github/workflows/master.yml in my case):
name: Master CI
on:
push:
branches:
- master
jobs:
DEPLOY:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: 🪜 Set up JDK 16
uses: actions/setup-java#v1
with:
java-version: 16
- name: 🔦 Test
run: |
chmod +x ./gradlew
./gradlew test
# Create `build/jacoco/jacocoMergeSubprojects.exec`
- name: ⛈ 1) Jacoco merge subprojects reports
run: ./gradlew jacocoMergeSubprojects -x test
# Create `build/reports/jacoco/jacocoAggregatedReport/jacocoAggregatedReport.xml`
- name: ⛈ 2) Jacoco Jacoco aggregated report
run: ./gradlew jacocoAggregatedReport -x test
- name: ⛈ 3) Sonarqube report
run: ./gradlew sonarqube -x test
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
I'm am trying to publish an in-house libGDX framework to use for all the games I work on using artifactory. I have been following this guide, however, I am stuck at the point in which the project itself is actually published. My libGDX project only has a core module, as it is not a game in itself, so I did not make a desktop or android module. This is my buildscript for the whole project
buildscript {
repositories {
mavenLocal()
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
jcenter()
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:latest.release"
}
}
allprojects {
apply plugin: "eclipse"
apply plugin: "idea"
version = '1.0'
ext {
appName = "lichengine"
gdxVersion = '1.9.8'
roboVMVersion = '2.3.3'
box2DLightsVersion = '1.4'
ashleyVersion = '1.7.0'
aiVersion = '1.8.0'
}
repositories {
mavenLocal()
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
}
project(":core") {
apply plugin: "java"
dependencies {
compile group: 'org.json', name: 'json', version: '20160810'
compile "com.badlogicgames.gdx:gdx:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-controllers:$gdxVersion"
}
}
tasks.eclipse.doLast {
delete ".project"
}
And here is the buildscript for the core module
apply plugin: "java"
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
sourceCompatibility = 1.7
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceSets.main.java.srcDirs = [ "src/" ]
eclipse.project {
name = appName + "-core"
}
def libraryGroupId = 'my.com.lichengine'
def libraryArtifactId = 'lichengine'
def libraryVersion = '1.0'
publishing {
publications {
aar(MavenPublication) {
groupId libraryGroupId
version libraryVersion
artifactId libraryArtifactId
artifact("$buildDir/publications/aar/${artifactId}-release.aar")
}
}
}
artifactory {
contextUrl = 'http://mywebsite.com:8081/artifactory'
publish {
repository {
repoKey = 'libs-release-local'
username = artifactory_username
password = artifactory_password
}
defaults {
publications('aar')
publishArtifacts = true
properties = ['qa.level': 'basic', 'q.os': 'android', 'dev.team': 'core']
publishPom = true
}
}
}
When I try to run the artifactoryPublish task on the root project or the core project I get the following error:
lichengine\core\build\publications\aar\lichengine-release.aar' does not exist, and need to be published from publication aar
Am I missing something? Everything I search for online is specifically for android libraries so nothing really helps.It seems like an aar file that is supposed to be generated is not.
After a lot of pain, I figured it out. Instead of using an aar publication I am using mavenJava. Here is my updated publications block:
publications {
mavenJava(MavenPublication) {
groupId libraryGroupId
version libraryVersion
artifactId libraryArtifactId
from components.java
}
}
I also had to update the defaults block
defaults {
publications ('mavenJava')
publishArtifacts = true
properties = ['qa.level': 'basic', 'q.os': 'android', 'dev.team': 'core']
publishPom = false
}
Hopefully, anyone who sees this can use this to help them make libGDX libraries with artifactory!
While writing my first app using JavaFX, I ensured that JAVA_HOME is setup properly, and included the javafx-gradle-plugin into my app, but got an error of missing ant-javafx-library which actually available in my JDK :(
my build.gradle and the error msg I got are below:
// set up the kotlin-gradle plugin
buildscript {
ext.kotlin_version = '1.1.60'
repositories {
mavenLocal() // mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "de.dynamicfiles.projects.gradle.plugins:javafx-gradle-plugin:8.8.2"
}
}
// apply the kotlin-gradle plugin
apply plugin: "kotlin"
apply plugin: 'javafx-gradle-plugin'
// add kotlin-stdlib dependencies.
repositories {
mavenLocal() // mavenCentral()
}
dependencies {
//dependencies from a remote repositor
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "no.tornado:tornadofx:1.7.12"
compile "de.dynamicfiles.projects.gradle.plugins:javafx-gradle-plugin:8.8.2"
}
jar {
manifest {
//Define mainClassName as: '[your_namespace].[your_arctifact]Kt'
attributes ('Main-Class': 'MyAppKt', "Implementation-Title": "Gradle",
"Implementation-Version": 1)
}
// NEW LINE HERE !!!
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
sourceSets {
main.kotlin.srcDirs += 'src/kotlin'
main.resources.srcDirs += 'src/resources'
}
kotlin {
experimental.coroutines 'enable'
}
compileKotlin {
kotlinOptions.jvmTarget= 1.8 // optional, Minimum jvmTarget of 1.8 needed since Kotlin 1.1
kotlinOptions.suppressWarnings = true
}
I was able to solve it, by downloading the javafx.plugin from here.
Then created a 'plugin' folder, and copied that file in it.
Last, I installed this plugging into my gradle.build as:
apply from: "plugins/javafx.plugin"
So, my last gradle.build is:
// set up the kotlin-gradle plugin
buildscript {
ext.kotlin_version = '1.1.60'
repositories {
mavenLocal() // mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// classpath files('plugins/javafx.plugin')
}
}
// apply the kotlin-gradle plugin
apply plugin: "kotlin"
apply from: "plugins/javafx.plugin" // apply from: "http://dl.bintray.com/content/shemnon/javafx-gradle/8.1.1/javafx.plugin"
// add kotlin-stdlib dependencies.
repositories {
mavenLocal() // mavenCentral()
}
dependencies {
//dependencies from a remote repositor
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "no.tornado:tornadofx:1.7.12"
}
jar {
manifest {
//Define mainClassName as: '[your_namespace].[your_arctifact]Kt'
attributes ('Main-Class': 'MainKt', "Implementation-Title": "Gradle",
"Implementation-Version": 1)
}
// NEW LINE HERE !!!
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
sourceSets {
main.kotlin.srcDirs += 'src/kotlin'
main.resources.srcDirs += 'src/resources'
}
kotlin {
experimental.coroutines 'enable'
}
compileKotlin {
kotlinOptions.jvmTarget= 1.8 // optional, Minimum jvmTarget of 1.8 needed since Kotlin 1.1
kotlinOptions.suppressWarnings = true
}
and my app structure, is:
UPDATE
OPTION 2
Install the new plugin here by:
buildscript {
repositories {
mavenCentral() // or mavenLocal()
}
dependencies {
compile "de.dynamicfiles.projects.gradle.plugins:javafx-gradle-plugin:8.8.2"
}
}
If interested in using mavenLocal() then is can be downloaded using the below command:
mvn dependency:get -DrepoUrl=https://mvnrepository.com/artifact/de.dynamicfiles.projects.gradle.plugins/javafx-gradle-plugin/8.8.2 -Dartifact=de.dynamicfiles.projects.gradle.plugins:javafx-gradle-plugin:8.8.2
To get the mvn command line, you get download it from here, add it to your path then call it from anywhere.
I'm having difficulty generating a Grails 3.2.7 plugin with its dependencies.
This works and creates a JAR with all dependencies:
gradle clean build
..but not this; the JAR has no dependencies, just plugin project files:
gradle publishToMavenLocal
My gradle file, I know there's something missing, and based on the docs it's my understand there's no native feature to do this.
buildscript {
ext {
grailsVersion = project.grailsVersion
}
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
jcenter()
}
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.0.1"
}
}
plugins {
id "io.spring.dependency-management" version "0.5.2.RELEASE"
id "com.jfrog.bintray" version "1.2"
}
version "3.1-SNAPSHOT"
group "mygroup"
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: "org.springframework.boot"
apply plugin: "com.jfrog.artifactory"
apply plugin: "org.grails.grails-plugin"
apply plugin: "org.grails.grails-gsp"
apply plugin: 'maven-publish'
ext {
grailsVersion = project.grailsVersion
gradleWrapperVersion = project.gradleWrapperVersion
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
idea {
module {
downloadSources = true
}
}
repositories {
mavenLocal()
mavenCentral()
maven { url "https://repo.grails.org/grails/core" }
}
dependencyManagement {
imports {
mavenBom "org.grails:grails-bom:$grailsVersion"
}
applyMavenExclusions false
}
dependencies {
provided 'org.springframework.boot:spring-boot-starter-logging'
provided "org.springframework.boot:spring-boot-starter-actuator"
provided "org.springframework.boot:spring-boot-autoconfigure"
provided "org.springframework.boot:spring-boot-starter-tomcat"
provided "org.grails:grails-web-boot"
provided "org.grails:grails-dependencies"
provided 'javax.servlet:javax.servlet-api:3.1.0'
compile "org.grails:grails-plugin-testing"
testCompile "com.fiftyonred:mock-jedis:0.4.0"
console "org.grails:grails-console"
compile "redis.clients:jedis:2.5.2"
}
task wrapper(type: Wrapper) {
gradleVersion = gradleWrapperVersion
}
artifactory {
contextUrl = 'http://myrepo/'
}
task sourceJar(type: Jar) {
from sourceSets.main.allJava
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
The issue stems from the publishing section:
When I change this to:
task jarWithPlainPom(type: Jar) {
from sourceSets.main.output
from configurations.runtime
}
publishing {
publications {
mavenJava(MavenPublication) {
artifact jarWithPlainPom
}
}
}
It includes most of the needed dependencies, however the POM is not updated and the project fails when importing the plugin.