How to define my own configuration in gradle plugin? - gradle

In gradle plugin I wrote
project.configurations.create("myConf")
But when I import my plugin into project and write myConf("smth:smth:smth") the compiler doesn't recognize myConf

Well, the project with that plugin couldn't resolve my custom configuration at compile time.
I had to declare a variable val myConf = configurations.getByName("myConf") and then I could use it

Related

Need help on java2wsdl using gradle

I have a java project to which I build it using gradle build and generate a war file.
Currently my requirement is to generate WSDL file at the time of build from java classes. I came to know about axis2-java2wsdl-maven-plugin and found the syntax of applying it in gradle. But I am not able to get the tasks list or the example of using this plugin in gradle to generate the WSDL file using this plugin.
Can anybody let me know of how to use this plugin or any other help so that I can generate WSDL file form my java classes.
Dependency section which I included in build.gradle:
repositories {
mavenCentral()
}
dependencies {
'org.apache.axis2:axis2-java2wsdl-maven-plugin:1.6.2'
}
axis2-java2wsdl-maven-plugin is a maven plugin not a gradle one.
Moreoever, gradle plugins must be defined in a buildscript closure or a plugins one if you want to use the new plugins DSL.
Here, you are just using the maven plugin as a regular dependency for your project.
As far as i know, there is not "java2wsdl" gradle plugin.

gradle fails to compile java classes using kotlin class

I have a gradle 4.1 multiproject containing a "projectA" containing 2 subfolders "api" and "implementation".
The multiproject uses kotlin and java-library plugins defined in the subprojects section of the main build.gradle.
The implementation project avec a API dependency to :projectA:api
In the api folder I have kotlin and java files inside 'src/main/java' and in the implementation project I'm creating a new instance of a kotlin class from the API.
Inside Intellij Idea, I don't have any compilation errors ; but when I compile the whole project using gradle I have an error: cannot find symbol. It is as if the compileJava doesn't have access to the folder kotlin-classes.
Inside the build/kotlin-classes, I see my file.class
The class file is on build/classes dir also
Details of the error :
Task :projectA:api:compileKotlin
Using kotlin incremental compilation
Task :projectA:implementation:compileJava
(...) error: cannot find symbol (the import fails)
Update 1 : removing java-library solved my problem
This is a known issue of the java-library plugin: when used in a project with another JVM language (Kotlin, Scala, Groovy etc.) , it does not register the classes of the other language so that the dependent projects get them as they consume the classes.
Fortunately, it has a workaround as well. Adapted to Kotlin, it would look like:
configurations {
apiElements {
outgoing.variants.getByName('classes').artifact(
file: compileKotlin.destinationDir,
type: ArtifactTypeDefinition.JVM_CLASS_DIRECTORY,
builtBy: compileKotlin)
}
}
If you use Kapt1, it's file: compileKotlinAfterJava.destinationDir, and for Gradle versions lower than 4.0 use builtBy: copyMainKotlinClasses instead.
This issue is also tracked in the Kotlin issue tracker: KT-18497, follow that issue to see when it's fixed on the Kotlin Gradle plugin side, so that the above workaround will be no more necessary.

How to get source code directory in Gradle plugin?

I want to get the directory of Java files in a Groovy plugin.
For example, I have a Java file in a directory:
"/gradleProject/src/main/java/com/file.java"
How can I get:
"src/main/java"
In Maven there is Build.getSourceDirectory(), what is the equivalent in Gradle?
Further to Rene's answer, the groovy DSL makes it easy to get the SourceSet
SourceSet mainSourceSet = project.sourceSets.main
In java this is a little bit more verbose
SourceSet mainSourceSet = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().getByName("main");
In Gradle, those source folders are managed by SourceSets, that are brought to you by the Java plugin. The java plugin adds two sourceSets named main and test.
once the java plugin is applied, you can access those sourceSets and their properties (e.g. the folders you're looking) simply by name: project.sourceSets.main.srcDirs - this will give you all the configured source directories for the main sourceSet in your project.
The latest way to do this, using the Kotlin DSL:
val javaExt = project.extensions.getByType(JavaPluginExtension::class.java)
val mainJavaSource = javaExt.sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME)
Using the Kotlin DSL (Gradle 7.5)
tasks.register<JacocoReport>("jacocoTestReport") {
dependsOn("testDebugUnitTest")
val sourceSets = project.extensions.getByType<KotlinProjectExtension>().sourceSets
sourceDirectories.setFrom(sourceSets)
}
Note that besides the JavaPluginExtension there is also a KotlinAndroidProjectExtension, so you may can get rid of the Android Gradle Plugin in your community plugins.

spring-boot with the gradle java java-plugin

I use gradle with the new java-library plugin.
With this plugin we can use new configurations for dependencies instead of 'compile', 'runtime', 'test' etc.
see java-library plugin documentation
But with the spring-boot plugin, when I launch the task
gradle build
The produced jar does not contain the dependencies of the project.
It is because I use the 'implementation' configuration.
If I use 'compile' configuration as I did before, it works.
Is there a solution, to use these new configurations ?
Or do you plan to implement this new feature in the next versions of spring-boot.
Thank you :)

What types (configurations) of dependencies are available out of the box in Gradle?

I want to write a Gradle script to download a JAR file from Maven and combine it with some other resources to create an RPM file using the nebula.ospackage plugin.
I can't declare the dependency using the compile configuration as it is not available without using the java plugin (and it also doesn't make sense to use compile as I am not compiling anything).
Is there a type of dependency I can use for this purpose?
Or am I required to use the java plugin?
As far as I understood java plugin will not be required. Please have a look a the script below - you can define custom configuration and use it in build script. As you can see, java plugin is not applied:
repositories {
mavenCentral()
}
configurations {
lol
}
dependencies {
lol 'junit:junit:4.12'
}
task cp(type: Copy) {
from configurations.lol
into ('lol')
}

Resources