When I try to run the gradle project from Idea I've an error
FAILURE: Build failed with an exception.
What went wrong: Task 'run Application' not found in root project 'app-search'.
Try: Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. CREATE IMAGE BUILD FAILED
Total time: 0.153 secs Task 'run SearchApplication' not found in root
project 'app-search'.
Please how to solve it.?
This is the main part of my build.gradle
apply plugin: 'application'
apply plugin: 'java'
apply plugin: 'docker'
apply plugin: 'com.github.johnrengelman.shadow'
sourceCompatibility = 1.8
ext {
dropwizardVersion = '1.1.2'
mainClass = "com.jobservice.search.AppLauncher"
mapstructVersion = '1.1.0.Final'
}
mainClassName = mainClass
repositories {
mavenLocal()
jcenter()
mavenCentral()
}
buildscript {
repositories { jcenter() }
dependencies {
classpath 'se.transmode.gradle:gradle-docker:1.2'
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.4'
}
}
// add logging into test stream
test {
testLogging.showStandardStreams = true
}
dependencies {
compile group: 'org.bouncycastle', name: 'bcprov-jdk16', version: '1.46'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.1'
compile group: 'org.slf4j', name: 'slf4j-ext', version: '1.7.25'
compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
compile 'com.google.inject:guice:3.0'
compile group: 'io.swagger', name: 'swagger-jersey2-jaxrs', version: '1.5.15'
compile group: 'io.swagger', name: 'swagger-annotations', version: '1.5.15'
compile group: 'com.hubspot.dropwizard', name: 'dropwizard-guice', version: '1.0.6.0'
compile group: 'org.liquibase', name: 'liquibase-core', version: '3.5.3'
compile (
'io.dropwizard:dropwizard-core:' + dropwizardVersion,
'io.dropwizard:dropwizard-hibernate:' + dropwizardVersion,
'org.postgresql:postgresql:9.4.1208.jre7',
'io.dropwizard:dropwizard-testing:' + dropwizardVersion
)
compile 'org.mapstruct:mapstruct-jdk8:' + mapstructVersion
compile group: 'org.apache.commons', name: 'commons-collections4', version: '4.1'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.hamcrest', name: 'hamcrest-junit', version: '2.0.0.0'
testCompile 'org.mockito:mockito-core:2.0.54-beta'
testCompile group: 'com.h2database', name: 'h2', version: '1.4.194'
testCompile('org.glassfish.jersey.test-framework.providers:jersey-test-framework-provider-grizzly2:2.23.2') {
exclude group: 'javax.servlet', module: 'javax.servlet-api'
exclude group: 'junit', module: 'junit'
}
}
// Configure the shadow jar task
shadowJar {
classifier = 'dist'
baseName = 'jobs-search'
version = "1.0"
mergeServiceFiles()
manifest {
attributes 'Main-Class': mainClassName
}
version = ''
}
jar {
manifest {
attributes 'Main-Class': mainClassName
}
}
Related
I need to create an executable file for my program (.exe). However when running the executable I get the message com / abstudio / printersreports / Launcher not found -Failed to launch JVM. Program works in IDE
My gradle file
plugins {
id 'java'
id 'application'
id 'idea'
}
sourceCompatibility = JavaVersion.VERSION_1_8
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
jar {
manifest {
attributes(
'Main-Class': 'com.abstudio.printersreports.Launcher'
)
}
}
sourceSets.main {
java {
srcDir 'src/main/java' //assume that your source codes are inside this path
}
resources {
srcDirs = ['src/main/java', 'src/main/resources']
exclude "**/*.java"
}
}
compileJava.options.encoding = 'UTF-8'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
// https://mvnrepository.com/artifact/com.jfoenix/jfoenix
implementation group: 'com.jfoenix', name: 'jfoenix', version: '8.0.10'
compile 'com.jfoenix:jfoenix:8.0.10'
compile group: 'de.jensd', name: 'fontawesomefx', version: '8.1'
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.22'
// https://mvnrepository.com/artifact/com.itextpdf/itextpdf
compile group: 'com.itextpdf', name: 'itextpdf', version: '5.5.13.2'
compile group: 'org.apache.derby', name: 'derby', version: '10.14.1.0'
// https://mvnrepository.com/artifact/org.controlsfx/controlsfx
compile group: 'org.controlsfx', name: 'controlsfx', version: '8.40.18'
compile "com.google.zxing:core:3.3.0"
compile 'com.google.zxing:javase:3.3.0'
}
mainClassName = 'com.abstudio.printersreports.Launcher'
Artifacts looks like this
I am trying to add ajc compiler to my gradle project as gradle plugin. Unfortunately, during compilation it shows me massive amount of errors due to Lombok.
build.gradle:
group 'com.kmb.bank'
version '0.0.1-SNAPSHOT'
project.ext {
aspectjVersion = '1.9.2'
}
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.6"
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: "aspectj.gradle"
sourceCompatibility = 11
targetCompatibility = 11
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.7'
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'commons-codec', name: 'commons-codec', version: '1.11'
implementation('org.springframework.boot:spring-boot-starter-amqp')
implementation('org.springframework.boot:spring-boot-starter-web')
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile ('org.springframework.boot:spring-boot-starter-security')
compile("org.springframework.boot:spring-boot-starter-data-mongodb")
compile group: 'org.springframework.boot', name: 'spring-boot-starter- aop', version: '2.1.1.RELEASE'
}
It shows me errors that there are no getter, setters for every model.
This is working for me:
plugins {
id 'java'
id "io.freefair.aspectj.post-compile-weaving" version "4.1.4"
}
group 'com.amdocs'
version '1.0.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.projectlombok', name: 'lombok', version: '1.18.10'
annotationProcessor "org.projectlombok:lombok:1.18.10"
}
I'm writing java console application using Spring Boot Jpa and MySQL connector. How I can easily exclude all unnecessary libs from my fat jar?
build.gradle
buildscript {
ext {
springBootVersion = '1.5.9.RELEASE'
}
repositories {
// mavenCentral()
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath 'ca.cutterslade.gradle:gradle-dependency-analyze:1.2.0'
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'ca.cutterslade.analyze'
sourceCompatibility = 1.8
targetCompatibility = 1.8
ext {
mysqlVersion = '6.0.6'
hibernateVersion = '5.2.12.Final'
}
repositories {
mavenCentral()
}
dependencies {
// compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.12.Final'
// compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.2.12.Final'
// compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'
// compile group: 'org.springframework.boot', name: 'spring-boot', version: '1.5.9.RELEASE'
// compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.0-api', version: '1.0.0.Final'
// compile group: 'org.springframework', name: 'spring-context', version: '4.3.13.RELEASE'
// compile group: 'org.springframework', name: 'spring-beans', version: '4.3.13.RELEASE'
// compile group: 'org.springframework.boot', name: 'spring-boot-autoconfigure', version: '1.5.9.RELEASE'
// compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
// compile group: 'org.springframework', name: 'spring-tx', version: '2.5.4'
// compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '1.11.9.RELEASE'
// testCompile group: 'org.springframework.boot', name: 'spring-boot-test', version: '1.5.9.RELEASE'
// testCompile group: 'junit', name: 'junit', version: '4.12'
// testCompile group: 'org.springframework', name: 'spring-test', version: '4.3.13.RELEASE'
compile group: 'mysql', name: 'mysql-connector-java', version: mysqlVersion
compile group: 'org.hibernate', name: 'hibernate-core', version: hibernateVersion
compile("org.springframework.boot:spring-boot-starter-data-jpa")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
jar {
baseName 'ReportGenerator'
version '1.0'
}
UPDATE
I have tried to use gradle-dependency-analyze and received the following result:
usedUndeclaredArtifacts:
- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final
- org.springframework.boot:spring-boot:1.5.9.RELEASE
- org.springframework:spring-context:4.3.13.RELEASE
- org.springframework:spring-beans:4.3.13.RELEASE
- org.springframework.boot:spring-boot-autoconfigure:1.5.9.RELEASE
- org.slf4j:slf4j-api:1.7.25
- org.springframework:spring-tx:4.3.13.RELEASE
- org.springframework.data:spring-data-jpa:1.11.9.RELEASE
unusedDeclaredArtifacts:
- mysql:mysql-connector-java:6.0.6
- org.hibernate:hibernate-core:5.2.12.Final
- org.springframework.boot:spring-boot-starter-data-jpa:1.5.9.RELEASE
As you see unused libraries are marked all libraries in my gradle at the moment. And required librariesare marked only which are used in the scope of my classes in app but don't get me know what these required libraries depends on also. If I will put only these list of required dependencies in the gradle then I will get different initialization errors since some libraries are missed.
Couple of suggestions.
Use dependency:analyze on your project which will list the unused dependencies, which you can exclude or get rid of.
use the <scope> attribute for each and every dependency in your pom, which can greatly reduce your fat jar size. Provide correct scope parameters, like compile, test .....
I have a spring boot project using gradle
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'jetty'
apply plugin: 'war'
apply plugin: 'org.springframework.boot'
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web:1.5.1.RELEASE") {
exclude module: "spring-boot-starter-tomcat"
}
compile group: 'org.springframework.boot', name: 'spring-boot-starter-jetty', version: '1.5.1.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: '1.5.1.RELEASE'
compile group: 'org.springframework', name: 'spring-webmvc', version: '4.3.6.RELEASE'
compile group: 'org.springframework', name: 'spring-context-support', version: '4.3.6.RELEASE'
compile group: 'org.springframework', name: 'spring-orm', version: '4.3.6.RELEASE'
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
compile group: 'org.freemarker', name: 'freemarker', version: '2.3.23'
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.5'
compile group: 'org.hibernate', name: 'hibernate-core', version: '5.1.4.Final'
compile group: 'commons-validator', name: 'commons-validator', version: '1.5.1'
compile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.5.1.RELEASE'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.2'
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.1.RELEASE")
}
}
jar {
baseName = 'base-name'
version = '0.1.0'
manifest {
attributes 'Main-Class': 'some.application.Application'
}
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
I build using
./gradlew clean jar
Then try to run using
java -jar <jar-name>.jar
However I get an error saying
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
at some.application.Application.main(Application.java:11)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
I am so confused as to why I can run it in IDE but not as a jar. Also I found out that my jar doesn't contain any libraries at all.
EDIT:
This is my Application.class
package some.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application
{
public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
}
}
This is because your dependencies are not included into the end jar file.
Take a look on the examples here or here.
Or alternatively use ./gradlew clean build that should automatically pack the app correctly. Take a look on the official guide
Define a task called fatJar in build.gradle:
jar {
baseName = 'base-name'
version = '0.1.0'
manifest {
attributes(
'Main-Class': 'com.github:'
)
}
}
task fatJar(type: Jar) {
manifest.from jar.manifest
// classifier = 'all'
from {
configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
} {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
with jar
}
artifacts {
archives fatJar
}
Run gradle fatJar or ./gradlew fatJar to generate a jar file with all dependencies, which can run independently but may be pretty large in size (that's why it's called a fat jar).
When I try to run main method in my gradle project using run configuration from IDEA Intellij option I'm getting exception as follows:
15:12:03: Executing external task 'run'...
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/GsonBuilder
at pl.edu.agh.flowshop.utils.ConfigReader.getMachinesConfig(ConfigReader.java:46)
at pl.edu.agh.flowshop.utils.ConfigReader.createModel(ConfigReader.java:30)
at pl.edu.agh.flowshop.Experiment.main(Experiment.java:25)
Caused by: java.lang.ClassNotFoundException: com.google.gson.GsonBuilder
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 3 more
It bothers me because if I just create runnable jar from same configuration everything works fine. I've tried refrehing gradle project as well as 'Invalidate Caches/Restart' option but none of them helped.
My build.gradle file:
group 'pl.edu.agh'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'application'
sourceCompatibility = 1.7
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceSets.main.java.srcDirs = ['src/main/java']
mainClassName = 'pl.edu.agh.flowshop.Experiment'
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile group: 'nz.ac.waikato.cms.weka', name: 'weka-stable', version: '3.6.6'
compile group: 'commons-io', name: 'commons-io', version: '2.4'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
compile group: 'org.apache.commons', name: 'commons-math3', version: '3.6'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.5'
compile group: 'com.google.guava', name: 'guava', version: '19.0'
compile group: 'com.google.code.gson', name: 'gson', version: '2.6.2'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.5'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.5'
compile name:'piqle'
}
jar {
manifest {
attributes(
'Implementation-Title': project.name,
'Implementation-Version': version,
'Built-By': System.getProperty('user.name'),
'Built-Date': new Date(),
'Built-JDK': System.getProperty('java.version'),
"Main-Class": mainClassName
)
}
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
}