gradle & Idea auto import: Project resolve error - intellij-idea

I create a simple gradle project:
build.gradle:
plugins {
id 'idea'
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
compile "org.apache.thrift:libthrift:0.9.3"
}
task taskTest {
println "Hello world"
}
I run the command gradle idea and was open project in the Idea IDE. Idea offered me an Import Gradle project, I allow her this with "Use auto-import option".
In "Message Gradle sync" window I see the next warnings:
Warning:root project 'test': Unable to resolve additional project configuration.
Details: groovy.lang.MissingPropertyException: No such property: id for class: org.gradle.api.internal.artifacts.DefaultResolvedArtifact
Possible solutions: file
Warning:root project 'test': Unable to resolve additional project configuration.
Details: groovy.lang.MissingPropertyException: No such property: id for class: org.gradle.api.internal.artifacts.DefaultResolvedArtifact
Possible solutions: file
What I doing wrong?

Not really sure, but it seems, that your your Gradle version does not yet support the plugins DSL. You can avoid using it, just remove this:
plugins {
id 'idea'
id 'java'
}
and apply this plugins this way:
apply plugin: 'java'
apply plugin: 'idea'
Or you can try to update you Gradle version.

I also face this problem. It may be due to using new gradle version with old IntelliJ IDE. I just upgrade IntelliJ with latest version and problem disappear.

Related

"No main manifest attribute" when creating Kotlin jar using IntelliJ IDEA

When creating a jar from my Kotlin code and running it, it says "No main manifest attribute".
When looking at the manifest.mf, it has this content:
Manifest-Version: 1.0
When looking at the file in the source, it has this content:
Manifest-Version: 1.0
Main-Class: MyMainClass
When manually copying the source manifest to the jar, it runs perfectly.
Screenshot of my artifact settings
I got this error with Gradle and Kotlin.
I had to add in my build.gradle.kts an explicit manifest attribute:
tasks.withType<Jar> {
manifest {
attributes["Main-Class"] = "com.example.MainKt"
}
}
From the gradle documentation, it's better to create a fatJar task to englobe all of the runtime dependencies in case you encounter java.lang.NoClassDefFoundError errors
If any of the dependent jars has a MANIFEST.MF file, it will override your custom one which defines the Main-Class.
In order to address this problem you should do the following:
Disable the alphabetical ordering
Change items ordering so that item which has META-INF/MANIFEST.MF file is the first in the list
Your custom MANIFEST.MF will be picked up by IntelliJ IDEA and displayed for the jar artifact.
See the related issue for more details.
You can also use Gradle or Maven to generate the fat jar instead.
1.Add the following task definition in the build script
tasks.jar {
manifest {
attributes["Main-Class"] = "MainKt"
}
configurations["compileClasspath"].forEach { file: File ->
from(zipTree(file.absoluteFile))
}
}
Then the jar tasks (Tasks | build | jar) again from the right hand sidebar.
For Spring boot apps:
What worked for me (gradle kotlin) in build.gradle.kts
add spring boots plugin &. apply dependency management
plugins {
id("org.springframework.boot") version "2.6.7"
}
apply(plugin = "io.spring.dependency-management")
set your main class
springBoot {
mainClass.set("com.example.Application")
}
Found this all by reading up on spring-boot docs found here

Gradle build settings in IntelliJ

What can I do to make the following Gradle project build succesfully?
I've build a new project. settings.gradle is:
rootProject.name = 'myproject'
build.gradle is:
group 'mygroup'
version '1.0-SNAPSHOT'
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.5
repositories {
mavenLocal()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
maven { url "https://mvnrepository.com/artifact/org.apache.commons/commons-lang3" }
}
dependencies {
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.6'
compile 'org.codehaus.groovy:groovy-all:2.3.11'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
If I use "Refresh all gradle projects" in the Gradle Tool Window, the Event Log says:
Unindexed remote maven repositories found. [Disable...]
The following repositories used in your gradle projects were not indexed yet:
https://plugins.gradle.org/m2
https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
Project Structure --> Problems tells me:
Library Gradle: org.apache.commons:commons-lang3:3.6 has broken
classes path:   /home/myhome/git/myproject/Could not resolve
org.apache.commons:commons-lang3:3.6.
Does anybody know a good instruction website for building gradle projects (other than Getting started with Gradle)? I've also re-setup the project based on these instructions, but I cannot resolve any dependencies either. (auto-import enabled and using a Gradle wrapper)
Do you have maven installed?
With regards to mavenLocal() Gradle goes and reads ~/.m2/settings.xml to find the info about where is your repo situated, when it can't find any info it is going to assume it's '~/.m2/repository'
Please update the question with the IntelliJ version you are using as versions from 2015, 2016 wasn't that good with Gradle integration yet, if you are using an outdated version, I recommend downloading latest and trying again.
You can use some kind of a workaround for this, in your build.gradle apply plugin: 'idea' and then on the command line do gradle cleanIdea idea - this will delete and recreate your idea files .iws .ipr .iml and recrete the classhpaths and your project should refresh in IntelliJ and work just fine
The problem disappeared after:
upgrading from IntelliJ 2016.3 to 2017.2
setting up another project from scratch
being asked to deactivate "Gradle offline mode" - and answering yes.
Obviously the problem was the "Gradle offline mode" which can be directly deactivated as described in this answer.

How can I create a Kotlin project that builds with Gradle?

I'm trying to create a new Kotlin project that builds with Gradle using IntelliJ IDEA (2016.2.5 on Ubuntu 16.04). When I do this I immediately get an error message.
Here's what I'm trying:
Select "Create New Project" from Welcome Screen.
Select "Gradle" from left hand pane, "Kotlin (Java)" from right. Click "Next".
Enter "hello-world" as ArtifactId. Click Next.
Ensure "Create separate module from source set" and "Use default Gradle wrapper" are
selected, nothing else is. Click "Next".
Use defaults for project name and location. Click "Finish".
I then immediately get this error:
Gradle 'hello-world' project refresh failed
Error: Could not find org.jetbrains.kotlin:kotlin-gradle-plugin:1.1-M02-12.
Searched in the following locations:
https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.1-M02-12/kotlin-gradle-plugin-1.1-M02-12.pom
https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.1-M02-12/kotlin-gradle-plugin-1.1-M02-12.jar
Required by:
:hello-world:unspecified
The generated build.gradle looks like this:
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.1-M02-12'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
How can I create a Kotlin project that builds with Gradle correctly?
In your build.gradle, change ext.kotlin_version to be:
ext.kotlin_version = '1.1-M02'
It's a minor bug that the IDE plugin puts its own version into build scripts, not the Kotlin version.
And also add the 1.1 EAP repository to the repositories in both buildscript and the root scope:
repositories {
// ...
maven { url "http://dl.bintray.com/kotlin/kotlin-eap-1.1" }
}
Kotlin artifacts related to EAP versions are not put into Maven Central like those of public releases, and this repository is not added automatically into the generated build script.
Then refresh the Gradle project, and the build should pass.
Feel free to check with this build.gradle file.

Gradle not downloading dependencies in intelliJ idea

I am building a Java project in Intellij Idea IDE.
Following is my build.gradle file:
group 'fyp_group09'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.5
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'log4j:log4j:1.2.17'
}
I use a proxy connection provided by school and have done the required proxy setup in the IDE.
The log4j dependencies are not getting downloaded. I've refreshed the gradle project many times, but it does not download the dependencies and does not even give any error. How can I solve this problem?
The following trick as given here, worked well for me:
Un-check "Offline work" in File>Settings>Gradle>Global Gradle Settings
We had this, worked after doing File>Invalidate Caches / Restart...

Using autosuggest on dependency jars that are downloaded by Gradle

I'm pretty new to Gradle and IntelliJ and I'm trying to understand how it works [cos that's what my project uses]. I created a simple java project in Intellij IDEA with a HelloWorld class and manually added the gradle.build file along with the necessary dependencies as follows:
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'
mainClassName = 'main.java.test.HelloWorld'
dependencies {
compile 'joda-time:joda-time:2.2'
}
repositories {
mavenLocal()
mavenCentral()
}
jar {
baseName = 'gradle-test'
manifest {
attributes 'Main-Class': 'main.java.test.HelloWorld'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.2'
}
While I specified the joda-time dependency, my IntelliJ project is unaware of this library and therefore I can't use of any of the autosuggest features of IntelliJ for the joda-time library.
Could someone give me information on how I can get IntelliJ to automatically start recognizing the downloaded jars and allow me to use the auto-suggest features?
In other words,
where are the dependency jars downloaded ?
How can I have these jars to automatically be included in the
class path of the project ? [Is it ok to do so ? If not, what
would be your recommendation ?]
Thanks !
I used Peter's suggestion to run gradle idea and imported the project into IntelliJ and I was able to start using the auto-suggestion features in IntelliJ.
Thanks