Project ignores build.gradle when cloned from Git - java

First of all I've got a Java Project with Spring boot and I'm using Gradle.
My Problem is, when I am cloning my Project from Gitlab the spring framework libraries aren't working. On every import statement the following occurs:
Unused import statements
I figured out how to fix it. I only need to copy the content of my whole build.gradle file, delete the content and paste it in the build.gradle file again. After this step everything works normal.
my build.gradle looks as follows:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.7.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
jar {
baseName = 'gs-rest-service'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile('org.springframework.boot:spring-boot-starter-test')
}
Is there a better way of fixing this issue than copy and paste?

Related

Error while trying to build gradle project in Intellij Idea

I am trying to accomplish a Java Spring course at Coursera and downloaded the project with this gradle file.
buildscript {
ext {
springBootVersion = '2.1.0.RELEASE'
}
repositories {
maven { url "https://repo.spring.io/libs-snapshot" }
mavenLocal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.2.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
sourceCompatibility = 1.8
targetCompatibility = 1.8
war {
baseName = 'gs-convert-jar-to-war'
version = '0.1.0'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/libs-snapshot" }
maven { url "http://maven.springframework.org/milestone" }
flatDir {
dirs 'lib'
}
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.boot:spring-boot-starter-aop")
compile("org.springframework.boot:spring-boot-starter-test")
compile("org.springframework.boot:spring-boot-starter-jetty")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.data:spring-data-rest-webmvc")
compile("org.apache.httpcomponents:httpclient:4.5.6")
compile("org.hsqldb:hsqldb")
compile("com.google.guava:guava:17.0")
compile("org.apache.commons:commons-lang3:3.3.2")
compile("com.squareup.retrofit:retrofit:1.6.0")
compile("commons-io:commons-io:2.4")
compile("com.github.davidmarquis:fluent-interface-proxy:1.3.0")
compile(":mobilecloud.handin:1.0.0")
compile(":video.up.test:1.0.0")
compile(":autograder.handin:1.0.0")
compile(":autograder.spec:1.0.0")
testCompile("junit:junit")
}
Unfortunately, I have this error in IntelliJ Idea :
A problem occurred configuring root project 'mobile-cloud-asgn1'.
> Failed to notify project evaluation listener.
> org.gradle.api.tasks.SourceSetOutput.getClassesDir()Ljava/io/File;
I am quite a newbie to Java and Gradle and never faced errors like this, so I have no idea how to fix it. Can anyone help me?
P.S. Repository with project template: https://github.com/juleswhite/mobile-cloud-asgn1

Unable to use outside dependencies within custom gradle plugin implementation

When attempting to use outside dependencies within a custom gradle plugin I'm building, I am not able to import or use them in any regard.
I've attempted to specify in both the build script and the normal dependencies closure my dependencies. I'm using Gradle 5.5 (wrapper script) and I am using the buildSrc method of writing a custom gradle plugin.
// Necessary if loading custom plugins
apply plugin: 'java-gradle-plugin'
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.google.code.gson:gson:2.8.5'
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
group 'com.foo'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
}
gradlePlugin {
plugins {
greeterPlugin {
id = 'com.foo.dbcreation-plugin'
implementationClass = 'com.foo.dbcreation.DbCreation'
}
}
}
dependencies {
compile 'com.google.code.gson:gson:2.8.5'
}
There are quite a few issues I see here.
buildscript does not control the dependencies for your plugin implementation.
Use the plugins {} DSL block to apply plugins. It is the preferred way: https://docs.gradle.org/current/userguide/plugins.html#sec:plugins_block
Should be using implementation over compile since compile is deprecated as noted in https://docs.gradle.org/current/userguide/java_plugin.html#tab:configurations
With that said, your Gradle file should be like:
plugins {
id 'java-gradle-plugin'
id 'eclipse'
id 'idea'
}
group 'com.foo'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
}
gradlePlugin {
plugins {
greeterPlugin {
id = 'com.foo.dbcreation-plugin'
implementationClass = 'com.foo.dbcreation.DbCreation'
}
}
}
dependencies {
implementation 'com.google.code.gson:gson:2.8.5'
}
I figured out what my issue was. For projects being built using the buildSrc directory, you need to have the build.gradle file reside in that directory instead of the root project directory (where the build.gradle normally lives). I just converted the project to a normal gradle project and it works just fine.

spring boot gradle plugin in a multi module project - error finding mainClass

I am using Spring boot gradle plugin version 1.5.1 RELEASE as shown below. The build fails at webProject complaining about missing property 'mainClass' and works only when I run 'webProject:build'. Is this the expected usage?
Edit: Updated the build script and removed 'spring-boot' plugin from allProjects. Had to add 'bootRepackage' in web project as it was failing at this step - with the same error. Adding the 'bootRepackage' didn't help.
buildscript {
ext {
springBootVersion = '1.5.1.RELEASE'
}
repositories {
mavenLocal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.1.RELEASE")
}
}
plugins {
id 'org.springframework.boot' version '1.5.1.RELEASE'
}
defaultTasks 'clean', 'build'
apply plugin: 'java'
apply plugin: 'war'
sourceCompatibility = 1.7
targetCompatibility = 1.7
allprojects {
apply plugin: 'java'
//apply plugin: 'org.springframework.boot' -- Commented out based on the answer
repositories {
mavenLocal()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
//all dependencies
}
}
project('aProject') {
dependencies {
compile(project(':bProject'))
}
}
project('webProject') {
apply plugin: 'war'
apply plugin: 'org.springframework.boot'
war {
baseName = 'webProject'
version = '1.0.0-SNAPSHOT'
}
dependencies {
compile(project(':aproject'))
compile(project(':bProject'))
compile 'org.springframework.boot:spring-boot-starter-tomcat'
}
springBoot {
mainClass = 'com.abc.SomeApplication'
}
bootRepackage{
enabled = false
mainClass = 'com.abc.SomeApplication'
}
}
Do not use Spring Boot gradle plugin in main project, only in webProject sub-module.
I have a similar problem in a multi module Spring boot project.
When compiling module A which doesn't have a main class. The main class is in a different module (module B).
I add a plugin in that module A's build.gradle.
apply plugin: 'application'
mainClassName = "module B.ITS_MAIN_CLASS"
Then it works.

Unable to Build Spring RestService Tutorial in IntelliJ

I am attempting to work through the Spring Framework Restful Web Service creation tutorial(https://spring.io/guides/gs/rest-service/#scratch) using Gradle and IntelliJ. I have followed everything to the letter but being fairly new to Spring, IntelliJ, and Java in general I'm unsure how to go about further debugging my issue.
When I attempt to build my project I receive a few errors stating "Java: package org.springframework.web.bind.annotation does not exist." I'm guessing I'm missing a library reference but am unsure how to check and include it.
buildscript {
ext {
springBootVersion = '1.3.5.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.springframework:spring-web:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'hello_springtest'
version = '0.0.1-SNAPSHOT'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
Just thought I'd add some additional information. I'm still seeing the errors and am unsure why but my project does report that the build was successful. When I attempt to make the project however that's when I receive the annotation does not exist error.
You have some dependency in your builds script, which seems to me redundant and causes Gradle to look up for additional dependencies.
Just remove this dependency from your buildscript dependencies
classpath("org.springframework:spring-web:${springBootVersion}")
I see no reason to use it within your buildscript.

build using gradle instead of eclipse

I'm very new to whole tomcat/spring/eclipse world.
I used gradle a little bit for android projects.
This is a project with tomcat/spring/eclipse and I 'd like to build it with gradle.
I copied a build.gradle file from one of tutorial on the web.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.7
targetCompatibility = 1.7
dependencies {
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
testCompile("junit:junit")
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
Now I run > gradle build and I see tons of errors which says 'org.springframework.*** does not exist'
I guess I need to somehow tell gradle to include *.jar files under
WebContent/WEB-INF/lib directory, but don't know how.
Please let me know if need to supply more info.
To add all jar files from WebContent/WEB-INF/lib and subfolders you must include the first line:
dependencies {
compile(fileTree(dir: "WebContent/WEB-INF/lib", include: "**/*.jar"))
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
testCompile("junit:junit")
}

Categories

Resources