How to solve red-cross overlay on build.gradle file? - gradle

There is a red cross overlay on my build.gradle. It is annoying because i am not able to pin-point where it is coming from.
Some information about my project:
1. I am using sts-3.9.8.RELEASE as IDE
2. It is a spring boot project, 2.1.7.RELEASE
3. In gradle-wrapper.properties, i am using v5.5
4. When i run a build or a bootWar task, both tasks run successfully.
5. The rootProject.name in settings.gradle, matches the project name in sts.
6. I can run the spring boot application successfully, and i can access the end-points without any issue.
I have tried deleting the build directory of the project, as i thought there could be some old settings that is upsetting gradle, but no.
I have also tried to right-click on the project, click on Gradle, click on Refresh Gradle Project, to no avail.
I have also tried to refresh the project, but still no, the red cross is still there.
Contents of my build.gradle:
plugins {
id 'java'
id 'org.springframework.boot' version '2.1.7.RELEASE'
}
apply plugin: 'io.spring.dependency-management'
apply plugin: 'eclipse'
apply plugin: 'war'
group = 'com.somename'
//version = '1.0.0'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
jcenter()
}
configurations {
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
}
dependencies {
developmentOnly("org.springframework.boot:spring-boot-devtools")
implementation 'org.springframework.boot:spring-boot-starter-web'
...
testImplementation 'org.springframework.security:spring-security-test'
}
Any help or pointers as to what is upsetting gradle is most welcome.
Thanks! :-)

Solution:
Deleting the build directory and re-building was the solution. The red-cross overlay was still visible on my build.gradle, because in the midst of trying things, i changed the gradle version to the latest v5.6.2 and did not notice that an error appeared in the Problems view pane. In short, it said that it can't run a build with that distribution.
New Issue:
~ 2 years later, with v7.1.1, i stumbled upon my own question/answer which did not help me this time. If you are interested, i posted a new question here.

Related

Could not find org.springframework.boot:spring-boot-dependencies:2.6.1

I follow the instructions here for the latest spring.framework.boot plugin.
My build.gradle has
plugins {
id 'java-library'
id 'eclipse'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'org.springframework.boot' version '2.6.2'
id 'org.springframework.boot.experimental.thin-launcher' version '1.0.23.RELEASE'
}
but gradle tasks have the error
> Could not resolve all dependencies for configuration ':detachedConfiguration1'.
> Could not find org.springframework.boot:spring-boot-dependencies:2.6.2.
UPDATE I neglected to add the final lines of the error description, which were in fact important to answer it.
Searched in the following locations:
- https://repo.spring.io/snapshot/org/springframework/boot/spring-boot-dependencies/2.6.2/spring-boot-dependencies-2.6.2.pom
- https://repo.spring.io/milestone/org/springframework/boot/spring-boot-dependencies/2.6.2/spring-boot-dependencies-2.6.2.pom
Here is a FULL file as an example: (this is the ROOT build.gradle) (If you code a monolith, you'll only have a root build.gradle, if you write multiple "gradle modules", you'll have a root and subproject build.gradle files) (if you have no idea what i'm talking about see link to docs.gradle.org at bottom)
(you can remove the dependencies, but i prefer to give a full working)
plugins {
id 'org.springframework.boot' version '2.6.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-batch'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-ldap'
implementation 'org.springframework.boot:spring-boot-starter-integration'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-web-services'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
implementation 'com.h2database:h2'
implementation 'mysql:mysql-connector-java'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
testImplementation 'org.springframework.batch:spring-batch-test'
testImplementation 'org.springframework.integration:spring-integration-test'
testImplementation 'org.springframework.security:spring-security-test'
}
test {
useJUnitPlatform()
}
allprojects {
/* custom task to show dependencies. run "gradle printAllDependencies" from commandline. see https://stackoverflow.com/questions/44266687/how-to-print-out-all-dependencies-in-a-gradle-multi-project-build/54436979#54436979 */
task printAllDependencies(type: DependencyReportTask) {}
}
Alternatively, you can try a "gradle FLUSH"
Gradle FLUSH Cache
(Optional, but preferred). Close all instances of IntelliJ or any other Java IDE.
./gradlew --stop
OR
gradle --stop
(now delete the folders)
rm -rf $HOME/.gradle/caches/
rm -rf $HOME/.gradle/build-cache-tmp/
(now resume normal gradlew commands like:)
./gradlew clean build
and alternatively, you can do an intelliJ "FLUSH"
(tested with IntelliJ version 2020.1 or later)
Close IntelliJ.
Delete the ".idea" folder off of the root folder.
Re OPEN the project.
Wait for Gradle imports and indices rebuild to complete
Try the IDE build again.
And the big hammer: "Invalidate IntelliJ caches". see https://www.jetbrains.com/help/rider/Cleaning_System_Cache.html and/or https://www.jetbrains.com/help/idea/invalidate-caches.html
If you're using Eclipse or NetBeans or Other, you'll have to find the "equivalent". The idea is that the IDE gets "confused".
BONUS
Multi Gradle Module :
https://docs.gradle.org/current/samples/sample_building_java_applications_multi_project.html
Thanks to #granadaCoder above, and after reinstalling Gradle to the latest version 7.3.3 the actual problem was that a 2nd repositories setting had crept into my build.gradle
repositories {
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
Which was exactly where the error said the dependencies were not found. I changed this to
repositories {
mavenCentral()
}
Things then started to work.

Can I specify in gradle that I only want to use a certain plugin for tests?

I have a project and for testing purposes only I want to standup a quick websocket server. Spring Boot seems like the simplest way to do that, but I don't want to include the plugin in my implementation, only for tests. I can't seem to find documentation on something like this, but in general I've found the answer to the question "can gradle do this" is usually yes.
So how would I go about specifying to only use the 'org.springframework.boot' plugin for test builds?
I've tried placing the plugins {} block inside a test {} block but that doesn't work.
I'm pretty sure you may just apply the org.springframework.boot plugin in the regular way and it won't effect your build artifacts.
However, it is possible to apply plugins dynamically, if you are afraid that they might have undesired side-effects on build artifacts. The old plugin mechanism actually worked this way by default and separated the resolution of plugins from their application to the Project instance:
// This part resolves the plugin
buildscript {
repositories {
maven {
url 'https://plugins.gradle.org/m2/'
}
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.3.2.RELEASE'
}
}
// This part applies the plugin
apply plugin: 'org.springframework.boot'
This way it was possible to apply a plugin based on a condition:
if (testMode) {
apply plugin: 'org.springframework.boot'
}
Using the new plugins block to apply plugins, this is not possible directly, as the plugins block is a special block that does not allow custom code:
// This is not allowed!
plugins {
if (testMode) {
id 'org.springframework.boot' version '2.3.2.RELEASE'
}
}
The solution is to tell the plugins block to resolve a plugin without applying it automatically. This can then be done dynamically using apply plugin::
plugins {
id 'org.springframework.boot' version '2.3.2.RELEASE' apply false
}
if (testMode) {
apply plugin: 'org.springframework.boot'
}
You don't have to use org.springframework.boot plugin: just use io.spring.dependency-management plugin. This way, you will be able to declare spring-* related dependencies in your testImplementation configuration, without any impact on the implementation configuration.
plugins {
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-websocket'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
dependencyManagement {
imports {
mavenBom "org.springframework.boot:spring-boot-dependencies:2.2.0.RELEASE"
}
}
Then in your test sources, you will be able to declare SpringBoot application and other websocket-related stuff (controllers)
EDIT what org.springframework.boot is actually doing, is to change the packaging of the main jar artifact by declaring the bootJar task, see Springboot plugin reference. In your case you don't need this, as far as I understand, if you just want to run some Spring application in test sourceset.

Gradle build problem: Could not get unknown property 'libraries' in Caffeine Simulator

I used to work on my fork of Caffeine’s Simulator using Eclipse, and the project was compiled and built OK.
Suddenly I started getting the following error while running Gradle build:
Could not get unknown property 'libraries' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
I indeed don’t understand the syntax “implementation libraries.X” in Caffeine's Simulator build.gradle dependencies.
To study the problem, I generated the project MWE in Eclipse, with the following build.gradle:
plugins {
id 'java-library'
}
repositories {
jcenter()
}
dependencies {
api 'org.apache.commons:commons-math3:3.6.1'
implementation 'com.google.guava:guava:28.2-jre'
testImplementation 'junit:junit:4.12'
implementation libraries.xz
}
This MWE gives the same error.
Once removing the line implementation libraries.xz
the project builds OK.
I guess that "libraries" is a property / variable defined somewhere in Caffeine, but fail to find it.
The manifest of dependencies is defined in dependencies.gradle. The root build file applies this to all of the subprojects,
subprojects {
apply plugin: 'biz.aQute.bnd.builder'
apply plugin: 'java-library'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply from: "${rootDir}/gradle/publish.gradle"
apply from: "${rootDir}/gradle/codeQuality.gradle"
apply from: "${rootDir}/gradle/dependencies.gradle"
...
}
The central manifest allows for referring to the dependency more abstractly, so that versions can be upgraded globally and reduces copy-and-paste issues.
It sounds like a local change to the build causes it to no longer import the manifest and the property cannot be resolved.

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.

Spring Boot Gradle Plugin "Blessed" Dependencies in a multi-project environment

In a multi-project Gradle environment, I have the usual buildscript block in my parent build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.7.RELEASE")
}
}
However, the dependencies which are supposed to be "blessed" (See 54.2 Declaring dependencies without versions) does not seem to be, since Gradle does not search for the correct version (because there is not version at all). I wonder if I am missing some additional configuration or it's just not possible at this moment to have "blessed" dependencies for a multi-project Gradle environment.
Applying spring-boot plugin to all projects (not just parent project) should help.
Try:
allprojects {
apply plugin: 'spring-boot'
}
EDIT: Andy and I posted the answers almost at the same time. But yes, either allprojects or subprojects should work fine, depending whether you parent project needs the plugin, or not.
You need to apply the Spring Boot plugin to every project that you want to use the Boot-provided dependency versions. As it's a multi-project build (and assuming you want to apply the plugin to every subproject), add the following to your build.gradle:
subprojects {
apply plugin: 'spring-boot'
}

Resources