How to setup Findbugs for a Java 8 project with gradle? - gradle

I have a Java 8 project build using gradle 1.12 which works perfect. Now I want to use FindBugs and as I use Java 8 I have to use FindBugs 3. But the build hangs in findbugsMain:
:my-module:compileJava UP-TO-DATE
:my-module:processResources UP-TO-DATE
:my-module:classes UP-TO-DATE
> Building 6% > :my-module:findbugsMain
The resulting build.gradle contains the following:
apply plugin: 'java'
apply plugin: 'findbugs'
findbugs.toolVersion = '3.0.0'
dependencies {
compile 'com.google.code.findbugs:annotations:3.0.0'
…
}
Any Idea why the build is getting hanged? What should I do?

Ok, after all it seems that findbugs 3 simply does not work with gradle 1.x!
After switching to gradle 2.1 all works fine.
Thanks to Peter Niederwieser!

Related

Getting gradle error "Could not get unknown property 'srcDirs' for source set 'groovy'" after upgrading to 5.5.1

I have a multi-project build and I'm including the groovy plugin in the master build.gradle like this:
plugins {
id 'groovy'
}
However, one of my subprojects which has groovy sources is doing this:
sourceSets {
main {
java.srcDirs = []
groovy.srcDirs += ["src/main/java", "src/main/groovy"]
}
}
And I get an error:
> Could not get unknown property 'srcDirs' for source set 'groovy' of type org.gradle.api.internal.tasks.DefaultSourceSet.
I'm in the process of upgrading my build scripts to work under gradle v5.5.1 rather than v2.13. This was working previously where I had this in the subproject build file:
apply plugin: 'groovy'
But after upgrading to 5.5.1 from 2.13 it started giving me this:
* What went wrong:
A problem occurred evaluating project ':mysubproj'.
> Failed to apply plugin [class 'org.gradle.api.plugins.GroovyBasePlugin']
> Cannot change attributes of configuration ':mysubproj:apiElements' after it has been resolved
Which is why I thought I should declare the plugin in the master build script.
Shouldn't my plugin reference in the master build script make the subproject aware of the groovy extensions? If not, what's the correct way? Thank you!
The issue is the Groovy plugin isn't being applied to the subproject, you will want to apply it appropriately.
allprojects {
apply plugin: 'groovy'
}
I know in the past the new plugin style didn't work for subprojects, that may have been resolved and if so - use it.
As for the exception, when you seem to be doing the above, I'd need the buildscripts to advise you :)

gradle build fails cannot find sonar plugin "sonar-runner"

My gradle project cannot build. It fails with Plugin with id 'sonar-runner' not found.
Here's my build.gradle
subprojects {
apply plugin: 'sonar-runner'
}
I've refreshed my Gradle project. Then I've deleted my ~/.gradle/caches folder, but didn't work. Then I deleted my entire ~/.gradle folder, still the same error.
Any ideas please?
It's funny that gradle build doesn't run and gives me that exception, but bash gradle build runs fine, can someone clarify the difference in between? Thanks. My setup is on Mac OSX 10.11.6.
From https://docs.sonarqube.org/display/SONARQUBE51/Analyzing+with+Gradle:
Users of Gradle Sonar or Sonar Runner Plugins ? The existing Gradle
Sonar Runner and Gradle Sonar plugins, which are part of the Gradle
distribution, should no longer be used (See this official note from
the Gradleware team). The use of the Gradle SonarQube plugin is
recommended instead.
You have to use it as described at https://plugins.gradle.org/plugin/org.sonarqube
Build script snippet for use in all Gradle versions:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.2.1"
}
}
apply plugin: "org.sonarqube"
Build script snippet for new, incubating, plugin mechanism introduced in Gradle 2.1:
plugins {
id "org.sonarqube" version "2.2.1"
}
Issue discussing this problem:
https://discuss.gradle.org/t/plugin-with-id-org-sonarqube-not-found/11588/10

Invalid classpath publish/ export dependency /ouat-contract. Project entries not supported

I am trying create an Gradle multi project similar to this structure
ouat-services
- ouat-contract
- ouat-servicesImpl (web project)
I followed the eclipse example and define my ouat-services settings.gradle as
include "ouat-contract", "ouat-servicesImpl"
In my ouat-servicesImpl build-gradle I define
dependencies {
compile project(':ouat-contract')
}
My problem starts when I try apply war plug-in in ouat-servicesImpl, I receive the following message in eclipse problem view:
Invalid classpath publish/ export dependency /ouat-contract. Project entries not supported
My ouat-services build.gradle
configure(subprojects) {
apply plugin: 'com.github.ben-manes.versions'
apply plugin: 'eclipse'
apply plugin: 'java'
version = '1.0'
sourceCompatibility = 1.8
targetCompatibility = 1.8
def defaultEncoding = 'UTF-8'
[compileJava, compileTestJava]*.options*.encoding = defaultEncoding
repositories {
jcenter()
mavenLocal()
mavenCentral()
}
jar {
manifest.attributes provider: 'Company'
}
}
configure(project(':ouat-servicesImpl')) {
apply plugin: 'checkstyle'
apply plugin: 'eclipse-wtp'
apply plugin: 'findbugs'
apply plugin: 'jacoco'
//apply plugin: 'jetty'
apply plugin: 'pmd'
apply plugin: 'war'
}
buildscript {
repositories {
jcenter()
mavenCentral()
mavenLocal()
}
dependencies {
classpath 'com.github.ben-manes:gradle-versions-plugin:0.10.1'
}
}
My ouat-servicesImpl build gradle was changed to:
dependencies {
compile project(':ouat-contract')
cxfArtifacts.each { artifact ->
compile "org.apache.cxf:$artifact:$cxfVersion"
}
springArtifacts.each { artifact ->
compile "org.springframework:$artifact:$springVersion"
}
testCompile "org.testng:testng:$testNGVersion"
testCompile "org.hamcrest:hamcrest-all:$hamcrestVersion"
testCompile "org.springframework:spring-test:$springVersion"
//WAR PLUGIN
providedCompile "javax.servlet:javax.servlet-api:$servletAPIVersion"
runtime "javax.servlet:jstl:$jstlVersion"
}
Is this an eclipse plug-in problem or I am doing something wrong?
Here's the magic steps I've discovered to make it work without messing with Project settings manually.
Run command: gradle cleanEclipse eclipse
as a result of this command Eclipse forgets that the project was supposed to have a gradle nature.
Add gradle nature back to the project by doing Configure -> Convert to Gradle Project.
as a result of this command the error reappears.
if incompatible plugin java version error appears then just delete .settings directory and refresh.
Run command: gradle cleanEclipseClasspath eclipseClasspath
this final step should get it fixed until the next time.
In my case, this was due to mixing "faceted" and non-faceted projects. The projects with the error had been converted to faceted form, and the project they referenced which it was complaining about had not been. You can configure the project to be faceted via use of the eclipse-wtp plugin, by adding this to your ouat-contract gradle file:
eclipse{
wtp{
facet{}
}
}
This will add facets for Java and a utility module when using the java and war plugins (see the EclipseWTPFacet documentation for more information on the defaults and manually adding facets if you aren't using the war plug-in). The utility module part is the key to avoid the error.
Note that within this block you can also access the facet file directly to perform manual XML manipulation if you need to do other things, like specify a particular Apache Tomcat Runtime or or similar
Once you make this change, you can use Eclipse to do Gradle -> Refresh All on ouat-contract within your workspace - once I did this, the error went away
I've also run into this problem long time ago. It really seems to be the problem related to the Eclipse plugin included in "Gradle IDE Pack" (as it works from the command line without problems).
My setup is probably way more complex than Yours (I'm including modules from one top-level gradle project into another top-level gradle project), but to overcome this specific error
Invalid classpath publish/ export dependency /my-project. Project entries not supported
... i excluded project dependency if some specific gradle property was missing:
if(project.hasProperty("myProjectRefAddedFromIDE")) {
println "NB! Build script started with property 'myProjectRefAddedFromIDE' - expecting that this project in IDE is configured to add the direct reference to my-project"
} else {
compile project(':my-project')
}
And to add the property "myProjectRefAddedFromIDE" only from IDE, i have configured eclipse plugin as follows:
Window -> Preferences -> Gradle -> Arguments -> Program arguments -> Use: ´-PmyProjectRefAddedFromIDE´
Just a warning: this will probably work for you, but there might be some other problem with Your setup, as for simple multi-module project (that doesn't include modules form another multi-module project) I don't have to use this workaround.
This works for me to remove the duplicate jar files from JRE System Library.
Steps Right click on Project and go to Build Path->configure build path->Libraries.
Remove the jars that are not in the classpath or duplicated in Maven dependency.

Using Gradle project-report and jacoco plugins together

I'd like to use both the Gradle project-report and jacoco plugins in my project. To illustrate the problem I'm having though, I opened build.gradle at: gradle-1.7-rc-1/samples/testing/jacoco/quickstart and modified it to be:
apply plugin: "java"
apply plugin: "jacoco"
apply plugin: "project-report"
running a gradle projectReport then gives me the error:
gradle projR
:dependencyReport
:propertyReport
:taskReport FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':taskReport'.
> Could not determine the dependencies of task ':jacocoTestReport'.
Any advice on how to get these two plugins to play nice together would be appreciated (note, they both work independently of each other).
Thanks
Covered by GRADLE-2917, will be fixed in version 1.10 as described in the Gradle Forum

Debug Gradle plugins with IntelliJ

Problem
I want to use the interactive debugger with IntelliJ. Unfortunately, I can't convince IntelliJ to load and compile the plugin. However, I can do gradle clean build and the plugin builds and runs its tests as expected.
Specifically, I'm trying to debug local changes to gradle-js-plugin and IntelliJ says it can't find com.google.javascript.jscomp.CompilerOptions as well as spock.lang.Specification. (I'm thinking maybe it's something about the way they are loaded, but that's a guess.)
Things I've tried
NOTE: I didn't revert any processes between steps.
0. My First Guess
I noticed a howto on docs.codehaus.org. IntelliJ couldn't find org.gradle.launcher.GradleMain, so I've adapted it to use GradleLauncher with the following:
import org.gradle.GradleLauncher
class GradleScriptRunner {
public static void main(String[] args) {
GradleLauncher.newInstance(
"-p",
"/path/to/gradle-js-plugin/src/test/resources/build.gradle",
"clean assemble"
)
}
}
Per GradleLauncher's documentation.
Outcome: IntelliJ won't compile the project.
1. Per Peter Niederwieser's answer Fix idea project & debug via plugin
Steps
~# cd /path/to/gradle-js-plugin && gradle cleanIdea idea
Opened the newly created project and attempted to debug using the ScriptRunner from step 0.
Outcome: Project compiles (yay!), but I can only hit breakpoints in GradleScriptRunner.groovy.
2. Per Peter Niederwieser's answer run gradle CLI w/ special options
1 & 2. Merged for clarity:
~# export GRADLE_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
~# gradle clean assemble
Listening for transport dt_socket at address: 5005
Configure IntelliJ to connect to this port and start debugging (see image):
For this step I tried the following .gradle file configurations:
1. Use only build.gradle
--build.gradle--
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'maven'
apply plugin: 'js'
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile findProject "/path/to/gradle-js-plugin"
}
}
repositories {
mavenLocal()
mavenCentral()
}
Outcome:
FAILURE: Build failed with an exception.
* Where:
Build file '/path/to/gradle-js-plugin/src/test/resources/build.gradle' line: 13
* What went wrong:
A problem occurred evaluating root project 'resources'.
> No such property: findProject for class: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 8 mins 50.498 secs
2. Use both build.gradle and settings.gradle
--settings.gradle--
include "/path/to/gradle-js-plugin"
--build.gradle--
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'maven'
apply plugin: 'js'
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
}
repositories {
mavenLocal()
mavenCentral()
}
Outcome:
FAILURE: Build failed with an exception.
* Where:
Build file '/path/to/gradle-js-plugin/src/test/resources/build.gradle' line: 5
* What went wrong:
A problem occurred evaluating root project 'resources'.
> Plugin with id 'js' not found.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 13.553 secs
My Setup
Gradle
~# gradle -v
------------------------------------------------------------
Gradle 1.0
------------------------------------------------------------
Gradle build time: Tuesday, June 12, 2012 12:56:21 AM UTC
Groovy: 1.8.6
Ant: Apache Ant(TM) version 1.8.2 compiled on December 20 2010
Ivy: 2.2.0
JVM: 1.7.0_04 (Oracle Corporation 23.0-b21)
OS: Linux 3.2.0-2-amd64 amd64
Java
~# java -version
java version "1.7.0_04"
Java(TM) SE Runtime Environment (build 1.7.0_04-b20)
Java HotSpot(TM) 64-Bit Server VM (build 23.0-b21, mixed mode)
IntelliJ
IntelliJ IDEA Ultimate 117.499 w/ Bundled Gradle plugin
Hoping for
Any tips that'll get me into debug mode within the plugin.
I was able to debug gradle sources (including plugins) using -Dorg.gradle.debug=true (found on gradle forum):
Stop daemons if any:
./gradlew --stop
Run
./gradlew <task> --no-daemon -Dorg.gradle.debug=true
Connect remotely to gradle process (port 5005) - if using IntelliJ IDEA, see OP's image above
It should stop on breakpoints now.
BTW, I have created a separate IntelliJ IDEA project for gradle sources. Since I use gradle wrapper, I have grabbed the sources from
~/.gradle/wrapper/dists/gradle-1.11-all/7qd8qq8te5j4f5q9aaei3gh3lj/gradle-1.11/src
In IDEA I did File->Import Project, then selected the above path, then - "Create project from existing sources". Hit Next couple of times (made sure I didn't include any jars from lib/plugins directory, since IDEA would complain that most project files already exist).
I then created a remote debug configuration in that IDEA project and used it for debugging gradle.
First, it sounds like there is a problem with your IDEA Gradle project. If you run gradlew cleanIdea idea and then open the generated project from IDEA (rather than using the JetGradle plugin), all should be fine.
Second, if you still can't get the GradleMain/GradleLauncher (the former class does exist) approach to work, another approach is to debug the Gradle build as an external application. For that you need to add -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005 to the GRADLE_OPTS environment variable, run the build from the command line, wait until it suspends, and then start a "Remote" run configuration (with corresponding settings) from IDEA. At that point the debugger should connect to the Gradle process and you should be up and running.
IntelliJ IDEA 12.1 provides ability to debug gradle tasks out of the box - right-click target task at the JetGradle tool window tasks list and choose 'debug'

Resources