Gradle tasks are disabled in eclipse - 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.

Related

Where do I apply the gradle idea plugin?

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.

Gradle Eclipse not working correctly for subprojects

Gradle 2.4 and Eclipse Mars here. Here's my project directory structure:
myapp/ <-- root project
myapp-client/
src/main/groovy/*
build.gradle <-- just declares some client-specific deps
myapp-shared/
src/main/groovy/*
build.gradle <-- shared-specific deps
myapp-server/
src/main/groovy/*
build.gradle <-- server-specific deps and how to package server JAR
build.gradle
settings.gradle
Where myapp/build.gradle is:
allprojects {
apply plugin: 'groovy'
apply plugin: 'eclipse'
apply plugin: 'maven'
apply plugin: 'codenarc'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
repositories {
// ...etc.
}
dependencies {
// ...etc.
}
task wrapper(type: Wrapper) {
gradleVersion = '2.4'
}
}
And where myapp/settings.gradle is:
include ':myapp-shared'
include ':myapp-client'
include ':myapp-server'
When I do a ./gradlew eclipse I get messages indicating that the command was successful. I then open Eclipse to import myapp as a series of individual subprojects.
Expectation based on what I'm used to seeing, historically
In previous projects that contained subprojects, I would go to Import >> Existing Projects into Workspace, where I would see this dialog:
I would then click Browse and select the project root (in this particular case, myapp). Eclipse would then auto-detect the 3 subprojects and populate them as a checked list inside the Projects component in the UI above. I would then click "Finish" and Eclipse would load the 3 subprojects into my workspace, each showing as a separate project (as it should).
What's actually happening
Instead, when I click on myapp as the root project, Eclipse just populates it as a single project, almost as if it doesn't detect that I have subprojects at all:
When I click Finish it imports a single project into my workspace, that has source folders for each of the 3 subprojects. And while that's just an annoyance, the real problem is that the classpath seems to be totally jacked up. For instance, I can add a class to myapp-shared, and then include it in a class defined inside myapp-client, and Eclipse doesn't force me to add an import statement into the client class! It's like the whole project is sharing the same package/namespace.
Can any Gradle gurus spot a defect in my Gradle setup that could be the cause of this?
This was nasty, but I figured it out. Posting the solution here in case anybody else runs into this.
Instead of a single allprojects closure, you need allprojects and subprojects closures. Not sure what is "safe" to put in either closure, but I got this working by simply declaring my repositories inside of allprojects and then putting everything else inside subprojects.
The problem was that I had previously ran gradle eclipse on the root project (before I added in the subprojects closure), and so Gradle generated the typical artifacts (e.g. .projects, .classpath, .settings/*) in the parent/root project directory. So...
Delete Gradle-Eclipse artifacts out of the root directory. Delete the project from your Eclipse workspace (but not the disk!!!). I'd even go so far as to restart Eclipse to clear out any caches if they exist.
Run gradle clean for good measure. Probably not needed.
Run gradle eclipse again.
Re-import into Eclipse, and all will be well in your kingdom once again, my friend.

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.

Writing a buildSrc Gradle plugin that can be published

I'm writing a custom Gradle plugin for my company to assist with integration tests of our product. My team wants to have the plugin be built with and used in the main product build (like a 'buildSrc' plugin would), but also need the plugin to be published as an artifact for other teams to use in integration with our product.
If I try and include it as a standalone plugin in the settings.gradle file and then also include it in the buildscript as a dependency, it obviously does not work because the buildscript block is interpreted first.
I also tried running another build from within the buildscript like so:
buildscript {
def connection = GradleConnector.newConnector()
.forProjectDirectory(file("${project.projectDir}/theplugin"))
.connect()
try {
connection.newBuild()
.forTasks('clean', 'build', 'install')
.run()
} finally {
connection.close()
}
repositories {
mavenLocal()
...
}
dependencies {
classpath 'com.company.product.gradle.theplugin'
}
}
This causes the plugin to be built and placed in the local Maven repo, but then the initial Gradle build fails directly afterward because it can't resolve the newly built archive. If I run it again, it works. I don't understand this behavior.
I'm probably going down a rabbit hole with this approach. Is there a way to make this work and in a less 'hacky' way?
I discovered a hacky way to accomplish this: symlink the plugins to the buildSrc (on *nix at least).
project directory
project/
buildSrc/ -> gradle_plugins/
gradle_plugins/
pluginA/
pluginB/
...
build.gradle
settings.gradle
...
build.gradle
settings.gradle
project/settings.gradle
include 'gradle_plugins:pluginA'
include 'gradle_plugins:pluginB'
...
project/gradle_plugins/settings.gradle
include 'pluginA'
include 'pluginB'
...
project/gradle_plugins/build.gradle
...
rootProject.dependencies {
runtime project(path)
}
...
The way I'm solving this is the following:
Regular multi project build with buildSrc/myPlugin/.. Within my build process I call ./gradlew -b buildSrc/myPlugin/build.gradle uploadArchives (or whichever task you use to publish your maven artifact).
Due to the "official" hack of having to add the gradle plugin to the runtime dependencies of the root project this step would fail. So I surround it with a try catch. I feel that is not perfect but it seems to work.

Building library project in Android Studio that has been imported from Eclipse

I have an Android library project in Eclipse that I am trying to build with Android Studio so I can generate an .aar file for my users.
The project seems to have been imported cleanly into Android Studio using the "Import Project" option in the welcome screen.
How do I now build the module? The instructions on the dev site say that I need to change:
apply plugin: 'android'
to
apply plugin: 'android-studio'
However my build.gradle file doesn't have that line (I would have assumed that the importer would have added it(?)).
If I insert the line apply plugin: 'android-library', and try to 'Sync Project with Gradle Files', I get the error:
Gradle 'MyProject' project refresh failed
Error:C:\Users\Fred\AndroidStudioProjects\MyProject\src\main\AndroidManifest.xml
(The system cannot find the path specified)
The same happens if I try to make the project anyway.
Here's what my build.gradle looks like:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
allprojects {
repositories {
mavenCentral()
}
}
apply plugin: 'android-library'
My searches to find a solution have failed. Anyone know what is going on?
I'm using the latest Android Studio (0.5.8)
Don't put the apply plugin: 'android-library' statement in the top-level build file. You should find an apply plugin statement in your module-level build file, and you can modify it there as necessary.
Okay. After some head scratching and more searching, I resolved all my problems I think. For the record:
I think my first import broke somehow. Partly because of the problem highlighted by Scott Barta and possibly because my project directory had spaces in it? Not totally sure.
A later import went more smoothly and I was able to sync and build without errors. However, apart from the build output in the Gradle Console, there was no evidence anything had actually happned. The artifact (ie. the .aar file) doesn't show up in the UI.
After finding this question: How to export library to Jar in Android Studio? I realised that the aar file is in fact built. It is just hidden from you. You have to root around in the file system to find the .aar. It is in:
<Library module>/build/libs/
Something that the docs don't tell you anywhere. Sigh.
Hope this helps someone else..