Groovy CompilerScript and Intellij - gradle

I have a Gradle multi-module project where each module have a CompilerScript.groovy similar to this:
import groovy.transform.CompileStatic
withConfig(configuration) {
ast(CompileStatic)
imports {
star('jakarta.inject')
}
}
Each Gradle module is configured to read this file and it works as expected.
compileGroovy.groovyOptions.configurationScript = file('src/compiler/groovy/CompilerConfig.groovy')
But IntelliJ show errors inside editor about missing import statements. There is a way
to configure the Idea Gradle Plugin with the same configs as Gradle? Thanks.

Related

How to setup an existing Gradle project on STS with springframework libraries?

I have downloaded a gradle spring boot project from GitHub. I'm using STS (Spring Tool Suite). I have downloaded the Gradle plugin from marketplace. and I have tried options such as manually adding the eclipse and eclipse-wtp plugins and the classpath like below to the build.gradle file:
plugins {
id 'eclipse-wtp'
id 'org.springframework.boot' version '2.4.13'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'jacoco'
id "org.unbroken-dome.test-sets" version "4.0.0"
}
eclipse {
classpath {
file {
whenMerged {
def source = entries.find { it.path == 'src/main/java' }
source.entryAttributes['ignore_optional_problems'] = 'true'
}
}
}
}
However, I cannot build the project and I get error messages on imports related to annotations such as below:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Can you please guide me in how to build this gradle project with STS and resolve the errors?
p.s. I have downloaded latest gradle too. if I navigate to the project folder on CMD and run it with "gradle bootRun", tomcat comes up properly. but I don't know how to set it up on STS!
If you download and use the latest Spring Tools 4 for Eclipse distribution, there is usually nothing you need to add in order to use Gradle, the Buildship tooling comes pre-installed.
With that, you can select "Import -> Gradle / Existing Gradle Project", select your Gradle project, and go from there.
If your Gradle build file works in general, this should work just fine in the IDE, including resolving all the necessary dependencies that you have defined in your build file. So if your Gradle build runs fine from the command line, the import into the IDE should work as well.

Gradle project that the dependencies cannot be imported or even not be existed at all

I create a simple Gradle project, then add some dependencies for test, it can build the project normally, but CANNOT use the APIs in class, the gradle.build file:
plugins {
id('java')
id('war')
}
repositories { mavenLocal() mavenCentral() }
// ... other code
dependencies {
providedCompile 'javax.servlet:servlet-api:2.5'
runtime 'javax.servlet:jstl:1.1.2'
}
By the way, it's an old-test Java project, in class file I cannot import the packages like this: import javax.servlet.*;, how to solve? Thanks for any help!
It's so weird, it works today, when I open the project, the all needed libraries appear in the External Libraries, and I can import the classes now.
Why? I already restarted the IDEA and project several times yesterday, but that did not work at all, however, it's automatically solved today.

Can not import class after added in build.gradle.kts

I generated spring boot kotlin project with spring initializer in intellij.
And added lib dependency in build.gradle.kts after auto generated.
Implementation "org.springframework.boot:spring-boot-starter-security".
I tried import WebSecurityConfigureAdapter, but intellij is 'No suggestions'.
I could not import class after added in build.gradle.kts.
And i tried Run Configurations build, buildDependents, clean, etc...
How can I fix?
Refer to Kotlin Gradle DSL specification. Use
implementation("org.springframework.boot:spring-boot-starter-security:2.2.6.RELEASE")
to declare dependencies.

How to define my own configuration in gradle plugin?

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

call java function in gradle script

I have a java class which does some kind of functionality, one of these function returns something that I need to use it into gradle script to set the project properties.
I had achieved it by creating an artifact of project and used that artifact by adding it into classpath, that gave me accessibility of that class and function.
buildscript {
repositories {
jcenter()
maven{
url 'http:localhost:8081/artifactory/temp'
}
}
dependencies {
classpath "utility:sampleutility:1.0"
}
}
import com.polsys.utility.MyUtil
dependencies {
compile 'org.slf4j:slf4j-api:1.7.13'
compile 'HRP:'+new MyUtil().callMe()+':1.0'
//callme function returns the name of artifact.
testCompile 'junit:junit:4.12'
}
Now, I had achieved it by the way as mentioned above that is by creating artifact, add that artifact into classpath, then import classes and use function. Is this any way by which I can call functions of current project? so I can merge that functionality which is available in the artifact into current project.
Simple way is to put your Java/Groovy code under buildSrc dir. Gradle will compile it and you'll be able to call this code from your buildscript. Check https://docs.gradle.org/current/userguide/custom_plugins.html and related docs.
To make your java code available to gradle script you need to have your java code under the directory hierarchy given below:
ProjectRootDirectory
buildSrc
src
main
groovy/java
YourPackages(in my case java packages and classes)
This is the path where gradle script looking for external plugins. Now you can import and access classes into gradle script(you will not end up with "unable to resolve class" error).

Resources