Where do I apply the gradle idea plugin? - plugins

The documentation says:
apply plugin: 'idea'
This is easy but not very useful. It does not tell me where to apply the plug-in, for example. allProjects, perhaps? subprojects? Are there any places where it should not be applied?

Typically all plugins are applied just after buildscript section at the very top of build.gradle script, especially in single module projects.
In multimodule projects you can apply the plugin in both allprojects and subprojects. It just depends if this plugin will be required in all the projects. If not and it's applied - nothing bad happens.

Related

Gradle tasks are disabled in eclipse

I'm trying to create a .jar file for my eclipse project. Through export option, I am able to create .jar, but I want to use Gradle build task to do so. Problem is that, all tasks are disabled for this particular project only, as showing in the following image...
For another project, these Gradle tasks are working fine. See its build.gradle file...
apply plugin : 'eclipse'
apply plugin: 'java-library'
repositories {
jcenter()
mavenCentral()
}
dependencies {
compile group:"org.apache.pdfbox", name:"pdfbox", version:"2.0.6"
compile group:"org.apache.pdfbox", name:"pdfbox-tools", version:"2.0.6"
compile 'com.google.code.gson:gson:2.8.2'
compile 'org.json:json:20180130'
api 'org.apache.commons:commons-math3:3.6.1'
implementation 'com.google.guava:guava:21.0'
testImplementation 'junit:junit:4.12'
}
It's settings.gradle file contains a single line that is...
rootProject.name = 'schedule-extractor'
Can anybody suggest what I am missing here?
Have you check this buildship's issue:
https://github.com/eclipse/buildship/issues/511
The following comment resolved it for me (I was having the same problem) :
https://github.com/eclipse/buildship/issues/511#issuecomment-367392031
For those who had a similar problem in include builds - Check the file
.setting/org.eclipse.buildship.core.prefs whether the
connection.project.dir property is empty. If not, make it empty. In
my case the grayed tasks become green again.
In my case, It happened when I was using composite build concept of Gradle. According to this approach, One build could be dependent on another build. In this case, the projects may behave like this.
For example, have a look on my composite build setup in settings.gradle...
rootProject.name = 'BackendRESTServices'
includeBuild ('../Algorithms')
includeBuild ('../Utilities')
Now I won't be able to build Algorithms & Utilities projects from Gradle Tasks tab as shown in the attached image of question.

Where can I find gradle dependencies?

I'm trying to learn Gradle in Eclipse.
Is there any good integration where you can search for a Jar to include in the project?
Right now it's a hit-and-miss operation from my side.
Example:
I want to add the CLI library from Apache Commons. Also the Codec library.
I have added both jcenter() and mavenCentral like this:
repositories {
jcenter()
mavenCentral()
}
and I have tried this (and variations) in the dependencies section:
compile 'org.apache.commons:cli:1.2'
compile 'org.apache.commons:codec:1.10'
but all I get is
Could not resolve: org.apache.commons:cli:1.2
Could not resolve: org.apache.commons:codec:1.10
When searching in search.maven.org, if I search on org.apache.commons I get 111 pages of hits... I haven't found the time to step thru them all.
When searching for commons-cli, it finds a version from 2005... plus a library called
org.mod4j.org.apache.commons cli
No idea what the "mod4" means.
Is the conclusion that Apache commons doesnt exist in these repositories?
How do you do in these cases? How do you come up with the correct "compile"-specification?
Can I for example say "get latest version of this jar" ?
Would love to have a way to do:
gradle search apache-commons --only-latest-version
or something similar. Something like the wonderful GEM/BUNDLE commands in Ruby.
Thanks for all help
You can search the Maven Central repository to find specific versions of artifacts, but in some cases you have to know the exact name (artifactId) of the artifact. Unfortunately the apache artifacts are not consistent in their groupId or artifactId naming schemes. For example, Apache Commons CLI is commons-cli:commons-cli.
Here is the latest version of that artifact. There is even a convenient panel that shows the exact line to use based on your dependency tool (maven, Gradle, Ivy, etc.)
The below build.gradle works. Have a look at the syntax for dependency declaration. Please read this documentation for syntax.
apply plugin: 'java'
repositories {
jcenter()
mavenCentral()
}
dependencies {
compile group: 'commons-cli', name: 'commons-cli', version: '1.2'
compile group: 'commons-codec', name: 'commons-codec', version: '1.10'
testCompile 'junit:junit:4.12'
}
And as for getting the 'latest' version of a library in gradle, I am afraid thats not possible currently like in maven.
Is there any good integration where you can search for a Jar to include in the project?
As far as I know Gradle itself has no specific support for this.
How do you do in these cases? How do you come up with the correct "compile"-specification?
Before searching in Maven repositories for the desired dependency, best is to consult the project's homepage first. commons-cli for example has a separate Dependency Information section and lists the required information for all kinds of build tools. For Gradle/Grails and the latest SNAPSHOT version this is
compile 'commons-cli:commons-cli:1.4-SNAPSHOT'
The list of valid versions is compiled in the Changes Reports section.
Can I for example say "get latest version of this jar"?
There is a plugin called gradle-versions-plugin that can list updates of defined dependencies. Of course, this requires that you have already defined your dependencies correctly.

View Gradle dependency tree in Eclipse

Is it possible to visualize the dependency tree from inside of Eclipse (e.g. the output of gradle dependencies)? Expanding the Gradle Dependencies tree in Eclipse is a flat view and does not show dependencies for other projects (e.g. if I have a dependency compile project(':project2'), none of project2's dependencies are shown).
Based on this it looks like a tree view is not supported?
Basically I'm looking for the equivalent of this in the maven plugin:
At the time of writing, neither Spring Eclipse Integration Gradle nor Buildship provide a Dependency Hierarchy view we know from m2e.
I don't know when this has been implemented but you can do a gradle dependencies either on command-line or via Buildship Gradle Tasks view within Eclipse. This prints a nice dependency tree of your project's dependencies to the console.
Use gradle 'project-report' plugin to generate report in HTML format.
https://docs.gradle.org/current/userguide/project_report_plugin.html
Add plugin in your build.gradle file:
apply plugin: 'project-report'
And generate report using:
> gradle htmlDependencyReport
For me, it was simply one command
in build.gradle add plugin
apply plugin: 'project-report'
and then go to cmd and run following command
./gradlew htmlDependencyReport
This give me a HTML report WOW Html report 💕
Or if you want the report in a txt file, to make search easy use following command
gradlew dependencyReport
That's all my lord.

Gradle dependency sources not appearing in Eclipse for the War Plugin

I am working on a Java web project that uses Gradle (version 2.1) as the build dependency tool. I use Eclipse Luna as my IDE. My OS is Mac 10.9 (Mavericks).
This is my build.gradle file (very basic and stripped down for ease of illustration):
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
sourceCompatibility = 1.8
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.google.inject', name: 'guice', version: '3.0'
}
So just one dependency, Guice. I will generate my Eclipse classpath and project files using gradle cleanEclipse eclipse. Then I will import an existing project into my workspace. I like being able to view the source code of my dependencies in my Eclipse projects, so I will open a Guice class, such as com.google.inject.Injector, using CMD-SHIFT-T. And voila, the source code of that file will appear.
But I working on a web project, so I need to be able to build a WAR file instead of a JAR file. Therefore, I am going to apply the Gradle War Plugin by replacing apply plugin: 'java' with apply plugin: 'war'. Then I rerun gradle cleanEclipse eclipse and reopen my project.
Now, instead of seeing the source code when I open up Injector, I will see the bytecode viewer with the Attach Source button. Anyone have any ideas whether it's something I'm forgetting in my build.gradle file or if it could be a bug in Gradle/Eclipse?
Note that I am not using the Gradle Integration for Eclipse Plugin because I wish to pinpoint the root cause of this issue without adding an extra layer of complexity to it. I have also checked other SOF questions about Attach Source issue with Gradle and Eclispe, such as how to tell gradle to download all the source jars and Why is Eclipse not attaching 3rd party libs source files to a WTP-faceted Gradle project?, but to no avail.

Can JetGradle Be Used In IntelliJ 12 Alongside Gradle Generated Project Files That Have Scala Facets?

I've been trying to make the switch to IntelliJ and Gradle for the Java/Scala projects at my company. From the command line Gradle has been great but I've run into a problem when using Gradle with IntelliJ and projects with Scala facets.
I've tried importing projects in both ways and the only one I've been able to get Scala facets working is to run:
gradle idea
using the Gradle IDEA plugin. After that all I need to do it open the ipr file for the project and the projects work fine.
However, there are some Gradle tasks that I would like to run from within IntelliJ for various things (code generation being the biggest one). I would like to be able to use JetGradle to run those tasks. The problem is when I link the Gradle project to JetGradle, the dependencies in each project break the Scala facet's compiler settings.
It's a multi-module project. Here is the build.gradle for the parent:
allprojects {
apply plugin: 'idea'
version = '1.0'
group = 'company-x'
repositories {
mavenCentral()
}
}
And here's the build.gradle for the scala modules:
apply plugin: 'scala'
dependencies {
compile 'org.scala-lang:scala-library:2.10.1',
'org.scala-lang:scala-reflect:2.10.1',
'org.scala-lang:scala-compiler:2.10.1',
'org.slf4j:slf4j-log4j12:1.7.2',
'com.typesafe.akka:akka-actor_2.10:2.1.4'
testCompile 'junit:junit:4.8.1',
'org.scalatest:scalatest:1.2'
}
Before linking the project to JetGradle the Scala facet is setup with a library called 'scala-compiler-2.10.1' that contains the Scala compiler, library, and reflect jars and src. After linking the project, the project libraries are replaced with all of the Gradle project dependencies. The Scala facet then gives the error:
Compiler library: no scala-library*.jar found
When I look at the libraries again, there is still a library called 'scala-compiler-2.10.1' but it's only the compiler. The library and reflect jars are now separated out.
Has anyone out there figured out how to get the Scala facet and JetGradle to play nicely with one another?
From this IntelliJ blog (Better Gradle Support in IntelliJ IDEA 12.1) it seems JetGradle is not ready to support Scala.
I'm waiting for IntelliJ 13.x and stick to Maven for the time being.