I have a project with the following files:
src/main/scala/package/Class.scala
src/test/java/package/ClassTest.java
When I run ./gradlew clean build test I get an error in :compileTestJava - it says "error: cannot find symbol" because apparently ClassTest.java cannot find Class.scala
I have the following in build.gradle which I think should work but doesn't:
sourceSets {
test {
java { srcDirs += ['src/main/scala', 'src/test/java'] }
}
}
I also have the following plugins for what it is worth:
plugins {
id 'java'
id 'scala'
id 'java-library'
id 'maven-publish'
id 'jacoco'
}
The only other things in build.gradle are repositories and dependencies
I have simulated your scenario locally. The below works with Gradle 6.5 and 'java' and 'scala' plugins. My main srcDirs are slightly different from yours but I do not think that is material.
build.gradle:
sourceSets
{
main
{
scala {
srcDirs = ['app/glue', 'app/controllers', 'app/models']
}
}
test {
java {
srcDirs = ['test/java']
}
scala {
srcDirs = ['test/scala']
}
}
}
Sample JUnit test which refers to a scala case class Form which is defined in the 'main' source set:
public class TestScalaFromJava
{
#Test
public void testNothing() { }
#Test
public void testScalaClassInJava1() {
Firm firm = new Firm(1, "hello", "mello", "yellow");
}
}
I can then run either of the below and they work:
./gradlew compileTestJava
./gradlew clean build test
Related
I made a minimalist java gradle project to test java 9 modules.
I made only 1 module and tried testing depending on a third party module. But I keep getting the error when I try to build or run the project:
module not found: commons.validator
here are my files:
Demo.java"
public class Demo {
public static void main(String[] args) {
boolean result = EmailValidator.getInstance().isValid("abcd");
System.out.println(result);
}
}
module-info.java:
module auth.server.main {
requires commons.validator;
}
build.gradle:
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
// for email validation
implementation 'commons-validator:commons-validator:1.7'
}
/*this is to let gradle infer the locations of the modules*/
java {
modularity.inferModulePath.set(true)
}
Whats wrong and how to solve it?
This might easy way.
plugins {
id 'java'
id "org.javamodularity.moduleplugin" version "1.1.1"
}
repositories {
mavenCentral()
}
dependencies {
implementation 'commons-validator:commons-validator:1.7'
}
Check it out : https://github.com/java9-modularity/gradle-modules-plugin
I want to create a gradle java application that generates a client from an openAPI specification file and uses that client.
So I created a java application with gradle init (type:application, language:Java, DSL:groovy, test-framework:Junit Jupiter, project-name:simple-java-app, package-structure:a.aa).
Small example of what works:
I can create a new source folder second/loc/src/main/java with a package b.bb and a class Foo.
And with the following build.gradle
plugins {
id 'java'
id 'application'
}
repositories {
jcenter()
}
sourceSets {
second {
java {
srcDir 'second/loc/src/main/java'
}
}
}
compileJava {
source += sourceSets.second.java
}
dependencies {
implementation 'com.google.guava:guava:29.0-jre'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
}
application {
mainClassName = 'a.aa.App'
}
test {
useJUnitPlatform()
}
The main class can access Foo:
package a.aa;
import b.bb.Foo;
public class App {
public static void main(String[] args) {
System.out.println(new Foo().sayFoo());
}
}
What doesn't work
Now I try the same for generated code by openapi-generator:
Under plugins I add id "org.openapi.generator" version "4.3.1"
And I add a new task:
openApiGenerate {
generatorName = "java"
inputSpec = "$rootDir/specs/petstore.yaml".toString()
outputDir = "$buildDir/generated".toString()
apiPackage = "org.openapi.example.api"
invokerPackage = "org.openapi.example.invoker"
modelPackage = "org.openapi.example.model"
configOptions = [
dateLibrary: "java8"
]
}
Then I execute the task openApiGenerate and confirm in the file system that the sources have been generated(eclipse won't show the build folder).
Now I use the same method as above resulting in below build.gradle
plugins {
id 'java'
id 'application'
id "org.openapi.generator" version "4.3.1"
}
repositories {
jcenter()
}
openApiGenerate {
generatorName = "java"
inputSpec = "$rootDir/specs/petstore.yaml".toString()
outputDir = "$buildDir/generated".toString()
apiPackage = "org.openapi.example.api"
invokerPackage = "org.openapi.example.invoker"
modelPackage = "org.openapi.example.model"
configOptions = [
dateLibrary: "java8"
]
}
sourceSets {
client {
java {
srcDir '$buildDir/generated/src/main/java'
}
}
}
compileJava {
source += sourceSets.client.java
}
dependencies {
implementation 'com.google.guava:guava:29.0-jre'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
}
application {
mainClassName = 'a.aa.App'
}
test {
useJUnitPlatform()
}
But when I try to use the classes now:
package a.aa;
import org.openapi.example.model.Pet;
public class App {
public static void main(String[] args) {
Pet p = new Pet(0L);
System.out.println(p.getId());
}
}
neither import nor Pet can be resolved.
> Task :compileJava FAILED
C:\...\simple-java-app\src\main\java\a\aa\App.java:6: error: package org.openapi.example.model does not exist
import org.openapi.example.model.Pet;
^
C:\...\simple-java-app\src\main\java\a\aa\App.java:14: error: cannot find symbol
Pet p = new Pet(0);
^
symbol: class Pet
location: class App
C:\...\simple-java-app\src\main\java\a\aa\App.java:14: error: cannot find symbol
Pet p = new Pet(0);
^
symbol: class Pet
location: class App
3 errors
FAILURE: Build failed with an exception.
I don't know how to debug this, frankly I'm unsure if source sets are even the right way. All openapi-generator tutorials seem to use them, I haven't tried subprojects yet, the openApiGenerate task seems to create a complete project with build.gradle and everything.
You need to add the sources from the generated code to your project. One example from one of my projects:
sourceSets.main.java.srcDir "${buildDir}/generated/src/main/java"
After generation make sure you refresh gradle and project.
With build.gradle.kts and gradle 7+ I used in Kotlin DSL:
configure<SourceSetContainer> {
named("main") {
java.srcDir("$buildDir/generated/src/main/java")
}
}
To add the java generated sources to the project's source.
I have a Spring Boot Java project that builds using Gradle (v6.2.2).
build.gradle.kts
plugins {
base
java
id("org.springframework.boot") apply false
}
val gradleVersionProperty: String by project
val javaVersion: String by project
val springBootVersion: String by project
val springCloudStarterParentBomVersion: String by project
if (JavaVersion.current() != JavaVersion.VERSION_11) {
throw GradleException("This build must be run with JDK 11")
} else {
println("Building with JDK " + JavaVersion.current())
}
tasks.withType<Wrapper> {
gradleVersion = gradleVersionProperty
distributionType = Wrapper.DistributionType.ALL
}
allprojects {
group = "com.meanwhile.in.hell"
version = "$version"
// Repos used in dependencyManagement section of settings.gradle
repositories {
mavenLocal()
mavenCentral()
maven("https://repo.spring.io/snapshot")
maven("https://repo.spring.io/milestone")
}
}
subprojects {
if (!project.name.startsWith("platform")) {
apply {
plugin("java-library")
}
java.sourceCompatibility = JavaVersion.VERSION_11
java.targetCompatibility = JavaVersion.VERSION_11
// Change the default test logging settings
tasks.withType<Test>() {
useJUnitPlatform()
testLogging {
events = setOf(
org.gradle.api.tasks.testing.logging.TestLogEvent.FAILED,
org.gradle.api.tasks.testing.logging.TestLogEvent.PASSED,
org.gradle.api.tasks.testing.logging.TestLogEvent.SKIPPED
)
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
}
enableAssertions = false
ignoreFailures = gradle.startParameter.isContinueOnFailure
maxParallelForks =
(Runtime.getRuntime().availableProcessors() / 2).takeIf { it > 0 } ?: 1
}
dependencies {
"api"(enforcedPlatform("org.springframework.boot:spring-boot-dependencies:$springBootVersion"))
"api"(enforcedPlatform("org.springframework.cloud:spring-cloud-dependencies:$springCloudStarterParentBomVersion"))
"implementation"(enforcedPlatform(project(":platform-dependencies")))
"testCompile"("org.springframework.boot:spring-boot-starter-test")
}
}
}
However, I would like to add support for Spring Boot Kotlin sub-projects within it. I have used a very simple sample project from a Kotlin-only project I have that builds fine within it. Without any changes to my root build.gradle.kts file, my current build error is:
* What went wrong:
Execution failed for task ':kotlin-sample-project:bootJar'.
> Main class name has not been configured and it could not be resolved
I have not configured the main class for any of the Java sub-projects and neither have I in my Kotlin-only other project.
My build.gradle.kts in kotlin-sample-project is very simple:
plugins {
id("org.springframework.boot")
}
dependencies {
implementation("org.springframework.cloud:spring-cloud-starter-gateway")
}
And my main class looks like:
src/main/kotlin/sample/KotlinSampleApplication.kts
package com.meanwhile.in.hell.kotlinsample
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
#SpringBootApplication
open class KotlinSampleApplication
fun main(args: Array<String>) {
runApplication<KotlinSampleApplication>(*args)
}
I have tried to add the kotlin plugin, but the build fails instantly not knowing what it is.
plugins {
base
java
kotlin
}
Error:
Line 9: kotlin
^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public val <T : Any> Class<TypeVariable(T)>.kotlin: KClass<TypeVariable(T)> defined in kotlin.jvm
I have tried to add the kotlin plugin, but the build fails instantly not knowing what it is.
It should be:
build.gradle.kts:
plugins {
kotlin("jvm").version("1.3.72")
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
Bot Kotlin JVM plugin should be applied and Kotlin stdlib be present on compile classpath.
I'm trying to build a Springboot web application that creates a WAR and also a suite of tests using the Spock framework but every time I try to run gradle build the console will execute the tests but will not create the web app WAR although it mentions a file in the web app source code saying a deprecation warning so I know it is at least doing something with it. Is there some kind of configuration I'm missing from my build.gradle or concept about how sourceSets work? I can compile the web app fine and the tests when I do them separately with their own build.gradle files but I was hoping that the code below could build the WAR and execute the tests in the same file.
plugins {
id 'java'
id 'maven-publish'
id 'war'
id 'groovy'
}
repositories {
mavenLocal()
maven {
url = 'https://repo.spring.io/libs-release'
}
maven {
url = 'http://repo.maven.apache.org/maven2'
}
}
sourceSets{
main {
java { srcDir "src\\TestingWebAppSpringBoot\\src\\main\\java" }
resources {srcDir "src\\TestingWebAppSpringBoot\\src\\main\\resources" }
groovy { srcDirs = [] }
}
test{
groovy {
srcDirs = ["src\\test_pack\\com\\example\\diag\\test\\spock\\main", "src\\test_pack\\com\\example\\diag\\test\\spock\\test" ]
}
}
}
test{
reports.html.destination = file("build\\spockTestResults")
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-thymeleaf:2.0.0.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-web:2.0.0.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-security:2.0.0.RELEASE'
compile 'org.codehaus.groovy:groovy-all:2.4.14'
providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
testCompile (
fileTree(dir: 'build\\server\\_archives\\_release', include: '*.jar'),
fileTree(dir: 'build\\common\\_archives\\_release', include: '*.jar'),
'net.sourceforge.htmlunit:htmlunit:2.26',
"org.codehaus.groovy:groovy-all:2.4.5",
"org.spockframework:spock-core:1.0-groovy-2.4"
)
}
group = 'com.something.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
publishing {
publications {
maven(MavenPublication) {
from(components.java)
}
}
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
My gradle pitest is not able to give me the right results. It looks like it is not able to locate my test files.
I have the following build.gradle file:
apply plugin: "java" apply plugin: "maven" apply plugin: "info.solidsoft.pitest"
group = "myorg" version = 1.0
repositories {
mavenCentral() }
sourceSets.all { set ->
def jarTask = task("${set.name}Jar", type: Jar) {
baseName = baseName + "-$set.name"
from set.output
}
artifacts {
archives jarTask
} }
sourceSets {
api
impl main{ java { srcDir 'src/api/java' srcDir 'src/impl/java' } } test { java { srcDir 'src/test/java' } } }
buildscript {
repositories {
mavenCentral()
//Needed only for SNAPSHOT versions
//maven { url "http://oss.sonatype.org/content/repositories/snapshots/" }
}
dependencies {
classpath 'info.solidsoft.gradle.pitest:gradle-pitest-plugin:1.1.6'
} }
dependencies {
apiCompile 'commons-codec:commons-codec:1.5'
implCompile sourceSets.api.output
implCompile 'commons-lang:commons-lang:2.6'
testCompile 'junit:junit:4.9'
testCompile sourceSets.api.output
testCompile sourceSets.impl.output
runtime configurations.apiRuntime
runtime configurations.implRuntime }
jar {
from sourceSets.api.output
from sourceSets.impl.output }
pitest { println sourceSets.main
targetClasses = ['doubler.*'] targetTests = ['doubler.*'] verbose="on" }
THe output is stored in the correct folder. And when I run gradle test, it also runs fine.
Some additional information about this issue was supplied in the pitest user group.
https://groups.google.com/forum/#!topic/pitusers/8C7BHh-Vb6Y
The tests being run look like this.
#Test
public void testIt2() {
assert new DoublerImpl().testIt(1) == 2;
}
Pitest is correctly reporting that these tests provide 0% coverage of the class. There is no coverage because the assert keyword has been used.
Unless the -ea flag is set in the JVM running the tests assertions are disabled. There is basically hidden if block around this code generated by the compiler
#Test
public void testIt2() {
if (assertionsEnabled) {
assert new DoublerImpl().testIt(1) == 2;
}
}
As assertions are not enabled no code is executed.
To fix the issue use the built in JUnit assertions instead.
http://junit.sourceforge.net/javadoc/org/junit/Assert.html