how to create executable java gradle project? - java

this is my build.gradle
this is what happens when i attempt to run the built jar.
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
implementation "net.dv8tion:JDA:5.0.0-alpha.6"
implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.1.2'
implementation"io.github.bonigarcia:webdrivermanager:5.1.0"
}
test {
useJUnitPlatform()
}
jar {
manifest {
attributes 'Main-Class': 'bullshitPackage.main'
}
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}

You could use the shadowjar plugin to include all dependencies in your jar.
To use it in your build.gradle file, try:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.github.jengelman.gradle.plugins:shadow:7.0.0"
}
}
plugins {
id 'java'
id 'com.github.johnrengelman.shadow' version '7.0.0'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
implementation "net.dv8tion:JDA:5.0.0-alpha.6"
implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.1.2'
implementation"io.github.bonigarcia:webdrivermanager:5.1.0"
}
test {
useJUnitPlatform()
}
jar {
manifest {
attributes 'Main-Class': 'bullshitPackage.main'
}
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}
Then from your console, navigate to your project's root folder that contains the file gradlew, then run gradlew shadowjar to build the jar into the ./build/libs folder.

Related

Gradle Build Can't find dependencies

I've been using gradle for Springboot and it used to be fine but all of the sudden the gradle build stopped working. I keep getting errors saying that dependencies can't be found.
Here is the gradle code:
plugins {
id 'org.springframework.boot' version '2.4.1'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}
version = '0.0.1'
sourceCompatibility = '1.8'
repositories {
maven {
url 'https://repo1.maven.org/'
}
}
springBoot {
buildInfo()
}
ext {
set('springCloudServicesVersion', "2.4.1")
set('springCloudVersion', "2020.0.3")
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'io.pivotal.spring.cloud:spring-cloud-services-starter-config-client'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'com.microsoft.sqlserver:mssql-jdbc'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
implementation 'org.springframework.boot:spring-boot-starter-integration'
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation 'org.springframework.integration:spring-integration-test'
implementation 'org.springframework.integration:spring-integration-mail:5.5.1'
implementation group: 'javax.mail', name: 'mail', version: '1.5.0-b01'
implementation group: 'org.apache.velocity.tools', name: 'velocity-tools-generic', version: '3.0'
implementation 'org.jsoup:jsoup:1.14.1'
implementation 'com.microsoft.sqlserver:mssql-jdbc:7.4.1.jre8'
}
dependencyManagement {
imports {
mavenBom "io.pivotal.spring.cloud:spring-cloud-services-dependencies:${springCloudServicesVersion}"
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
test {
useJUnitPlatform()
}
The errors I'm getting:
Could not resolve org.springframework.boot:spring-boot-dependencies:2.4.1.
Could not resolve io.pivotal.spring.cloud:spring-cloud-services-dependencies:2.4.1.
Could not resolve org.springframework.cloud:spring-cloud-dependencies:2020.0.3.
Any Ideas?
Your maven repository is wrong.
Change
repositories {
maven {
url 'https://repo1.maven.org/'
}
}
to
repositories {
mavenCentral()
}
See here a good explanation on how to declare the maven repository in your build.gradle file.

Could not find spring-boot-2.3.0.BUILD-SNAPSHOT.jar Error

I am running into the following error when i try to build my spring boot project in gradle. I got the build.gradle from the spring initilizer.
Could not resolve all artifacts for configuration ':classpath'.
Could not find spring-boot-gradle-plugin-2.3.0.BUILD-SNAPSHOT.jar (org.springframework.boot:spring-boot-gradle-plugin:2.3.0.BUILD-SNAPSHOT:20200409.145011-519).
Searched in the following locations:
https://repo.spring.io/snapshot/org/springframework/boot/spring-boot-gradle-plugin/2.3.0.BUILD-SNAPSHOT/spring-boot-gradle-plugin-2.3.0.BUILD-20200409.145011-519.jar
Please find my gradle build
plugins {
id 'org.springframework.boot' version '2.3.0.BUILD-SNAPSHOT'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.mycompany'
version = '0.0.1-SNAPSHOT' //TODO how does verioning work in this project?
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-integration'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.kafka:spring-kafka'
implementation 'org.springframework.boot:spring-boot-starter-aop'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation ('org.springframework.integration:spring-integration-test'){
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation ('org.springframework.kafka:spring-kafka-test'){
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation group: 'org.mockito', name: 'mockito-all', version: '1.10.19'
}
test {
useJUnitPlatform()
}
If you do not have a special reason to use specifically the SNAPSHOT version I suggest using release or milestone.
SNAPSHOT builds are quite unstable. If you want to take a look at the new 2.3 version of the Spring Boot you should probably use the milestone version. For today it would be 2.3.0.M4.
For that, change org.springframework.boot plugin version like this:
id 'org.springframework.boot' version '2.3.0.M4'

Unable to resolve org.springframework.boot:spring-boot-starter-thymeleaf

I'm just learning how to work with spring and I'm new in this. I tried to add thymeleaf to my project with Gradle dependency but It can not be resolved. I think my way of adding thymeleaf to project is wrong. What should i do?
This is my build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath('org.springframework.boot:spring-boot-gradle-plugin:2.2.4.RELEASE')
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
group 'com.aminsl'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.springframework', name: 'spring-webmvc', version: '5.1.9.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.2.4.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
}
I would go to https://start.spring.io/, select the dependencies I need as shown below:
And then Generate the project.
Using the website gives me the below gradle file:
plugins {
id 'org.springframework.boot' version '2.2.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
Looks like you are missing the right plugins in your build file.

How to run my Gradle Project as a JAR from terminal

I have a GRADLE application which has the following structure:
It has some dependencies as well (can see it from the image above)
I have built it using ./gradlew clean build
I am trying to run the following from the terminal java -jar build/libs/script-application-1.0-SNAPSHOT.jar which is throwing no main manifest attribute, in script-application-1.0-SNAPSHOT.jar
I have the following in build.gradle
plugins {
id 'java'
}
jar {
manifest {
attributes(
'Main-Class': 'Application'
)
}
}
group 'ScriptApplication'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.9.3'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
}
Please help. How can I run the jar from terminal? The next step is to run the jar from a remote server.
Edit:
Followed the suggestion from the comment and updated my build.gradle. But I still don't understand where will the jar get generated and how will I run the jar?
plugins {
id 'java'
}
group 'ScriptApplication'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.9.3'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
}
task fatJar(type: Jar) {
manifest {
attributes 'Main-Class': 'Application'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
Had to add:
task fatJar(type: Jar) {
manifest {
attributes 'Main-Class': 'Application'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
Then run gradle clean and gradle fatJar.
Then one can run the jar by:
java -jar build/libs/<application>.jar

Could not find method suites() for arguments [src/test/resources/testSuite.xml] on project

I have created a project in gradle with testng framework, how to execute test-suite in testng using gradle, please help here is my build.gradle
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = 1.8
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart',
'Implementation-Version': version
}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
test {
systemProperties 'property': 'value'
}
test {
useTestNG() {
suites 'src/test/resources/testng.xml'
}
}

Categories