I am trying to include compiled classes (from the project3) to run junit tests.
Here is a brief project structure.
project1
build.gradle
project2
build.gradle
project3
src/main/java/***.java
src/test/java/***JunitTestClasses
build.gradle - I am running this.
Here is the build.gradle from project3.
apply plugin: 'java'
apply plugin: "jacoco"
apply plugin: 'maven'
apply plugin: 'com.github.jacobono.jaxb'
apply plugin: "io.spring.dependency-management"
sourceCompatibility = 1.8
targetCompatibility = 1.8
buildscript {
repositories {
maven
{
// omitted.
}
}
dependencies
{
classpath 'com.github.jacobono:gradle-jaxb-plugin:' + GRADLE_JAXB_PLUGIN_VERSION
classpath 'io.spring.gradle:dependency-management-plugin:' + GRADLE_DEP_MGMT_PLUGIN_VERSION
}
}
dependencies
{
compile project(':project1')
compile project(':project2')
compile project(':project3')
compile group: 'org.apache.ant', name: 'ant', version: ANT_VERSION
jaxb group: 'com.sun.xml.bind', name: 'jaxb-xjc', version: JAXB_VERSION
jaxb group: 'com.sun.xml.bind', name: 'jaxb-impl', version: JAXB_VERSION
jaxb group: 'com.sun.xml.bind', name: 'jaxb-core', version: JAXB_VERSION
xmlbeans 'org.apache.xmlbeans:xmlbeans:2.5.0'
testCompile group: 'junit', name: 'junit', version: '4.+'
runtime files('build/classes/main')
}
configurations {
repobootstrap
xmlbeans
}
sourceSets {
generated {
java {
srcDir file("${buildDir}/generated")
}
}
testGenerated {
compileClasspath += sourceSets.main.runtimeClasspath
java {
srcDir file("${buildDir}/generated")
}
}
main {
compileClasspath += sourceSets.generated.output
runtimeClasspath += sourceSets.generated.output
}
test {
compileClasspath += sourceSets.generated.output
runtimeClasspath += sourceSets.generated.output
}
}
test
{
systemProperties 'basedir': projectDir
systemProperties 'testdir': "${projectDir}/src/test"
systemProperties 'resourcedir': "${projectDir}/src/test/resources"
systemProperties 'TARGET': "${buildDir}/tmp"
systemProperties 'tmp.dir': "${buildDir}/tmp"
}
Some tasks which generate classes from XSD are omitted..
When I do 'gradle build', it successfully generates classes from XML and compiles. But when junit tests run, it throws 'java.lang.NoClassDefFoundError' for classes that belong to project3 and 'java.lang.ClassNotFoundException: schemaorg_apache_xmlbeans.system.sE2A73A83DA959459556520519D4CA82A.TypeSystemHolder'. Generated artifacts for xmlbeans are located under generated\schemaorg_apache_xmlbeans3 and compiled classes are located under project3/build/main
I'd appreciated if somebody has any clue.
Related
So I have a folder .ebextensions at the root of my spring boot project,and those files are not being included in my jar when I use "bootpackage" in my gradle plugin for intellij.I am deploying the jar On Amazon Web Services
How do I include these files in my jar?
My build.gradle
buildscript {
ext {
springBootVersion = '1.5.9.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'haughton.daniel'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
ext {
springCloudVersion = 'Edgware.SR1'
}
dependencies {
//compile 'org.springframework.cloud:spring-cloud-starter-aws'
compile('org.springframework.boot:spring-boot-starter-data-jpa')
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail
compile group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '2.0.0.RELEASE'
compile('org.springframework.boot:spring-boot-starter-jdbc')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
// https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4
compile group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity4', version: '2.1.2.RELEASE'
// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-aws-autoconfigure
//compile group: 'org.springframework.cloud', name: 'spring-cloud-aws-autoconfigure', version: '1.2.2.RELEASE'
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'
compile('org.springframework.boot:spring-boot-starter-web')
compile ('org.apache.tomcat:tomcat-dbcp:8.0.30')
runtime('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
Solution:
Added this to my build.gradle
processResources {
from ('.ebextensions/') {
into '.ebextensions'
}
}
I added an ebextensions folder into source of my project, this contains the .ebextensions folder with nginx config:
Folder structure
I extended bootJar task:
bootJar {
baseName = 'project'
version = '1.0.0'
from('ebextensions')
//...
When I run ./gradlew cle ass bootJar, then the unzipped project-1.0.0.jar contains:
.ebextensions
BOOT-INF
META-INF
org
I used Gradle 4.10.
The root cause was 413 Request Entity Too Large with ElasticBeanstalk.
Full content of proxy.conf:
client_max_body_size 20M;
I have a Spring Boot application backed with Kotlin as the language, and Gradle as the build system. So basically I'm trying to build a fat jar out of the application source and dependencies, which can be run using Java command line tool.
Gradle build script:
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: "kotlin-spring"
apply plugin: 'org.springframework.boot'
group 'myapp'
version '1.0'
buildscript {
ext.kotlin_version = '1.2.21' // Required for Kotlin integration
ext.spring_boot_version = '1.5.4.RELEASE'
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // Required for Kotlin integration
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin
classpath "org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version"
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin/'
test.java.srcDirs += 'src/test/kotlin/'
}
jar {
zip64 true
manifest {
attributes 'Main-Class': 'app.main.ApplicationKt'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
compile("org.jetbrains.kotlin:kotlin-reflect")
compile group: 'com.typesafe', name: 'config', version: '1.3.2'
compile 'org.springframework.boot:spring-boot-starter-web'
compile group: 'org.apache.camel', name: 'camel-spring-boot-starter', version: '2.20.2'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-redis', version: '2.0.0.RELEASE'
compile group: 'org.apache.camel', name: 'camel-quartz2', version: '2.20.2'
compile group: 'org.apache.camel', name: 'camel-http4', version: '2.20.2'
compile group: 'org.apache.camel', name: 'camel-docker', version: '2.20.2'
compile group: 'org.apache.camel', name: 'camel-aws', version: '2.20.2'
compile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.4.0'
compile group: 'redis.clients', name: 'jedis', version: '2.9.0'
compile group: 'joda-time', name: 'joda-time', version: '2.9.9'
compile group: 'net.gpedro.integrations.slack', name: 'slack-webhook', version: '1.4.0'
compile group: 'org.json', name: 'json', version: '20180130'
compile group: 'org.jfree', name: 'jfreechart', version: '1.5.0'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
If I run the project using gradle command then it runs fine. But when I build the jar and try ti run it complains with the below error:-
Error: Could not find or load main class app.main.ApplicationKt
Application.kt:-
#SpringBootApplication
#ImportResource("classpath*:camel-context.xml")
#ComponentScan("app")
class Application
fun main(args:Array<String>){
SpringApplication.run(Application::class.java, *args)
}
Not sure where exactly I'm doing anything wrong.
In Application.kt, you do not have a package declaration. From the documentation:
All the contents (such as classes and functions) of the source file are contained by the package declared. So, in the example above, the full name of baz() is foo.bar.baz, and the full name of Goo is foo.bar.Goo.
If the package is not specified, the contents of such a file belong to "default" package that has no name.
You can look into the build/classes directory to inspect what packages the classes were compiled into.
To fix this issue, add package app.main to the top of the Application.kt file.
for some reason I encountered the same error but my package was declared. I copied the package deleted the line and pasted, saved, reloaded the project and the application ran. I donĀ“t have a logical explanation for this, but that is how I solved this error.
I have a spring boot micro-services project built with Gradle.
I want to execute my cucumber integration tests on Jenkins or CI server just after code check-in. The build job on CI server gets triggered automatically after every code check-in. This job calls my gradle build.
I am able to execute the cucumber test cases as normal JUnit test case from my STS or eclipse and the embedded tomcat server gets started and after the cucumber test cases get executed on it, the server is stopped.
This is exactly what I want through gradle build:
How the cucumber integration tests get executed on a running spring boot embedded server by gradle.build ??
Current behavior is: when the gradle.build is called on CI server then the cucumber integration tests are just getting called with no target spring boot server.
Expected behavior: After gradle.build is called on CI server, the cucumber tests should get executed on the running spring boot embedded server and should get stop by itself after the cucumber test cases are executed.
NOTE: I have created a special cucumber-test profile for it in the project and have its own configuration file
My gradle.build looks like:
buildscript {
ext {
springBootVersion = '1.4.2.RELEASE'
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
jar {
manifest {
attributes 'Main-Class': 'com.pa.omas.Main'
}
baseName = 'omas'
version = ''
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url "http://smartbearsoftware.com/repository/maven2" }
}
sourceSets {
integrationTest {
java {
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
srcDir file('src/integration-test/java')
}
resources.srcDir file('src/integration-test/resources')
}
}
configurations {
integrationTestCompile.extendsFrom testCompile
integrationTestRuntime.extendsFrom testRuntime
cucumberRuntime {
extendsFrom testRuntime
}
}
task copyScripts(type: Copy) {
from("scripts")
into("build/libs")
}
task copyReports(type: Copy) {
from("reports")
into("build/libs/reports")
}
task integrationTest(type: Test) {
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
outputs.upToDateWhen { false }
}
check.dependsOn integrationTest
integrationTest.mustRunAfter test
tasks.withType(Test) {
reports.html.destination = file("${reporting.baseDir}/${name}")
}
bootRun {
args = ["--spring.profiles.active=cucumber-test"]
}
task cucumber(){
dependsOn assemble, compileTestJava
doLast {
javaexec {
main = "cucumber.api.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['-p', 'pretty', '--monochrome', '-p', 'html:reports/cucumber/cucumber-html-reports', '-p', 'junit:reports/cucumber-junit/cucumber-junit-report.xml',
'-p', 'html:reports/cucumber', '--glue', 'src/integration-test/java/com/pa/omas/cucumber', 'src/integration-test/resources']
}
copyReports
}
}
build {
dependsOn copyScripts
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.cloud:spring-cloud-starter-stream-kafka')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.projectlombok:lombok')
compile('org.springframework.boot:spring-boot-starter-web')
compile('ch.qos.logback:logback-classic')
compile('org.mariadb.jdbc:mariadb-java-client:1.5.4')
compile('org.hibernate:hibernate-java8')
compile 'com.puppycrawl.tools:checkstyle:8.3'
compile group: 'net.masterthought', name: 'cucumber-reporting', version: '3.11.0'
compile group: 'net.masterthought', name: 'maven-cucumber-reporting', version: '3.11.0'
compile group: 'com.zaxxer', name: 'HikariCP', version: '2.6.3'
compile('com.fasterxml.jackson.datatype:jackson-datatype-jsr310')
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.7.0'
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.7.0'
testCompile('com.h2database:h2')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.mockito', name: 'mockito-all', version: '2.0.2-beta'
testCompile group: 'info.cukes', name: 'cucumber-junit', version: '1.2.5'
testCompile group: 'info.cukes', name: 'cucumber-spring', version: '1.2.5'
integrationTestCompile('com.h2database:h2')
integrationTestCompile('org.springframework.boot:spring-boot-starter-test')
integrationTestCompile 'info.cukes:cucumber-java:1.2.5'
integrationTestCompile 'junit:junit:4.12'
integrationTestCompile group: 'info.cukes', name: 'cucumber-junit', version: '1.2.5'
integrationTestCompile group: 'info.cukes', name: 'cucumber-spring', version: '1.2.5'
integrationTestCompile group: 'info.cukes', name: 'cucumber-java', version: '1.2.5'
integrationTestCompile group: 'info.cukes', name: 'cucumber-core', version: '1.2.5'
integrationTestCompile group: 'info.cukes', name: 'cucumber-html', version: '0.2.6'
integrationTestCompile group: 'info.cukes', name: 'cucumber-jvm-deps', version: '1.0.5'
integrationTestCompile group: 'info.cukes', name: 'gherkin', version: '2.12.2'
integrationTestCompile group: 'io.cucumber', name: 'gherkin', version: '5.0.0'
integrationTestCompile group: 'info.cukes', name: 'cucumber-java8', version: '1.2.5'
integrationTestCompile group: 'org.webjars.npm', name: 'gherkin', version: '4.1.3'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Camden.SR3"
}
}
My project structure looks like:
StepsDefinitionAnnotations
CucumberTest
Thanks very much !!
So finally, I found the solution to this issue.
The cucumber cli main class was unable to find the testRuntime in which the stepsDefinition file was located. Therefore, it was simply calling the cucumber test cases but not executing them.
All I did is included the sourceSets for main & test in the gradle file, linke below:
sourceSets {
main {
java {
srcDirs = ["src/main/java"]
}
}
test {
java {
srcDirs = ["src/test/"]
}
}
}
And it worked fine for me.
Thanks Anyways. Hope this will help others as well !!
After bumping up kotlin-gradle-plugin from 1.1.4-3 to 1.1.50 (or 51), as well as all Kotlin related libraries I got error like below when trying to import gradle files:
Unable to build Kotlin project configuration
Details: java.lang.reflect.InvocationTargetException: null
Caused by: java.lang.NoSuchMethodError: kotlin.reflect.jvm.internal.KClassImpl.getData()Lkotlin/reflect/jvm/internal/ReflectProperties$LazyVal;
Compilation works fine when using older version of plugin (1.1.4-3).
Full gradle file:
apply plugin: 'war'
apply plugin: 'com.google.cloud.tools.appengine'
apply plugin: 'kotlin'
apply plugin: 'kotlin-kapt'
apply plugin: "net.ltgt.apt"
apply plugin: 'idea'
apply plugin: 'org.jetbrains.dokka'
def daggerVersion = "2.11"
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
buildscript {
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "org.jetbrains.dokka:dokka-gradle-plugin:0.9.9"
classpath 'com.google.cloud.tools:appengine-gradle-plugin:1.3.2'
classpath "net.ltgt.gradle:gradle-apt-plugin:0.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
dependencies {
compile "com.google.appengine:appengine-api-1.0-sdk:${appengineVersion}"
compile "com.google.appengine:appengine-endpoints-deps:${appengineVersion}"
compile "com.google.dagger:dagger:${daggerVersion}"
compile 'com.google.endpoints:endpoints-framework:2.0.5'
compile project(':backend')
kapt "com.google.dagger:dagger-compiler:${daggerVersion}"
apt "com.google.dagger:dagger-compiler:${daggerVersion}"
testCompile 'junit:junit:4.12'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
compile "com.google.code.gson:gson:2.8.2"
compile 'org.thymeleaf:thymeleaf:3.0.7.RELEASE'
compile 'com.warrenstrange:googleauth:1.1.2'
compile 'org.mindrot:jbcrypt:0.4'
testCompile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
testCompile "org.mockito:mockito-core:2.+"
testCompile "com.google.appengine:appengine-api-labs:${appengineVersion}"
testCompile "com.google.appengine:appengine-api-stubs:${appengineVersion}"
testCompile "com.google.appengine:appengine-tools-sdk:${appengineVersion}"
testCompile "com.google.appengine:appengine-testing:${appengineVersion}"
}
appengine {
stage {
enableJarClasses = true
}
}
repositories {
mavenCentral()
}
compileKotlin{
kotlinOptions{
jvmTarget = 1.8
}
}
dokka {
outputFormat = 'html'
outputDirectory = file("${buildDir}/javadoc")
}
sourceSets {
main.java.srcDirs += 'src/main/java'
main.java.srcDirs += 'build/generated/source/kapt/main'
main.kotlin.srcDirs += 'src/main/kotlin'
}
idea {
module {
sourceDirs += file("./build/generated/source/kapt/main")
excludeDirs -= file("$buildDir")
excludeDirs += file("$buildDir/dependency-cache")
excludeDirs += file("$buildDir/libs")
excludeDirs += file("$buildDir/tmp")
}
}
EDIT - SOLUTION
Just to elaborate Jack's answer:
I had to locate gradle-wrapper.properties and change the distribution url to:
distributionUrl=https\://services.gradle.org/distributions/gradle-4.2.1-all.zip
I came across the same issue.
Use gradle 3.4(+) will solve it for me.
I have the following gradle projects:
jar-module-A
+-- JavaEE lib(Dependency)
war-module-A
+-- jar-module-A
I want to exclude JavaEE lib from WEB-INF/lib.
Using providedCompile:
Since jar-module-A is not a web module but jar, I cannot use providedCompile in build.gradle of jar-module-A.
configurations { runtime.exclude JavaEE-lib }
It excludes JavaEE from not only runtime but also testRuntime, It fails my unit tests in war-module-A by ClassNotFoundException.
How can mitigate this situation?
Brute force solution would be:
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'maven'
repositories {
mavenCentral()
maven {
url "file:///tmp/repo"
}
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file:///tmp/repo")
}
}
}
dependencies {
compile group: 'ruimo', name: 'module_a', version: '1.0-SNAPSHOT'
testCompile group: 'junit', name: 'junit', version: '4.10'
}
war {
classpath = classpath.filter {
it.name != 'javaee-api-6.0.jar'
}
}
for module_b. It might be filename dependent (not sure of that). Maybe will have a look later on, but not sure - short with time.
UPDATE
More sophisticated:
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'maven'
repositories {
mavenCentral()
maven {
url "file:///tmp/repo"
}
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file:///tmp/repo")
}
}
}
dependencies {
compile(group: 'ruimo', name: 'module_a', version: '1.0-SNAPSHOT') {
transitive = false// here You can exclude particular dependency, not necessarily all
}
providedCompile group: 'javax', name: 'javaee-api', version: '6.0'
testCompile group: 'junit', name: 'junit', version: '4.10'
}
No I see it can be done with many ways. It depends on what are other requirements regard build.
If the transitive is not working, can try resolution startegy.
I was fed up with the transitive or even normal exclude.
https://docs.gradle.org/2.2.1/dsl/org.gradle.api.artifacts.ResolutionStrategy.html
dependencies{
compile(group: 'ruimo', name: 'module_a', version: '1.0-SNAPSHOT')
}
configurations.all {
resolutionStrategy {
force ruimo:module_a:1.0-SNAPSHOT
}
}