I am using Java, Spring, and Gradle. I am new to gradle and am trying to understand some stuff. I currently have gradle successfully building a jar file.
I would like to add a task so that gradle runs the jar that it builds.
this way I could do
./gradlew clean build run
or I could just do it in one command if possible. The pseudocode of which would be
task run {
clean
build
java -jar myJar.jar
}
How would I go about doing this?
---INFO----
my current build.gradle file:
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-snapshot" }
mavenLocal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:0.5.0.M6")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'gs-rest-service'
version = '0.1.0'
}
repositories {
mavenCentral()
maven { url "http://repo.spring.io/libs-snapshot" }
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web:0.5.0.M6")
compile("com.fasterxml.jackson.core:jackson-databind")
testCompile("junit:junit:4.11")
}
task wrapper(type: Wrapper) {
gradleVersion = '1.10'
}
It's important to think in task dependencies. run wouldn't execute some other tasks sequentially (tasks can't do that); rather, it would declare task dependencies on its prerequisites, such as perhaps jar. Anyway, the easiest solution is to just apply the application plugin, which you can learn more about in the Gradle User Guide.
Related
I am trying to configure a project that uses react-native and google firebase native.
The set up for react-native worked fine, but I ran into a problem with google firebase native.
I followed the instructions at rnfirebase.io.
When I try to sync My project with gradle files from Android Studio, I get the following error:
Build file 'C:\github\flashcards\flashcards\android\app\build.gradle' line: 139
A problem occurred evaluating project ':app'.
> Could not find method plugins() for arguments [build_2psolmxgn6smlx7165ef739cv$_run_closure1$_closure5#1b639892] on extension 'android' of type com.android.build.gradle.internal.dsl.BaseAppModuleExtension.
* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
...
Here is the relevant portion of my /android/build.gradle file:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
...
repositories {
google()
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle:7.2.1")
classpath("com.facebook.react:react-native-gradle-plugin")
classpath("de.undercouch:gradle-download-task:5.0.1")
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath('com.google.gms:google-services:4.3.14')
}
}
allprojects {
repositories {
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url("$rootDir/../node_modules/react-native/android")
}
maven {
// Android JSC is installed from npm
url("$rootDir/../node_modules/jsc-android/dist")
}
mavenCentral {
// We don't want to fetch react-native from Maven Central as there are
// older versions over there.
content {
excludeGroup "com.facebook.react"
}
}
google()
maven { url 'https://www.jitpack.io' }
}
}
I added the following two lines to the top of my /android/app/build.gradle file:
apply plugin: "com.android.application"
apply plugin: 'com.google.gms.google-services'
Any suggesions for how to fix this problem?
I expected the Sync Project with Gradle files command to complete successfully, but I receive the error described above.
I want to write a simple ORM on Kotlin Native. I'm creating a project in the Multiplatform Library studio. Found the Sql Delight driver. According to the documentation, I'm trying to install it. But nothing comes out. How to connect SqlDelight driver to Multiplatform Kotlin Library?
Here is my gradle.wrappers:
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
build.gradle:
plugins {
id 'org.jetbrains.kotlin.multiplatform' version '1.3.41'
}
repositories {
google()
mavenCentral()
}
group 'com.example.test'
version '0.0.1'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'maven-publish'
apply plugin: 'com.squareup.sqldelight'
archivesBaseName = 'store-item'
configurations {
compileClasspath
}
kotlin {
jvm()
sourceSets {
commonMain {
dependencies {
implementation kotlin('stdlib-common')
}
}
commonTest {
dependencies {
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}
jvmMain {
dependencies {
implementation kotlin('stdlib-jdk8')
implementation "com.squareup.sqldelight:sqlite-driver:1.1.4"
}
}
jvmTest {
dependencies {
implementation kotlin('test')
implementation kotlin('test-junit')
}
}
}
}
Gradle writes: Plugin with id 'com.squareup.sql delight' not found.
The first step in the SQLDelight docs is to set up the Gradle plugin.
By default Gradle will only search the Gradle Plugin Portal for plugins. But the SQLDelight Gradle plugin isn't published to Gradle Plugin Portal, it's published to Maven Central.
You need to add config to tell Gradle to search Maven Central for Gradle plugins.
However, you are on an old version of Gradle - 5.6. The current version of SQLDelight is 1.5.4, which is only compatible with Gradle 7+. You'll need to update your version of Gradle.
I have a Kotlin Android Studio project I am making that integrates with Firebase. I added all the lines required listed in Firebase's instructions into build.gradle for both the Project level and the App level.
But this doesn't include any of the dependencies my project seems to need, I get an error when trying to build and these warnings:
The Declared Dependencies list for my app module only has the two Firebase dependencies, and not any of the dependencies as listed by the above warnings:
I noticed there are some dependencies I assume I need in All Modules:
Is there a way I can add those into my app module folder? I am not sure if that's how it works so let me know.
If this is not the way, please let me know how I can add all these required dependencies quickly onto my Project (preferrably without manually searching each one's repository url and such) so that I can Sync and Build without any errors.
The Gradle Sync was just now successful, here's what happened:
During the editing of the build.gradle files, Google Firebase's instructions were to add this line of code to the Project level's build.gradle file:
buildscript {
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
}
dependencies {
...
// Add this line
classpath 'com.google.gms:google-services:4.3.10'
}
}
allprojects {
...
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
...
}
}
When Syncing the files through Gradle, an error occurred stating that settings.gradle was prefered to build.gradle, as it also contains repositories and dependencies. The fix was to remove the dependencyResolutionManagement code block in settings.gradle.
Here's what I did to fix all the Sync Warnings and Build errors:
Added the same dependencies found in settings.gradle to build.gradle in the Project level:
gradlePluginPortal()
google()
mavenCentral()
It did not work yet, so then I noticed you also need to add it under the allprojects code block. It will then look something like this:
buildscript {
repositories {
// Check that you have the following line (if not, add it):
gradlePluginPortal()
google() // Google's Maven repository
mavenCentral()
}
dependencies {
...
// Add this line
classpath 'com.google.gms:google-services:4.3.10'
}
}
allprojects {
...
repositories {
// Check that you have the following line (if not, add it):
gradlePluginPortal()
google() // Google's Maven repository
mavenCentral()
...
}
}
And that was all. After that the Gradle Sync took a long time this time and completed successfully without any errors, and the Build worked as well and ran fine as expected.
I am currently working on a Gradle custon plugin that should analyse my root project for specific configs in every subproject and then generate some kotlin source code in the build dir. I can't figure out a way to invoke my annotation processor from my gradle plugin which has a custom task for this matter.
Any ideas how to achieve this? Any resource/tutorial/documentation is also highly welcomed.
Thanks in advance and be safe.
After a long time of googling and mostly trying and failing, I finally figured out the solution to my question. Here is my task configuration.
Basically we have to provide the annotation processor's classpath as a project configuration. In my case I added this block to the project's build.gradle
allprojects {
configurations {
myProcessor //pick any name!!!
}
}
and then as a dependency in app build.gradle
dependencies {
myProcessor "PATH_TO_MY_PROCESSOR_JAR" //or maven dependency if it's uploaded to maven central
}
tasks.register(
"myTaskName",
JavaCompile::class.java
) {
compiler ->
with(compiler.options) {
isFork = true
isIncremental = true
}
with(compiler) {
group = shuttle.plugin.ShuttlePlugin.TASK_GROUP
destinationDir = outputDir
classpath = variant.getCompileClasspath(null)
options.annotationProcessorPath = configurations.getByName("myProcessor") //this is the missing piece!!
source = files(projectDir.resolve("src/main/java")).asFileTree
}
}
However, this task will only compile Java classes Only and not kotlin.
Any Idea how to fix this behaviour knowing that my plugin targets only android apps so I don't have direct access to kotlinCompile gradle default task?
Is there a way in gradle to specify a dependency (a jar), and then run that jarfile directly within a task?
Here is one way:
configurations {
tool
}
dependencies {
tool "some:tool:1.0"
}
task runTool(type: JavaExec) {
main = "some.tool.Main"
classpath configurations.tool
}
If you don't know the main class and/or want to do the equivalent of java -jar, you need to employ a workaround as described in http://issues.gradle.org/browse/GRADLE-1274.