build using gradle instead of eclipse - java

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")
}

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.

How to make Gradle + Google Sheets API + Java into a single downloadable program?

As of now I have made a chatbot application by combining Java and Google Sheets using the Google API which requires me to use Gradle. So far I have only been able to run the program through Gradle using the terminal.
In my end result I want to be able to send this program to someone and they should be able to install it or run it as easy as possible without having to run it through the terminal.
I suspect that the person might need to download Gradle to run the file since it is needed for the Google API. So I think the best way to do this is to send a zip file (or something else) with the Gradle Installer and the java file and somehow make it install everything automatically. Is this possible?
This is how my build.gradle file looks like
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'SheetsQuickstart'
sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '1.0'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.1'
}
}
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.api-client:google-api-client:1.23.0'
compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
compile 'com.google.apis:google-api-services-sheets:v4-rev516-1.23.0'
compile(group: 'org.springframework', name: 'spring-core', version:'4.3.11.RELEASE')
}
jar {
doFirst {
manifest {
if (!configurations.compile.isEmpty()) {
attributes(
'Class-Path':configurations.compile.collect{it.toURI().toString()}.join(' '),
'Main-Class': 'SheetsQuickstart')
}
}
}
}
Assuming google sheets api is a dependency within gradle, cant you just build a jar file through gradle build then run the jar as an executable file?
build.gradle file example:
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'com.github.johnrengelman.shadow'
group = 'your.package.path.here'
version = '0.0.1-SNAPSHOT'
mainClassName = 'your.package.path.here.MainClassName'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.1'
}
}
dependencies {
compile(group: 'org.springframework', name: 'spring-core', version:'4.3.11.RELEASE')
}
repositories {
mavenCentral()
}
jar {
doFirst {
manifest {
if (!configurations.compile.isEmpty()) {
attributes(
'Class-Path':configurations.compile.collect{it.toURI().toString()}.join(' '),
'Main-Class': 'your.package.path.here.MainClassName')
}
}
}
Should be enough for a reg min build file, then add your dependencies.

Project ignores build.gradle when cloned from Git

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?

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.

Categories

Resources