Automatically download sources in Gradle project - intellij-idea

In Maven projects, there is an option to automatically download sources (and javadoc) for all libraries. For Gradle project I found no option, just to open a class and click "Search in internet". This is very annoying if you have a lot of libraries. Is there any way to automatically attach sources from the internet (maven repo)?

I know that this is an old topic, but in case someone reaches here and does not want to gradle idea, you can also use a plugin and then it will resolve sources automatically.
e.g. both idea and eclipse compatible build.gradle
plugins {
id "java"
id "idea"
id "eclipse"
}
idea {
module {
downloadSources = true
}
}
eclipse {
classpath {
downloadSources = true
}
}
dependencies {
compile group: 'commons-io', name: 'commons-io', version: '2.6'
}

Related

Apply local jar-plugin without using maven

I'd like to load my custom plugin from a local jar. The jar file compiles fine and when I check it, the manifest and the plugin class are there.
gradlePlugin {
plugins {
create("asdf") { // <-- I really call it "asdf" in the kts script
id = "asdf"
implementationClass = "pluginTest.TestPlugin"
version = "1.4.0"
}
}
}
The plugin doesn't do anything useful yet as it should be a proof-of-concept to make sure it actually works at all:
class TestPlugin : Plugin<Project> {
override fun apply(project: Project) {
println("Hallo TestPlugin!")
}
}
I then try to use it like this in another project:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath(files("..\\..\\path\\to\\pluginTest.jar"))
}
}
plugins {
id("asdf") version "1.4.0"
}
but it keeps telling me that:
Plugin [id: 'asdf', version: '1.4.0'] was not found in any of the following sources:
What am I missing here? I use Gradle v6.5.
When you have the plugin jar on the classpath, you can't have a version number in the plugin application. I guess this is because you can't have multiple jars with different versions on the classpath in the first place, so specifying a version here doesn't make any sense (except perhaps to validate that you are using the correct one). This won't fix the problem, but it is a start.
To be honest, I don't know why your approach still won't work. The buildscript block is supposed to set up dependencies for that particular script, and that should make the plugin visible to it. It doesn't for some reason.
Perhaps this is a bug or perhaps this is just an undocumented limitation on the use of the plugin {} block. Maybe you could ask over at the Gradle forums or create an issue for it. However, there are workarounds that don't involve publishing to a (local) Maven repository, which I agree can be a bit annoying.
If you use "apply from" instead of "plugins {}", it works. For some reason, the former can see the buildscript classpath whereas the latter can't:
// build.gradle (Groovy DSL)
buildscript {
dependencies {
classpath(files("..\\..\\path\\to\\pluginTest.jar"))
}
}
apply from: "asdf"
Alternatively, move the buildscript plugin from the build.gradle file to the settings.gradle file. This makes is available to the entire build classpath and will make it work with the plugin block:
// settings.gradle (Groovy DSL):
buildscript {
dependencies {
classpath(files("..\\..\\path\\to\\pluginTest.jar"))
}
}
// build.gradle (Groovy DSL)
plugins {
id("asdf")
}
Lastly, just in case you haven't considered it already, you may be able to add the plugin as a composite build. This will create a source dependency to the plugin and has the advantage that transitive dependencies will be carried over (the ones you put in the plugin's own dependency block) and that it will be built automatically if not up-to-date. I use this approach for integration testing my plugins and also sometimes to apply them to my other real projects to test them in a bigger setting before publishing new versions.
Do that with either:
// settings.gradle (Groovy DSL):
includeBuild("..\\..\\path\\to\\plugin")
// build.gradle (Groovy DSL):
plugins {
id("asdf")
}
Or without hard-coding it in the build (so you can dynamically switch between local and published versions):
// build.gradle (Groovy DSL):
plugins {
id("asdf") version "1.4.0" // Version is optional (will be ignored when the command line switch below)
}
// Run with:
./gradlew --include-build "..\\..\\path\\to\\plugin" build
With #BjørnVester's answer I figured it out!
You need to put the buildscript in settings.gradle.kts as it doesn't get executed in the build.gradle.kts even when placed before plugins.
buildscript {
repositories {
flatDir {
dirs("..\\reusable-kotlin\\build\\libs") // <-- folder with jars
}
}
dependencies {
classpath("com.hedev.kotlin:reusable-kotlin:1.4.0")
}
}
But there's a catch! You must use the file-name of the jar in the classpath's name identifier that goes like this:
group:file-name:version
The file gradle will look for will be file-name-version.jar or file-name.jar which you'll see in the error message if you make a mistake (I added the _ on purpose to trigger the error):
Could not resolve all artifacts for configuration 'classpath'.
Could not find com.hedev.kotlin:reusable-kotlin_:1.4.0. Searched in the following locations:
- file:/C:/some/path/reusable-kotlin/build/libs/reusable-kotlin_-1.4.0.jar
- file:/C:/some/path/reusable-kotlin/build/libs/reusable-kotlin_.jar
In order for this to work I also had to add the group property to the plugin itself:
gradlePlugin {
plugins {
create("asdf") {
id = "asdf"
implementationClass = "com.hedev.kotlin.gradle.TestPlugin"
version = "1.4.0"
group = "com.hedev.kotlin"
}
}
}
Finally you can apply it in build.gradle.kts with (no version here):
plugins {
id("asdf")
}

Build Intellij plugin in IDEA 2019.1 & 2020.3

Building for IDEA 2019.1 works like a charm! I thought that building for 2020.3 would be just a matter of pointing to 2020.3 installation folder and that's it, but it is not being even close to it.
That's my gradle.build
group 'com.test.plugin'
version '1.0-SNAPSHOT'
buildscript {
repositories {
maven {
url "https://mydomain/repository/public-maven/"
}
}
dependencies {
classpath group: 'org.jetbrains.intellij.plugins', name: 'gradle-intellij-plugin', version: '0.6.5'
}
}
apply plugin: 'java'
apply plugin: 'org.jetbrains.intellij'
intellij {
localPath 'C:/Program Files/JetBrains/IntelliJ IDEA 2019.1.3'
}
sourceCompatibility = 1.8
repositories {
maven {
url "https://mydomain/repository/public-maven/"
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
runtime group: 'com.google.guava', name: 'guava', version: '23.0'
runtime group: 'org.apache.commons', name: 'commons-lang3', version: '3.11'
}
It is important to mention that since I'm working behind a restricted company proxy I can't just set the Intellij version in order to get the necessary distribution files to the build (Intellij.localPath)
Building it on IDEA 2019.1, JDK 1.8 works fine. In order to build the same code for a IDEA 2020.3 I just replaced the Intellij distribution path:
intellij {
//localPath 'C:/Program Files/JetBrains/IntelliJ IDEA 2019.1.3'
localPath 'C:/Dev/apps/ideaIU-2020.3'
}
Trying to build it now immediately throws it:
error: cannot access AnAction
bad class file: C:\Dev\apps\ideaIU-2020.3\lib\platform-api.jar(com/intellij/openapi/actionSystem/AnAction.class)
class file has wrong version 55.0, should be 52.0
Please remove or make sure it appears in the correct subdirectory of the classpath.
What I understand form it is that AnAction class was built using Java 11. So, I replaced the project JDK to use also JDK11 and from that moment I started facing compilation errors, like com.intellij.psi.PsiJavaFile cannot be found.
I might be missing some conceptual point here.
it turns out there was a missing plugin dependency.
this line was not being effective to resolve such dependency. In order to fix that I had to remove apply plugin: 'java' and set intellij.plugin
intellij {
localPath 'C:/Dev/apps/ideaIU-2020.3'
plugin = ['com.intellij.java']
}

IntelliJ Idea 16 CE gradle issues

In my gradle file I am trying to use the following to solve another issue. This is my gradle file:
group 'com.winapp'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'src/main/java/libs', include: ['*.jar'])
compile 'com.intellij:forms_rt:6.0.5'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
task copyDependenciesToTarget(type: Copy) {
println 'Copying dependencies to target...'
configurations.compile.collect().each { compileDependency ->
copy {
with from (compileDependency.getPath()) {
include '*'
}
into 'target/libs/libs'
}
}
}
build.dependsOn(copyDependenciesToTarget)
jar {
manifest.attributes(
"Main-Class": "Main",
"Class-Path": configurations.compile.collect { 'libs/' + it.getName()}.join(' ')
)
}
The problem are is this:
configurations.compile.collect().each { compileDependency ->
copy {
with from (compileDependency.getPath()) {
include '*'
}
into 'target/libs/libs'
}
}
When I run the application I get this Exception:
org.gradle.api.internal.file.copy.CopySpecWrapper_Decorated cannot be cast to org.gradle.api.internal.file.copy.CopySpecInternal
Main problem is, I have no idea how to fix this error, I simply want my project to create a JAR file that works with JDBC, using this gradle code seems to be a solution for that issue, but now I have run into another problem, yet again.
Please let me know if you require any additional information and thank you in advance. Literally, I cant even. This problem.
EDIT
As stated in the comments. My project runs fine when I run it through the IDE. There is an issue when I create a JAR file using gradle. The bare gradle file that IntelliJ created when I started the gradle project follows (Added the jar config so my Main class would be picked up):
group 'com.winapp'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'src/main/java/libs', include: ['*.jar'])
compile 'com.intellij:forms_rt:6.0.5'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
jar {
manifest.attributes(
"Main-Class": "Main",
"Class-Path": configurations.compile.collect { 'libs/' + it.getName()}.join(' ')
)
}
When I execute my app from terminal after the JAR has been created (keeping in mind there are no issues when hitting run in the IDE) with java -jar myAppName.jar:
java.lang.ClassNotFoundException: org.sqlite.JDBC
NOTE: Usually I get a full stack running a java app, but in this case the above is the only output.
I am using this JAR for my sqlite needs:
sqlite-jdbc-3.15.1
As a test I commented out the Sqlite usage in my application and the JAR file worked fine. My GUI was displayed and everything went as expected (all things considered). The JAR stops working when I add the sqlite JAR file usage (code). Added it the same as my other libs (like retrofit), so this seems to be quite a strange issue.
Please let me know if I have explained the issue correctly?

How to add dependency sources to to IntelliJ

I have an IntelliJ project with a gradle build file that includes several projects from a maven central repository. One such dependency is Geb.
When I navigate my classes, I sometimes come across a Geb class that looks interesting. I select "Go to declaration" and get a sad "Cannot find declaration to go to".
Obviously this is because IntelliJ has not loaded the Geb source files. But how do I get it to do that without including Geb as a source in my project? I DO NOT want Geb to be compiled into my project from source because I'm already including it as a dependency in my gradle build file.
Adding it as a module dependency does not work. This is like adding more sources.
I suppose I can grab the repo and build the jars and then include those. Is that really necessary?
Adding the IDEA plugin to the gradle file doesn't work.
Relevant part of the gradle script:
apply plugin: 'groovy'
apply plugin: 'idea'
dependencies {
// need to depend on geb-spock
testCompile "org.gebish:geb-spock:0.13.1"
testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
testCompile "org.apache.commons:commons-lang3:3.4"
testCompile "io.github.bonigarcia:webdrivermanager:1.5.0"
testRuntime "org.seleniumhq.selenium:selenium-support:2.53.1"
}
idea {
module {
downloadJavadoc = true // defaults to false
downloadSources = true
}
}
This is a complete build script that downloads all the dependencies with sources:
apply plugin: 'java'
apply plugin: 'idea'
idea {
module {
downloadJavadoc = true // defaults to false
downloadSources = true
}
}
repositories {
mavenCentral()
}
dependencies {
// need to depend on geb-spock
testCompile "org.gebish:geb-spock:0.13.1"
testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
testCompile "org.apache.commons:commons-lang3:3.4"
testCompile "io.github.bonigarcia:webdrivermanager:1.5.0"
testRuntime "org.seleniumhq.selenium:selenium-support:2.53.1"
}
task wrapper(type: Wrapper) {
gradleVersion = '3.3'
}
The dependency shows in the list:
And I am able to browse the source code, you can see the comments are there:
One of the possible explanation might be that in your repository list is a repo, such as mavenLocal or a caching Artifactory, that doesn't have the sources dependency.
The ordering of the repositories matters, so if mavenLocal is first and the sources are not available there, I believe they will not get downloaded. A possible fix would be to remove the dependency from mavenLocal and re-download it, change order of dependencies or if it is the parent script, exempt your subproject when adding the repositories:
configure(allprojects - project(':my-subproj')) {
repositories {
...
}
}
I don't think there is any way you can prevent that from the subproject's build script though. It must be done in the parent.

Excluding files in a Gradle multi-project in Intellij

I have a simple multi-project gradle build file that I use as a base for an Intellij project, and I want to exclude some files from the build, so I use 'excludes' on the compile task (which I have just copy-pasted), as shown in the listing.
This seems to be working from the command line, but it doesn't work from within Intellij i.e. excluded files still get included in the compilation.
Any suggestions? (Gradle 2.10, Intellij 15.0.3)
subprojects {
apply plugin: 'groovy'
apply plugin: 'application'
apply plugin: 'idea'
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.5'
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
}
}
project(':projfoo') {
group = 'com.bshishani.projfoo'
}
project(':projbar') {
group = 'com.bshishani.projbar'
dependencies {
compile project(':projfoo')
testCompile project(':projfoo')
}
compileGroovy {
excludes = ['**/aaa.groovy', '**/bbb.groovy']
}
}
You can exclude directories programmatically from IntelliJ project by configuring the idea plugin:
For example:
idea {
module {
excludeDirs += [file("myproject/dir")]
}
}
I'm not sure how to exclude files (or globs) from compilation in IntelliJ. If this isn't possible, it may be preferable to move all of the "special" sources to their own directory.
More information about IntelliJ Module config with Gradle
To mark a folder in the source as excluded from the IDEA complier you must first right click a folder in the projects view.
Right click folder > Mark directory as > Excluded