gradle test classnotfoundexception for compileOnly dependency - java

I have a war project, and I am trying to run my unit tests. Unfortunately, I am receiving this ClassNotFoundException for a class that is a 'compileOnly' dependency. I have spent hours trying to add this class to the test classpath, without success. Any help is appreciated.
The Gradle build file is below:
buildscript {
repositories {
mavenCentral()
maven {
name = 'Sonatype Nexus Snapshots'
url = 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
dependencies {
classpath 'net.wasdev.wlp.gradle.plugins:liberty-gradle-plugin:1.1-SNAPSHOT'
}
}
plugins {
id 'eclipse-wtp'
id 'war'
id 'org.unbroken-dome.test-sets' version '1.2.0'
}
apply plugin: 'liberty'
compileJava.options.fork = true
compileJava.options.forkOptions.executable = project.property('JDKPath')
// In this section you declare where to find the dependencies of your project
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
flatDir {
dirs 'WebContent/WEB-INF/lib'
}
}
configurations {
testCompile.extendsFrom compileOnly
testRuntime.extendsFrom compileOnly
}
testSets {
integrationTest
endToEndTest
}
dependencies {
// Libraries that will be provided by WebSphere.
compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
compileOnly group: 'com.ibm.websphere.appserver.api', name: 'com.ibm.websphere.appserver.api.json', version: '1.0.12'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'com.jayway.restassured', name: 'rest-assured', version: '2.9.0'
}
sourceSets {
test {
compileClasspath += [configurations.compileOnly]
runtimeClasspath += [configurations.compileOnly]
}
}

I had to dig deep into the exception stack, and when I did, I found that I was missing a dependency.

Related

Can't start web application in .WAR file : java.lang.ClassNotFoundException

I have a grails application written in Groovy. It is built and works when it's launched with :
./gradlew bootRun
Now I want to run it with java to put it in a Docker container. This is the Dockerfile I prepared :
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=build/libs/*.war
ADD ${JAR_FILE} app.war
ENTRYPOINT ["java"]
CMD ["-Dgrails.env=development","-jar","app.war"]
The server fails to start because of the following error:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [grails.boot.config.GrailsApplicationPostProcessor]: Factory method 'grailsApplicationPostProcessor' threw exception; nested exception is java.lang.ClassNotFoundException: com.flosslab.eGrocery.showcase.CmsTagLib
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
... 29 more
Caused by: java.lang.ClassNotFoundException: com.flosslab.eGrocery.showcase.CmsTagLib
When I checked the classes folder in WEB-INF, I found that the class is there in the correct path. I tried with a Tomcat setup, with Docker and without Docker and I'm still encountering the exact same error. I think something is wrong with the .WAR build. Here is the build.gradle file
build.gradle:
buildscript {
ext {
grailsVersion = project.grailsVersion
}
repositories {
mavenLocal()
maven { url "https://repo.grails.org/core" }
maven { url "https://repo.grails.org/grails/core" }
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
classpath "com.bertramlabs.plugins:asset-pipeline-gradle:2.5.0"
// classpath "org.grails.plugins:hibernate4:5.0.2"
classpath 'org.springframework:springloaded:1.2.8.RELEASE'
classpath "gradle.plugin.com.github.viswaramamoorthy:gradle-util-plugins:0.1.0-RELEASE"
}
}
version "1.0.49"
group "com.flosslab.egrocery"
apply plugin: "com.github.ManifestClasspath"
apply plugin: "eclipse"
apply plugin: "idea"
apply plugin: "war"
apply plugin: "org.grails.grails-web"
apply plugin: "org.grails.grails-gsp"
apply plugin: "asset-pipeline"
war.archiveName = "egrocery-showcase.war"
ext {
grailsVersion = project.grailsVersion
gradleWrapperVersion = project.gradleWrapperVersion
}
repositories {
mavenLocal()
maven { url "https://repo.grails.org/core" }
maven { url "https://repo.grails.org/grails/core" }
maven { url "http://192.168.12.16:8081/artifactory/egrocery-local" }
maven { url "http://192.168.12.16:8081/artifactory/libs-release-local" }
maven { url "http://192.168.12.16:8081/artifactory/plugins-release-local" }
mavenCentral()
}
dependencyManagement {
imports {
mavenBom "org.grails:grails-bom:$grailsVersion"
}
applyMavenExclusions false
}
def elasticsearchVersion = '5.6.8'
ext['elasticsearch.version'] = elasticsearchVersion
dependencies {
compile fileTree(dir: "libs", includes: ["*.jar"])
compile "com.flosslab.egrocery:domain:1.0.49"
compile "org.springframework.boot:spring-boot-starter-logging"
compile "org.springframework.boot:spring-boot-autoconfigure"
compile "org.springframework.boot:spring-boot-starter-actuator"
provided "org.springframework.boot:spring-boot-starter-tomcat"
compile "org.grails:grails-core"
compile "org.grails:grails-dependencies"
compile "org.grails:grails-web-boot"
compile "org.grails:grails-datastore-rest-client:5.0.3.RELEASE"
console "org.grails:grails-console"
profile "org.grails.profiles:web:$grailsVersion"
compile "org.grails.plugins:cache"
compile "org.grails.plugins:scaffolding"
compile "org.grails.plugins:hibernate4"
compile "org.grails.plugins:spring-security-rest:2.0.0.M2"
compile "org.grails.plugins:spring-security-rest-gorm:2.0.0.M2"
runtime "org.grails.plugins:asset-pipeline"
runtime group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.7'
runtime group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.7'
// compile group: 'org.elasticsearch', name: 'elasticsearch', version: elasticsearchVersion
// compile group: 'org.elasticsearch.client', name: 'transport', version: elasticsearchVersion
// compile "org.grails.plugins:elasticsearch:1.4.1"
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.13'
compile "org.hibernate:hibernate-ehcache"
compile "org.grails.plugins:cache-ehcache:3.0.0.BUILD-SNAPSHOT"
compile "net.sf.ehcache:ehcache:2.10.2.2.21"
compile "net.sf.ehcache:ehcache-core:2.6.11"
compile "com.google.code.maven-play-plugin.net.tanesha.recaptcha4j:recaptcha4j:0.0.8"
runtime group: "org.postgresql", name: "postgresql", version: "42.1.4"
compile group: "net.java.dev.jna", name: "platform", version: "3.5.0"
compile group: "com.nimbusds", name: "nimbus-jose-jwt", version: "4.8"
compile group: "org.pac4j", name: "pac4j-core", version: "1.8.3"
compile "org.grails.plugins:fields"
testCompile "org.grails:grails-plugin-testing"
// testCompile "org.grails.plugins:geb"
testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"
}
configurations {
all {
exclude module: "bcmail-jdk14"
exclude module: "bctsp-jdk14"
exclude group: "com.google.guava", module: "guava"
exclude group: "com.google.guava", module: "guava-base"
}
}
task wrapper(type: Wrapper) {
gradleVersion = gradleWrapperVersion
}
assets {
minifyJs = true
minifyCss = true
}
gradle.properties:
grailsVersion=3.1.8
gradleWrapperVersion=2.11
Maybe there is something is lacking or something is wrong with the grails/groovy version ? I'm stuck for the entire day so I would appreciate your help.
Update :
Here is the Dockerfile I tried with Tomcat that I mentionned earlier. It gives me the exact same error.
FROM tomcat:8.5.75-jdk8-temurin
COPY egrocery-showcase.war /usr/local/tomcat/webapps
CMD ["catalina.sh", "run"]
I can't believe I wasted two days straight on this because someone wrote the package with an uppercase letter. There was a class in a package :
package com.flosslab.eGrocery.showcase
When I changed it to
package com.flosslab.egrocery.showcase
I got past this error.

Could not resolve project, none of the consumable configurations have attributes

My Jenkins pipeline showed error message like this:
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine the dependencies of task ':distTar'.
> Could not resolve all task dependencies for configuration ':runtimeClasspath'.
> Could not resolve project :ProjectA.
Required by:
project :
> Unable to find a matching configuration of project :ProjectA:
- None of the consumable configurations have attributes.
> Could not resolve project :ProjectB.
Required by:
project :
> Unable to find a matching configuration of project :ProjectB:
- None of the consumable configurations have attributes.
> Could not resolve project :ProjectC.
Required by:
project :
> Unable to find a matching configuration of project :ProjectC:
- None of the consumable configurations have attributes.
> Could not resolve project :ProjectD.
Required by:
project :
> Unable to find a matching configuration of project :ProjectD:
- None of the consumable configurations have attributes.
> Could not resolve project :ProjectE.
Required by:
project :
> Unable to find a matching configuration of project :ProjectE:
- None of the consumable configurations have attributes.
> Could not resolve project :ProjectF.
Required by:
project :
> Unable to find a matching configuration of project :ProjectF:
- None of the consumable configurations have attributes.
I followed some tutorials online, but it seems that none has published the complete steps of how to configure the dependencies with configurations attributes.
My build.gradle goes something like this:
plugins {
id "java-library"
id "application"
}
apply plugin: 'java'
apply plugin: 'java-library'
apply plugin: 'eclipse'
apply plugin: 'application'
repositories {
jcenter()
mavenLocal()
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
flatDir { dirs 'lib' } }
configurations {
implementation
// A configuration meant for consumers that need the API of this component
exposedApi {
// This configuration is an "outgoing" configuration, it's not meant to be resolved
canBeResolved = true
// As an outgoing configuration, explain that consumers may want to consume it
canBeConsumed = true
}
// A configuration meant for consumers that need the implementation of this component
exposedRuntime {
canBeResolved = true
canBeConsumed = true
}
// declare a configuration that is going to resolve the compile classpath of the application
compileClasspath.extendsFrom(implementation)
// declare a configuration that is going to resolve the runtime classpath of the application
runtimeClasspath.extendsFrom(implementation)
}
configurations.compile.resolutionStrategy {
force project }
dependencies {
implementation project(':ProjectA')
implementation project(':ProjectB')
implementation project(':ProjectC')
implementation project(':ProjectD')
implementation project(':ProjectE')
implementation project(':ProjectF')
implementation fileTree(dir: 'lib', include: '*.jar')
implementation fileTree(dir: '../ProjectA/lib', include: '*.jar') implementation fileTree(dir: '../ProjectA/lib/gson', include: '*.jar')
implementation fileTree(dir: 'lib/symbol', include: '*.jar') implementation fileTree(dir: 'lib/symbol/lib', include: '*.jar') implementation fileTree(dir: '../ ProjectC/C/framework/libraries', include: '*.jar')
implementation fileTree(dir: '../ ProjectC/C/framework', include: '*.jar')
api 'org.apache.commons:commons-math3:3.6.1'
api 'junit:junit:4.12'
// Use JUnit test framework
// This dependency provides the public API for writing tests and extensions
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.4.1'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.4.1'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-migrationsupport', version: '5.4.1'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.4.1'
testCompile group: 'org.junit.vintage', name: 'junit-vintage-engine', version: '5.4.1'
testCompile group: 'org.junit.platform', name: 'junit-platform-engine', version: '1.4.1'
testCompile group: 'org.junit.platform', name: 'junit-platform-launcher', version: '1.5.0-M1'
testCompile group: 'junit', name: 'junit', version: '4.12'
//This dependency provides TestFX
testCompile group: "org.testfx", name: "testfx-core", version: "4.0.15-alpha"
testCompile group: "org.testfx", name: "testfx-junit", version: "4.0.15-alpha"
testCompile group: 'org.testfx', name: 'openjfx-monocle', version: 'jdk-9+181.1'
implementation group: 'org.testfx', name: 'testfx-junit5', version: '4.0.15-alpha'
// This dependency runs tests in JUnit 5, 4, 3
testRuntime(
'org.junit.jupiter:junit-jupiter-engine:5.4.1',
'org.junit.vintage:junit-vintage-engine:5.4.1',
'org.testfx:openjfx-monocle:jdk-9+181.1'
)
}
test {
useJUnitPlatform()
testLogging { events "passed", "skipped", "failed" } }
wrapper { gradleVersion = '5.5.1' }
eclipse { classpath {
downloadSources = true
downloadJavadoc = true } }
task sourceJar (type : Jar) { classifier = 'sources'
from sourceSets.test.allSource }
mainClassName = 'id.blablu.test.main.StartMainTest'
compileJava.options.encoding = 'UTF-8'
processResources {
from ('src/test/java') {
include ('**/*.properties') } from('C:/workspace/ProjectD/src'){ include ('**/*.properties') } }
sourceSets {
test{
java {
srcDir 'src/test/java'
}
}
}
def ArrayList vmArgs = ["-Dfile.encoding=UTF8"] if(JavaVersion.current() >= JavaVersion.VERSION_11) { vmArgs = vmArgs
+ [
"--add-modules", "javafx.base",
"--add-modules", "javafx.graphics",
"--add-modules", "javafx.controls",
"--add-modules", "javafx.fxml",
"--add-modules", "javafx.media",
"--add-modules", "javafx.swing",
"--add-modules", "javafx.web"
] }
run {
doFirst {
if(JavaVersion.current() >= JavaVersion.VERSION_1_9) {
jvmArgs = vmArgs + [
"--module-path", classpath.asPath
]
} else {
jvmArgs = vmArgs
}
} }
application {
if(JavaVersion.current() >= JavaVersion.VERSION_1_9) {
applicationDefaultJvmArgs = vmArgs + [
"--module-path", "../lib"
] } else { applicationDefaultJvmArgs = vmArgs } }
If anyone has complete configurations of dependencies and its attributes, please show me or
tell me please how to configure the configurations attributes.
Thanks.

Gradle Spring Boot plugin broken on Linux

I have a multimodule gradle build with a single module containing a spring boot application that consumes the artifacts of the other modules. I have the spring boot plugin applied to the single spring boot module.
Everything works correctly in Windows, but once I try the build on a Linux machine if fails. I have tried multiple linux machines (Jenkins server, Ubuntu VM, even bitbucket pipeline) and it fails with this error:
Problems reading data from Binary store in /tmp/gradle2075404181876889604.bin (exist: false)
I am not entirely sure what gradle uses this binary store for, but after debugging I found that this file name is different than binary store that every other module is using while building.
I have tried multiple gradle versions(2, 3, and 4) and several spring boot versions and they all fail with the same error. The only thing that fixes this is removing the spring boot plugin.
Here is the parent build.gradle
repositories {
mavenCentral()
}
def javaLangVersion = '1.8'
def gradleDir = "${rootProject.rootDir}/gradle"
def realProjects = allprojects - [project(':external'), project(':delivery')]
def javaProjects = realProjects - rootProject
def integrationTestProjects = javaProjects
configure(realProjects) {
group 'com.packagename'
version '1.0-SNAPSHOT'
apply plugin: 'eclipse'
apply plugin: 'idea'
}
configure(javaProjects) {
apply plugin: 'java'
targetCompatibility = javaLangVersion
sourceCompatibility = javaLangVersion
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.slf4j', name: 'slf4j-api', version: "${slf4jVersion}"
compile group: 'org.slf4j', name: 'log4j-over-slf4j', version: "${slf4jVersion}"
testCompile group: 'junit', name: 'junit', version: "${junitVersion}"
testCompile group: 'org.mockito', name: 'mockito-core', version: "${mockitoVersion}"
testCompile group: 'nl.jqno.equalsverifier', name: 'equalsverifier', version: "${equalsverifierVersion}"
}
}
configure(integrationTestProjects) {
apply from: "${gradleDir}/integrationTest.gradle"
}
task wrapper(type: Wrapper) {
gradleVersion = '3.5'
}
Here is the spring boot module build.gradle file:
buildscript {
ext {
springBootVersion = '1.5.3.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootStarterVersion}")
}
}
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
war {
baseName = "${project.name}"
version = "${project.version}"
}
dependencies {
compile project(':module1')
compile project(':module2')
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-rest'
compile group: 'org.apache.httpcomponents', name: 'httpcore', version: "${httpCoreVersion}"
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: "${httpClientVersion}"
compile group: 'com.h2database', name: 'h2', version: "${h2Version}"
testCompile project(':test-utils')
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test'
}
All other modules are plain java modules and only declare a dependencies block in the build file.
I have been working on this for 2 days now so any help or suggestions would be greatly appreciated. Thanks!
Well this is super embarrasing... I had a unit test using FileUtils.getTempDirectory() and then I was cleaning that directory using FileUtils.deleteQuietly after the test and it was deleting the gradle binary store that was in the /tmp folder. The reason it wasnt showing on windows is because windows locks files.
did you verify that your gradle.properties is OK on the Linux machine ?

Define compile dependencies in gradle that dont get packaged

I'm trying to configure gradle to use lombok to compile my project but I don't want the classes appearing in my jar. On the other side I need the mysql-connector dependency packages in the jar, but it's not needed for compiling. This is my build.gradle file:
group 'de.albritter'
version '1.0-SNAPSHOT'
apply plugin: 'java'
jar {
manifest {
attributes 'Main-Class': 'de.albritter.main.Main'
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
sourceCompatibility = 1.7
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile group: 'org.projectlombok', name: 'lombok', version: '1.16.8'
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.39'
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'mysql', name: 'mysql-connector-java', version: '5.1.39'
classpath group: 'org.projectlombok', name: 'lombok', version: '1.16.8'
}
}
I've seen some solutions using compileOnly but if i try to use it I just gete an error that this method is not known.
My gradle version is 2.9
How do I tell gradle that I don't need lombok in my jar?
What you're asking for is variously known as compileOnly or in the maven world, provided dependency. The compileOnly configuration was introduced in gradle in version 2.12. I would strongly recommend moving to the latest version of gradle (2.14 at the time of writing this).
If you need to stick to the older version, there are some workarounds you can find by looking for "gradle provided dependency". One way to do this is to declare your own configuration, lets call it provided and adding its dependencies to compile time classpath. So in your build.gradle:
configurations{
provided
}
sourceSets {
main.compileClasspath += configurations.provided
test.compileClasspath += configurations.provided
test.runtimeClasspath += configurations.provided
}
dependencies {
...
provided 'group:module:version'
...
}
Or alternatively you can use the prodeps plugin which does most of this work for you.

args4J doesn't work with gradle

I'm totally new to gradle so it may be a obvious problem:
I'm using eclipse with gradle and I actually have no problem adding dependencies for junit or stuff, it adds the junit lib to the gradle dependencies and there's no problem using junit, but if i try to use args4j (also with adding the dependency) it just doesn't work.
Just to make sure there's no problem with the build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = 1.8
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'title',
'Implementation-Version': version,
'Main-Class':'path.to.main.Main'
}
}
repositories {
mavenCentral()
}
test {
systemProperties 'property': 'value'
}
dependencies{
compile 'args4j:args4j-site:2.0.25'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
and No I'm not using title or path.to.main ^^
Eclipse shows me that the import (args4j) cannot be resolved
You forgot main "args4j" module:
compile group: 'args4j', name: 'args4j', version: '2.0.25'
compile group: 'args4j', name: 'args4j-site', version: '2.0.25'
With gradle-7.1 this works for me:
// build.gradle
repositories {
mavenCentral()
}
dependencies {
implementation group: 'args4j', name: 'args4j', version: '2.33'
implementation group: 'args4j', name: 'args4j-site', version: '2.33'
}

Categories

Resources