I have directory structure as below:
src
--foo
--A.groovy
--test
--groovy
buildTest.groovy
This is the A.groovy class :
package foo
class A {
void execute() {
println "Inside A"
}
}
This my buildTest.groovy
class buildTest {
#Test
void should_execute(){
A a = new A()
def result = a.execute()
}
}
When I run ./gradlew test, it throws error :
Unable to resolve class A
A a = new A()
^
This is my build.gradle :
plugins {
id "groovy"
}
repositories {
mavenCentral()
maven { url 'https://repo.jenkins-ci.org/releases/' }
}
dependencies {
compile 'org.eclipse.hudson:hudson-core:3.2.1'
implementation 'org.codehaus.groovy:groovy-all:3.0.7'
testImplementation 'junit:junit:4.12'
testImplementation "com.lesfurets:jenkins-pipeline-unit:1.3"
}
sourceSets {
main {
groovy {
srcDirs = ['src/foo']
}
}
}
Could someone help me in resolving this error.
You have declared a class A in package foo. (Note that the layout doesn't reflect this as the file should be in src/foo/foo/A.groovy, although Groovy doesn't really care).
However, the test class is not in a package and therefore can't see foo.A. Either import it or move the test class to package foo as well. Alternatively, remove the package declaration from class A, though I don't think that is good practice.
I also agree with the comment about not changing the layout unless you have a very good reason for it (e.g. you are working on a legacy project). This is probably what has caused confusion about what package the class is actually in.
Related
I am trying to set up testFixtures. I have a class that until now was in src/test/ and now I'm trying to turn it into fixrure.
In build.gradle I added:
plugins {
id 'java-library'
id "java-test-fixtures"
}
I moved the class from src/test/java/com/example/AmazonTestConfig.java to src/testFixtures/java/com/example/AmazonTestConfig.java
And in the dependencies I had to add a few things, i.e:
dependencies {
...
testFixturesImplementation project(':MyCommon')
testFixturesImplementation 'com.amazonaws:aws-java-sdk-s3:1.12.75'
testFixturesImplementation 'junit:junit:4.13'
testFixturesImplementation 'org.mockito:mockito-all:1.9.0'
}
The first 3 dependencies work, but the mockito doesn't. Here's part of my Foo.java:
import static org.mockito.Mockito.*;
#Ignore
public interface AmazonTestConfig {
default void setS3FilesDal(ServerEngine engineMock) {
ICloudStorage s3FilesDal = new AWSStorage();
CloudServices cloudServicesMock = Mockito.mock(CloudServices.class);
Mockito.when(engineMock.getCloudServices()).thenReturn(cloudServicesMock);
Mockito.when(cloudServicesMock.getStorage()).thenReturn(s3FilesDal);
}
}
But I get the following compilation error:
myproject/src/testFixtures/java/com/example/AmazonTestConfig.java:23: error: cannot find symbol
CloudServices cloudServicesMock = Mockito.mock(CloudServices.class);
^
symbol: variable Mockito
location: interface AmazonTestConfig
I'm not sure why, but changing:
import static org.mockito.Mockito.*;
to:
import org.mockito.Matchers;
import org.mockito.Mockito;
fixed the problem. Looks like a bug in gradle.
I've got a method in my Gradle plugin which declares a new "scratch" source set. I'm trying to convert this method from Groovy to Kotlin, but I cannot figure out how I'm meant to declare the new SourceSet using Kotlin.
class JavaConventionsPlugin : Plugin<Project> {
// ...
def scratchConfiguration(project) {
project.sourceSets {
scratch {
}
}
project.configurations {
// make scratch configurations include all normal dependencies automatically
scratchCompile.extendsFrom mainCompile
scratchRuntime.extendsFrom mainRuntime
scratchCompileOnly.extendsFrom compileOnly
scratchImplementation.extendsFrom implementation
scratchRuntimeOnly.extendsFrom runtimeOnly
}
project.dependencies {
// add dependency on main java code from scratch java code
scratchImplementation project.extensions.getByType(JavaPluginExtension).sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME).output
}
}
}
Within the converted scratchConfiguration method, all of these seem to be unresolved references:
sourceSets
project.sourceSets
java.sourceSets
So what should the new SourceSet declaration look like?
They are unresolved because you are moving from a weakly typed/dynamic language (Groovy) to a strongly typed one (Kotlin). At its core, Gradle is written in plain Java. So, you need to make use of the Java APIs instead of the syntactic sugar that the Groovy DSL had provided.
Your snippet above would moreorless translate to the following:
import org.gradle.api.Project
import org.gradle.api.Plugin
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceSetContainer
class ScratchGradlePlugin: Plugin<Project> {
override fun apply(project: Project) {
val sourceSets = project.extensions.getByType(SourceSetContainer::class.java)
val main = sourceSets.named(SourceSet.MAIN_SOURCE_SET_NAME)
sourceSets.register("scratch") {
it.compileClasspath.plus(main.get().output)
it.runtimeClasspath.plus(main.get().output)
}
val configurations = project.configurations
configurations.named("scratchImplementation") {
it.extendsFrom(configurations.named(JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME).get())
}
configurations.named("scratchRuntimeOnly") {
it.extendsFrom(configurations.named(JavaPlugin.RUNTIME_ONLY_CONFIGURATION_NAME).get())
}
}
}
I have omitted the project.dependencies {} part in your snippet since the sample above has scratch extend from main which is your main Java code.
Ref: https://docs.gradle.org/current/userguide/java_gradle_plugin.html#java_gradle_plugin
I'm writing a custom plugin which adds some data to manifest of Java project.
It looks something like this:
package com.example.gradle
import org.gradle.api.Plugin
import org.gradle.api.Project
public class ExamplePlugin implements Plugin<Project> {
def apply(Project project) {
project.jar() {
manifest {
attributes 'buildServer': checkIfIsBuildServer()
attributes 'personalBuild': checkIfIsPersonalBuild()
}
}
}
def checkIfIsBuildServer() {
return 'some result'
}
def checkIfIsPersonalBuild() {
return 'some result'
}
}
When I'm trying to apply it to some project, I get an error:
Could not find method jar() for arguments [com.example.gradle.ExamplePlugin$_apply_closure1#411e4f5e] on project ':SomeProject' of type org.gradle.api.Project.
I am reasonably sure this is some missing import. I don't have any idea how to determine what import it should be.
jar() isn’t a method on Project.
If I’m understanding your code correctly, what you are trying to do is configure the jar task that is created from the Java Plugin.
So you need to:
Get the task
Configure the task
Something like:
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.tasks.bundling.Jar
public class ExamplePlugin implements Plugin<Project> {
def apply(Project project) {
project.afterEvaluate {
project.tasks.named(JavaPlugin.JAR_TASK_NAME, Jar) {
it.manifest {
attributes 'buildServer': checkIfIsBuildServer()
attributes 'personalBuild': checkIfIsPersonalBuild()
}
}
}
}
def checkIfIsBuildServer() {
'some result'
}
def checkIfIsPersonalBuild() {
'some result'
}
}
I highly recommend switch to Kotlin or Java for your plugin. It will make errors like this trivial to resolve and you will fully understand where things are coming from compared to Groovy’s dynamic nature.
I am new to gradle. So let straight to the point. I want to implement the block as below. Note that the libraries is dynamic, and available for other developers to add on for the needed libraries.
libraries {
slf4j 'org.slf4j:slf4j-api:1.7.21'
junit 'junit:junit:4.12'
}
So that I can call them out like this.
dependencies {
compile libraries.slf4j
testCompile libraries.junit
}
I am not sure how to make it. But I found some related solution from here. As shown below:
apply plugin: GreetingPlugin
greeting {
message 'Hi'
greeter 'Gradle'
}
class GreetingPlugin implements Plugin<Project> {
void apply(Project project) {
project.extensions.create("greeting", GreetingPluginExtension)
project.task('hello') {
doLast {
println "${project.greeting.message} from ${project.greeting.greeter}"
}
}
}
}
class GreetingPluginExtension {
String message
String greeter
}
The problem is as I add on to the greeting block, I need to declare them in GreetingPluginExtension as well. Any idea how to make it such that only update on greeting block?
What you need to do is to utilize groovy meta programming. Below you can find just a sample, however fully functional.
apply plugin: LibrariesPlugin
libraries {
slf4j 'org.slf4j:slf4j-api:1.7.21'
junit 'junit:junit:4.12'
}
class LibrariesPlugin implements Plugin<Project> {
void apply(Project project) {
project.extensions.create("libraries", LibrariesPluginExtension)
project.task('printLib') {
doLast {
println "$project.libraries.slf4j"
println "$project.libraries.junit"
project.libraries.each {
println "$it.key -> $it.value"
}
}
}
}
}
class LibrariesPluginExtension {
Map libraries = [:]
def methodMissing(String name, args) {
// TODO you need to do some arg checking here
libraries[name] = args[0]
}
def propertyMissing(String name) {
// TODO same here
libraries[name]
}
Iterator iterator() {
libraries.iterator()
}
}
Can a groovy class (located in buildSrc/src/main/groovy) access the project directly, or does the project have to be passed in explicitly?
I am able to access the project by explicitly passing it in as a method parameter, but I do not want to have to pass it in. For an example, I would like to be able to get access to the project via a static method call. Is this type of implicit access possible?
Explicit Access
import org.gradle.api.Project
class MyClazz {
static void foo(Project project) {
println project.version
}
}
Task in build.gradle
task foo() << {
MyClazz.foo(project)
}
Implicit Access via Static Method Call (this is the desired access pattern)
import org.gradle.api.Project
class MyClazz {
static void foo() {
println Project.getProject().version
}
}
Task in build.gradle
task foo() << {
MyClazz.foo()
}
You can use Groovy extension methods to do this.
here's a self-contained example, but should work with Gradle too:
class Project {
// we add this method dynamically
//static getProject() { [ version: 2.3 ] }
}
class MyClazz {
static void foo() {
println Project.getProject().version
}
}
class Gradle {
static def main(args) {
Project.metaClass.static.getProject = { [ version: 4.2 ] }
MyClazz.foo()
}
}