OpenAPI Generator gradle plugin task not executed - gradle

In my android project I am trying to include the openapi-generator plugin to generate a client. I've copied the instructions from the readme for my project. Unfortunately, the task doesen't seem to be executed.
I am new to gradle and android. So I haven't a lot of knowledge on these subjects yet.
This is the projects build.gradle file:
buildscript {
ext {
nav_version = '2.4.2'
}
dependencies {
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
classpath "org.openapitools:openapi-generator-gradle-plugin:5.4.0"
}
}
plugins {
id 'com.android.application' version '7.1.3' apply false
id 'com.android.library' version '7.1.3' apply false
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
This is the build.gradle file of my module:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'androidx.navigation.safeargs.kotlin'
id 'kotlin-kapt'
id "org.openapi.generator"
}
android {
compileSdk 31
defaultConfig {
applicationId "..."
minSdk 29
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
dataBinding true
}
sourceSets {
main {
kotlin {
srcDirs += 'build/generated/source/navigation-args/'
}
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
openApiGenerate {
generatorName = "kotlin"
inputSpec = "$rootDir/app/src/main/assets/swagger.yml".toString()
outputDir = "$buildDir/generated".toString()
apiPackage = "org.openapi.example.api"
invokerPackage = "org.openapi.example.invoker"
modelPackage = "org.openapi.example.model"
configOptions = [
dateLibrary: "java8"
]
}
In the output of the build was no hint that the openApiGenerate task was executed and on the filesystem I didn't find the generated client.
Probably there is a mistake in the configuration, but I don't know what is wrong.
Can you help me?

This command should generate the client :
./gradlew openApiGenerate
If you want to avoid manually running this every time your schema changes, then you can go to the drop down here:
Now choose edit configurations:
Now add a gradle task with the + button:
Then:
Enter the task in run:
Now edit the app configuration, by adding what you just created to the before launch section:
You will see the YourAppsName[openApiGenerate] configuration you created as an option, so select that, then apply changes, and now every time you run your application your client will be built.

Related

How to ignore gradle build of root project for a multi-project Kotlin Spring Boot application?

Background:
I currently have a multi-module (multi-project) application repo. The "root" is not a runnable application. It's merely the source directory where I have a root build.gradle.kts file which holds the dependencies and plugins that are common between all my sub-projects. Each of my sub-projects have their own build.gradle.kts.
So my overall project structure looks sort of like this:
my_root_project
- gradle
- wrapper
- gradle-wrapper.jar
- gradle-wrapper.properties
- gradle.build.kts
- settings.gradle.kts
- my_nested_project_a
- src
- main
- kotlin
- my_nested_project_b
...
Issue:
Every time I run gradle build, I get an error saying:
> Task :bootJar FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':bootJar'.
> Main class name has not been configured and it could not be resolved
However when I run any one of my sub-projects (e.g. build :my_nested_project_a:build), it builds just fine.
Current Gradle Build Files
Here's what I currently have in the "root" gradle.build.kts:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
group = "com.example"
version = "1.0.0"
java.sourceCompatibility = JavaVersion.VERSION_1_8
java.targetCompatibility = JavaVersion.VERSION_1_8
plugins {
id("org.springframework.boot") version "2.1.8.RELEASE" apply false
id("io.spring.dependency-management") version "1.0.8.RELEASE" apply false
kotlin("jvm") version "1.3.50"
kotlin("plugin.spring") version "1.3.50"
kotlin("plugin.jpa") version "1.3.50"
kotlin("plugin.allopen") version "1.3.50"
}
allprojects {
repositories {
maven(url = "https://my.company.com/repo/with/all/the/stuff/I/need")
}
apply(plugin = "org.jetbrains.kotlin.jvm")
apply(plugin = "java")
apply(plugin = "org.springframework.boot")
apply(plugin = "io.spring.dependency-management")
apply(plugin = "org.jetbrains.kotlin.plugin.spring")
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
}
NOTE: I'm using apply false on my plugins because I thought it would keep gradle from trying to find a main class when building using gradle build.
What I'm trying to do:
I have a CI pipeline that I'd like to simply run gradle build which should run the build task for all of the sub-projects. However, in that process, I'd like to ignore running the build for the "root" project or bypass it since it's not a runnable application, and just build the sub-projects.
Any help would be greatly appreciated! :)
If you want to ignore task bootJar,s o add the following configuration.
bootJar {
enabled = false
}
In your root build.gradle.kts, ignore bootJar task, with Kotlin DSL :
import org.springframework.boot.gradle.tasks.bundling.BootJar
tasks.getByName<BootJar>("bootJar") {
enabled = false
}
If you have the plugin applied in allprojects session, you're applying it to the root as well, and since it's the first one resolved in gradle build, you should have the main class configured there.
Alternatively, you can remove the apply(plugin = "org.springframework.boot") line from the root and apply the plugin only to the module that has the main method annotated with #SpringBootApplication, and point the plugin to the main class there.
Say your main class is in my_nested_project_a/src/main/com/example/MainClass.kt.
Your my_nested_project_a/build.gradle.kts should look like:
plugins {
id("org.springframework.boot")
}
springBoot {
mainClassName = "com.example.MainClass"
}
dependencies {
...
}
And you should remove this line from the root build.gradle.kts:
apply(plugin = "org.springframework.boot")
I have a similar setup and question. I replaced allprojects with subprojects and added jar.enabled(false) to the root build.gradle file and it worked.
plugins {
id("java-library")
id('org.jetbrains.kotlin.jvm') version "${kotlinVersion}"
id("com.diffplug.spotless") version "${spotlessVersion}"
id("maven-publish")
}
jar.enabled(false)
subprojects {
apply plugin: "java-library"
apply plugin: "org.jetbrains.kotlin.jvm"
apply plugin: "com.diffplug.spotless"
apply plugin: "maven-publish"
group = GROUP
version = VERSION_NAME
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
dependencies {
testImplementation 'org.jetbrains.kotlin:kotlin-test'
testImplementation("org.junit.jupiter:junit-jupiter:${junitVersion}")
}
publishing {
publications {
library(MavenPublication) {
from components.java
}
}
repositories {
maven {
url "https://gitlab.mhighpoint.com/api/v4/projects/${System.getenv('CI_PROJECT_ID')}/packages/maven"
credentials(HttpHeaderCredentials) {
name = "Job-Token"
value = System.getenv('CI_JOB_TOKEN')
}
authentication {
header(HttpHeaderAuthentication)
}
}
}
}
spotless {
java {
googleJavaFormat() // googleJavaFormat('1.1') to specify a specific version
}
kotlin {
target '**/src/**/*.kt'
ktlint("0.41.0").userData('disabled_rules': 'no-wildcard-imports,import-ordering')
trimTrailingWhitespace()
indentWithSpaces()
endWithNewline()
}
format 'misc', {
target '**/*.gradle'
trimTrailingWhitespace()
indentWithSpaces() // or spaces. Takes an integer argument if you don't like 4
endWithNewline()
}
}
test {
useJUnitPlatform()
}
jar {
archiveBaseName = "${rootProject.name}-${project.name}"
}
tasks {
assemble.dependsOn(spotlessApply)
}
}

Gradle Multi-Project Integration Tests with Junit5

How can I create integrations tests using Gradle 5.2 and JUnit 5.3 in a multi-project build file?
This is my current build file:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:1.6.10"
classpath 'org.owasp:dependency-check-gradle:5.0.0-M2'
}
}
allprojects {
defaultTasks 'clean', 'build', 'publish', 'installDist'
group = 'coyote'
version = '0.1.0'
}
subprojects {
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: "com.github.spotbugs"
apply plugin: 'org.owasp.dependencycheck'
apply plugin: 'maven-publish'
apply plugin: 'application'
apply plugin: 'eclipse'
apply plugin: 'idea'
repositories {
jcenter()
mavenCentral()
}
dependencies {
testImplementation ("org.junit.jupiter:junit-jupiter-api:5.3.2")
testRuntimeOnly ("org.junit.jupiter:junit-jupiter-engine:5.3.2")
spotbugsPlugins ("com.h3xstream.findsecbugs:findsecbugs-plugin:1.8.0")
}
tasks.withType(Test) {
useJUnitPlatform()
}
apply from: "$rootDir/integration-test.gradle"
check.dependsOn jacocoTestCoverageVerification
check.dependsOn dependencyCheckAnalyze
spotbugs {
effort = "max"
reportLevel = "low"
ignoreFailures = true
showProgress = true
}
jacocoTestReport {
reports {
xml.enabled true
html.enabled true
}
}
check.dependsOn jacocoTestReport
tasks.withType(com.github.spotbugs.SpotBugsTask) {
reports {
xml.enabled false
html.enabled true
}
}
}
The integration-test.gradle file I am applying on line 46 contains the following:
sourceSets {
itest {
compileClasspath += sourceSets.main.output + configurations.testCompile
runtimeClasspath += output + compileClasspath + configurations.testRuntime
}
}
idea {
module {
testSourceDirs += sourceSets.itest.java.srcDirs
testResourceDirs += sourceSets.itest.resources.srcDirs
scopes.TEST.plus += [configurations.itestCompile]
}
}
task itest(type: Test) {
description = 'Runs the integration tests.'
group = 'verification'
testClassesDirs = sourceSets.itest.output.classesDirs
classpath = sourceSets.itest.runtimeClasspath
outputs.upToDateWhen { false }
}
Everything seems to work well within Intellij, but JUnit5 is not being added to the classpath, so any tests in the itest directories cannot find the JUnit libraries.
Running gradlew itest fails with similar results with the JUnit classes not being found.
I have tried to add use useJUnitPlatform() directly in the itest task, but with no success. I have also tried placing everything in the build.gradle file with no success.
Eventually, I'd like to use the same pattern to define load and security testing, running them separately as part of a CI/CD pipeline so placing everything neatly in their own directories under their respective projects is preferred to mixing everything in one test directory and using tags.
This is also helping other teams model CI/CD practices, so using Gradle 5 and JUnit 5 as designed is preferred as opposed to work-arounds or hacks to make things work. It's acceptable to learn that these versions don't work together or that there are currently defects\issues preventing this approach. Hopefully this is not the issue and I'm just missing something simple.
The solution is I needed to include the JUnit 5 dependencies myself. The following is the correct integration-test.gradle file:
sourceSets {
itest {
compileClasspath += sourceSets.main.output + configurations.testCompile
runtimeClasspath += output + compileClasspath + configurations.testRuntime
}
}
idea {
module {
testSourceDirs += sourceSets.itest.java.srcDirs
testResourceDirs += sourceSets.itest.resources.srcDirs
scopes.TEST.plus += [configurations.itestCompile]
}
}
dependencies {
itestImplementation ("org.junit.jupiter:junit-jupiter-api:5.3.2")
itestRuntimeOnly ("org.junit.jupiter:junit-jupiter-engine:5.3.2")
}
task itest(type: Test) {
description = 'Runs the integration tests.'
group = 'verification'
testClassesDirs = sourceSets.itest.output.classesDirs
classpath = sourceSets.itest.runtimeClasspath
outputs.upToDateWhen { false }
}

Gradle cannot find method android() for arguments (Gradle 2.2.3)

Gradle for my project isn't working. I've checked other questions that have had the same problem, but this one is in a newer version and I'm not sure how to solve it. I'm trying to get the Android SDK up and running for the first time, so I'm really new to this. Thorough answers are appreciated.
Here is my Gradle error:
Error:(22, 0) Could not find method android() for arguments [build_3bh8pl96cp64yo421v01fuk9s$_run_closure3#4b7fff2f] on root project 'Meet-upProject' of type org.gradle.api.Project.
And here is my build.gradle in root:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
dexOptions {
incremental true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_6
targetCompatibility JavaVersion.VERSION_1_6
}
}
dependencies {
compile files('app/libs/junit-4.12-JavaDoc.jar')
}

Could not find property 'plugin' on project:

I have an SDK for which I've developed a number of small test applications. These are all stand alone android apps which have the following in common:
1) All dependencies are the same (i.e. same version of my sdk/android sdk/etc..)
2) They all share a common top level build.gradle file (which is currently empty):
3) They're build,test etc.. phases are all identical.
I want the projects to all exist as modules in Android studio, and I want them to share as much configuration as possible in the top level build.gradle so that I can update the one file and have everything working correctly.
When I add the following to my top level build.gradle:
subprojects {
apply plugin: "android"
repositories {
mavenCentral()
maven {
url "my.repo.com"
credentials {
username "user"
password "pass"
}
}
dependencies {
compile 'com.my.library:my-library:0.0.1'
// compile 'com.google.android.gms:play-services:+'
}
}
}
I get
'could not find property 'plugin' on ':App1' where ':App1' is the first app in settings.gradle, and the build fails.
My goals would be to get the above block and as many other applicable elements in this top level file.
Edit: I've fixed the syntax error (i was missing the :), but I've that leads to a "plugin with id 'android' not found. Generally this would be resolved by adding:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.14.4'
}
}
allprojects {
repositories {
mavenCentral()
}
}
But that doesn't resolve it in this case.
Edit 2:
final form:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.14.4'
}
}
allprojects {
repositories {
mavenCentral()
}
}
subprojects {
apply plugin: "android"
repositories {
mavenCentral()
maven {
url "my.repo.com"
credentials {
username "user"
password "pass"
}
}
dependencies {
compile 'com.my.library:my-library:0.0.1'
// compile 'com.google.android.gms:play-services:+'
}
}
android {
compileSdkVersion 19
buildToolsVersion '19.1.0'
lintOptions {
abortOnError false
}
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
defaultConfig {
minSdkVersion 16
targetSdkVersion 19
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
productFlavors {
}
}
}
That seems to be working correctly.
This is a simple syntactical error. The apply() method takes a Map as argument. Don't forget the colon (:).
apply plugin: 'android'

Android Gradle Build Resource.getIdentifier() always returns 0

I'm getting started on porting my Game over to the new Gradle Build system for Android.
I'm using the Resource.getIdentifier() method to load some resources by name instead of ID. However, ever since switching to the Gradle build, this call always returns 0. The assets are included in the built jar, and the R.class in the dex file contains all of my resources and ID's as expected, so I am at a loss as to explain this. This code worked before I changed to the gradle build. My build.gradle is below:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android'
dependencies {
compile 'com.android.support:support-v4:18.0.+'
}
android {
compileSdkVersion 18
buildToolsVersion "19"
defaultConfig {
minSdkVersion 10
targetSdkVersion 18
}
buildTypes {
debug {
packageNameSuffix ".debug"
}
release {
runProguard true
proguardFile getDefaultProguardFile('proguard-android.txt')
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
}
TIA.
D'oh! my problem was that Resources.getIdentifer() takes 3 parameters, one of which is the package name. The debug section of my gradle script was tacking on ".debug" to the package name on the device. Solution is to remove the .debug suffix in the build.gradle.

Resources