I'm new to kotlin and gradle and tried to set up my very first project:
build.gradle
buildscript {
ext.kotlin_version = '1.0.1-1'
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: "kotlin"
src\main\kotlin\main.kt
package hello
fun main(args: Array<String>) {
println("Hello World!")
}
And I get the error message "src\main\kotlin\main.kt: (4, 4): Unresolved reference: println".
The build.gradle file I copied from http://kotlinlang.org/docs/reference/using-gradle.html
I'd expect that the standard libraries are included automatically - or do I need to add something here?
I'm using gradle 2.12, JDK 1.8. (in case this matters)
The reference is missing the kotlin-stdlib dependency. It is not added automatically.
kotlin-gradle-plugin buildscript dependency is only Gradle plugin for Kotlin builds, and it doesn't add any dependencies to your project code. In order to use the standard library, one should add it as a dependency.
Append the following to your build.gradle:
repositories {
jcenter()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
(jcenter() is needed again, these repositories are different from those in buildscript)
Related
I've got a Gradle multi-project build for a custom Spring Boot Starter. Following Spring Boot Starter convention, my classes are all in an 'autoconfiguration' project, and I have a separate 'starter' project that ONLY brings in the dependencies needed to use the autoconfiguration.
The multi-project build produces 2 non-runnable jars, 1 for the autoconfiguration sub-project, and one for the starter sub-project. My new project that is using this starter pulls in the starter jar, but when I go to use classes that are from transitive dependencies, my project can't find them anywhere on the classpath.
When I dug into the starter jar, I found that all the dependencies it defines are RUNTIME scoped, which would explain the problem. I can 'fix' the problem by setting all the dependencies in my starter as 'compile' instead of 'implementation', but it's my understanding that 'compile' is on its way out, and that 'implementation' dependencies should be compile-scoped anyway. Can someone tell me what additional config may be necessary to define the starter dependencies as 'implementation' without them being scoped as 'runtime' in the resulting jar?
My starter/autoconfigure multi-project root gradle file:
plugins {
id 'org.springframework.boot' version '2.1.4.RELEASE' apply false
id 'io.spring.dependency-management' version '1.0.7.RELEASE' apply false
}
wrapper {
gradleVersion = '5.2.1'
}
repositories {
mavenLocal()
// private repo info omitted
mavenCentral()
jcenter()
}
subprojects {
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven-publish'
apply plugin: 'java-library'
group = 'com.mystarter'
repositories {
mavenLocal()
// private repo info omitted
mavenCentral()
jcenter()
}
dependencies {
annotationProcessor "org.springframework.boot:spring-boot-autoconfigure-processor"
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
}
bootJar {
enabled = false
}
jar {
enabled = true
}
javadoc {
failOnError = false
options.addStringOption('Xdoclint:none', '-quiet')
}
task sourcesJar(type: Jar) {
from sourceSets.main.allJava
classifier = 'sources'
}
task javadocJar(type: Jar) {
from javadoc
classifier = 'javadoc'
}
publishing {
publications {
myProjStarterArtifacts(MavenPublication) {
from components.java
artifact sourcesJar
artifact javadocJar
}
}
repositories {
// private repo info omitted
}
}
tasks.build.finalizedBy tasks.publishToMavenLocal
}
My starter sub-project's build file:
dependencies {
compile project(':myproj-spring-boot-autoconfig')
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.security:spring-security-cas'
implementation 'org.springframework.security:spring-security-ldap'
}
If I change the above 'implementation' lines to be 'compile' lines, that's when the resulting pom file stops making these 4 dependencies 'runtime' scope and instead correctly scopes them as 'compile'. As a side note, the 'compile project' line works just fine, it's just the lines that are 'implementation' that don't seem to work the way I'd expect when the project has no classes of its own.
My new project's dependency for my starter:
dependencies {
implementation('com.myproj:myproj-spring-boot-starter:1.0.0')
// other dependencies
}
implementation dependencies defined in a Gradle project are only made transitive for the runtimeClasspath of the consumers of said project, that is by design.
If you have a project without code but only defining dependencies, consider using the java-platform plugin for it, which allows you to specify constraints and optionally dependencies.
Otherwise, if you want the project to expose its dependencies to consumers at compilation time, you should use the api configuration for declaring them instead of compile which is indeed on its way out.
For more details, see documentation.
I'm trying to add the library Exposed to my project. So, it leads me to the bintray page where it says to use compile 'org.jetbrains.exposed:exposed:0.8.5'. I open my file build.gradle and place that file into the dependencies segment:
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
compile 'org.jetbrains.exposed:exposed:0.8.5'
}
IntelliJ auto builds it and I get the following error
Warning:root project 'DB-Table-To-Orm': Unable to build Kotlin
project configuration Details:
java.lang.reflect.InvocationTargetException: null Caused by:
org.gradle.api.artifacts.ResolveException: Could not resolve all
dependencies for configuration ':compileClasspath'. Caused by:
org.gradle.internal.resolve.ModuleVersionNotFoundException: Could not
find org.jetbrains.exposed:exposed:0.8.5. Searched in the following
locations:
https://repo1.maven.org/maven2/org/jetbrains/exposed/exposed/0.8.5/exposed-0.8.5.pom
https://repo1.maven.org/maven2/org/jetbrains/exposed/exposed/0.8.5/exposed-0.8.5.jar
Required by:
project :
So, I look in the repo and there is no path beyond jetbrains with the exposed directory.
How do I install the Exposed library with Gradle? Do they have the path written down incorrectly? Should I put in a bug report with the project? Or am I just putting the compile statement in the wrong location?
Sorry, if this seems like a silly request, I'm new to Javaland and Kotlin and IntelliJ. Coming for the .NET world.
Update
Here's the build.gradle in its entirety:
group 'com.awebsite.db-table-to-orm'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.1.4-2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin'
repositories {
mavenCentral()
jcenter()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
compile 'org.jetbrains.exposed:exposed:0.8.5'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
As far as I know Exposed isn't in the main bintray repo (aka jcenter). To make gradle search in Exposed's repo you need to add this:
maven {
url "https://dl.bintray.com/kotlin/exposed"
}
to your repositories section.
Example:
repositories {
mavenCentral()
maven {
url "https://dl.bintray.com/kotlin/exposed"
}
}
Then just rebuild and it should work just fine
I'm trying to do some work with the springfox project which has been broken up into two separate projects: the springfox runtime, and a suite of demos.
In order to investigate the behavior of certain configurations, I need to change the module in springfox/springfox-petstore, and compile that into springfox-demos/springfox-java-swagger.
In springfox, I built and published a new version of springfox-petstore, and validated that it exists correctly in ~/.m2/repository/io/springfox/springfox-petstore/2.2.2-SNAPSHOT.
Next, in springfox-demos I added mavenLocal() as a repository, and added the springfox-petstore-2.2.2-SNAPSHOT as a changing=true dependency.
When I attempt to build the springfox-demos runtime, I get the following error:
* What went wrong:
A problem occurred configuring project ':spring-java-swagger'.
> Could not resolve all dependencies for configuration ':spring-java-swagger:runtimeCopy'.
> Could not find io.springfox:springfox-petstore:2.2.2-SNAPSHOT.
Searched in the following locations:
https://jcenter.bintray.com/io/springfox/springfox-petstore/2.2.2-SNAPSHOT/maven-metadata.xml
https://jcenter.bintray.com/io/springfox/springfox-petstore/2.2.2-SNAPSHOT/springfox-petstore-2.2.2-SNAPSHOT.pom
https://jcenter.bintray.com/io/springfox/springfox-petstore/2.2.2-SNAPSHOT/springfox-petstore-2.2.2-SNAPSHOT.jar
http://oss.jfrog.org/artifactory/oss-snapshot-local/io/springfox/springfox-petstore/2.2.2-SNAPSHOT/maven-metadata.xml
http://oss.jfrog.org/artifactory/oss-snapshot-local/io/springfox/springfox-petstore/2.2.2-SNAPSHOT/springfox-petstore-2.2.2-SNAPSHOT.pom
http://oss.jfrog.org/artifactory/oss-snapshot-local/io/springfox/springfox-petstore/2.2.2-SNAPSHOT/springfox-petstore-2.2.2-SNAPSHOT.jar
Required by:
springfox-demos:spring-java-swagger:unspecified
I've tried a variety of combinations of build tasks but I can't seem to get Gradle to honor my request for using the local maven repo with a -SNAPSHOT artifact.
Here is the top-level build.gradle:
buildscript {
repositories {
mavenLocal()
jcenter()
}
dependencies {
classpath "com.github.adrianbk:gradle-jvmsrc-plugin:0.6.1"
classpath 'com.ofg:uptodate-gradle-plugin:1.6.0'
}
}
apply from: "$rootDir/gradle/dependencies.gradle"
subprojects {
apply plugin: 'com.github.adrianbk.jvmsrc'
jvmsrc {
packageName "springfoxdemo"
}
apply plugin: 'java'
apply plugin: 'com.ofg.uptodate'
repositories {
jcenter()
maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local/' }
}
sourceCompatibility = 1.7
targetCompatibility = 1.7
configurations.all {
//Dont cache snapshots
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
}
wrapper {
gradleVersion = "2.4"
}
So it appears that the top-level build.gradle can have more than one repositories{} block. I had correctly added the mavenLocal() to one, but missed the other. Once adding the mavenLocal() to the second block, all worked well.
I have two modules in Android Studio.
Main is the application and Sub is a library module. Sub is referred from Main with compile project(':Sub') in the gradle script. That works when run from Android Studio. But when run from command line, gradlew says:
Could not create plugin of type 'LibraryPlugin'.
Caused by: java.lang.NoClassDefFoundError: org/gradle/api/artifacts/result/ResolvedComponentResult
This is the important parts in Main's build.gradle file:
apply plugin: 'android'
buildscript {
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
task wrapper(type: org.gradle.api.tasks.wrapper.Wrapper) {
gradleVersion = '1.11'
}
android {
buildToolsVersion '19.0.3'
}
dependencies {
compile 'com.android.support:support-v4:13.0.+'
compile project (':Sub')
}
The Sub gradle file is more or less identical, but has
apply plugin: 'android-library'
instead of 'android'
I have tried with gradle 1.9 and 1.10, but same result.
Anyone knows how to solve this?
Verify that your dependencies contains classpath 'com.android.tools.build:gradle:0.9.+' in each gradle.build file (or just put it in the base one and not declare it in the others). Update gradle/wrapper/gradle-wrapper.properties to point to gradle 1.11:
distributionUrl=http\://services.gradle.org/distributions/gradle-1.11-all.zip
If you have any other instances of the gradle-wrapper (such as if you originally made the library project on its own and later added an example app), verify that all instances are updated to point to the same version (each gradle-wrapper.properties file).
I have an empty Android project, and I want to add some library to it.
For example, Picasso.
So I've edited 'app/build.gradle' and added follow line to the dependencies block:
dependencies {
compile 'com.android.support:support-v4:19.0.0'
compile 'com.android.support:appcompat-v7:19.0.0'
compile 'com.squareup.picasso:picasso:2.2.0'
compile fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
}
Then I rebuilt my project. So now I want to use it, but Picasso classes is unavailable for Android Studio.
What did I miss?
After editing the build.gradle file by hand, you need to click the "Sync Project with Gradle Files" button before the IDE will pick up new dependencies.
I did it this way:
In Top build.gradle (Project: YourProject) I added:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.h2database:h2:1.4.187'
//NOTE: you can get the latest version in http://mvnrepository.com/artifact/com.h2database/h2
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
NOTE: I added this along with the predefined jcenter() repositories.
And then for my app/build.gradle file I added whichever library or dependency I needed on:
dependencies {
....//Add dependency here
}
I hope this can help!