Gradle: Cannot change dependencies of configuration ':compile' after it has been resolved - java

I'm using Netbeans 8.0.2 with Gradle Support plugin 1.3.8.
I've added a task to generate a uber-Jar while excluding a few signature files, however when I run the task it displays an error at line 38 (the compile group line) as follows:
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'br.com.myproject.Sample'
sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
version='1.0.0'
// For DEBUG to work
ext.mainClass = mainClassName
task uniqueJar(type: Jar) {
baseName = project.name
from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
manifest {
attributes 'Implementation-Title': project.name,
'Implementation-Version': version,
'Main-Class': mainClassName
}
with jar
}
repositories {
mavenCentral()
// You may define additional repositories, or even remove "mavenCentral()".
// Read more about repositories here:
// http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:repositories
}
dependencies {
// https://mvnrepository.com/artifact/org.eclipse.paho/org.eclipse.paho.client.mqttv3
compile group: 'org.eclipse.paho', name: 'org.eclipse.paho.client.mqttv3', version: '1.1.1' // line 38
testCompile group: 'junit', name: 'junit', version: '4.10'
}
Error message:
Executing: gradle unique Jar Arguments: [uniqueJar, -c,
D:\NetBeansProjects\testeMqtt\settings.gradle]
FAILURE: Build failed with an exception.
Where: Build file 'D:\NetBeansProjects\testeMqtt\build.gradle' line: 38
What went wrong: A problem occurred evaluating root project 'testeMqtt'.
Cannot change dependencies of configuration ':compile' after it has been resolved.
Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 0.102 secs
How can I fix the uber-Jar task?

Solved my issue using the Shadow gradle plugin for generating uber-Jars:
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'br.com.myproject.Sample'
sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
version='1.0.0'
// For DEBUG to work
ext.mainClass = mainClassName
repositories {
mavenCentral()
}
dependencies {
compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.1'
testCompile group: 'junit', name: 'junit', version: '4.10'
}
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.4'
}
}
apply plugin: 'com.github.johnrengelman.shadow'

Related

Gradle Error: JavaFX runtime components are missing, and are required to run this application

I got this error while executing a Gradle Project in Java using JavaFX.
My build.gradle:
plugins {
id 'eclipse'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.7'
}
sourceCompatibility = 11
targetCompatibility = 11
repositories {
mavenCentral()
}
dependencies {
testImplementation 'junit:junit:4.12'
compile 'com.google.api-client:google-api-client:1.23.0'
compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
compile group: 'com.google.apis', name: 'google-api-services-youtube', version: 'v3-rev222-1.25.0'
}
javafx {
modules = [ 'javafx.controls', 'javafx.fxml', 'javafx.graphics' ]
version = "11.0.2"
}
mainClassName = "Younify_Gradle.Main"
Any ideas or is anything in my build.gradle wrong?

Cannot keep dependency version in gradle.properties

I need to keep versions of dependencies in gradle.properties.
gradle.properties:
springBootVersion = '2.1.9.RELEASE'
build.gradle:
plugins {
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'org.springframework.boot' version '2.1.9.RELEASE'
}
subprojects {
repositories {
mavenCentral()
}
configurations.all {
resolutionStrategy {
failOnVersionConflict()
}
}
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
dependencies {
dependency "org.springframework.boot:spring-boot-starter-web:${springBootVersion}"
dependency "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
}
}
sourceCompatibility = 11
targetCompatibility = 11
tasks.withType(JavaCompile){
options.encoding = 'UTF-8'
}
}
settings.gradle:
include 'api'
api/build.gradle:
apply plugin: 'org.springframework.boot'
archivesBaseName = 'phone-gift-processing-api'
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Output of ./gradlew clean build:
> Task :api:compileJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':api:compileJava'.
> Could not resolve all files for configuration ':api:compileClasspath'.
> Could not find org.springframework.boot:spring-boot-starter-web:'2.1.9.RELEASE'.
Required by:
project :api
I.e., Gradle uses springBootVersion, but fails to bind the dependency for some reason.
When I replace ${springBootVersion} by ${springBootVersion}, build is successful.
You have the format wrong in gradle.properties. Instead of this:
springBootVersion = '2.1.9.RELEASE'
Try this:
springBootVersion=2.1.9.RELEASE
(The spaces are optional, but the important part is that you should not have the quotation marks around the value.)

Run as spring boot app is not working where as run as java app working

I am using STS.
This is my main class:
#EnableZuulProxy
#SpringBootApplication
public static void main(String[] args) {
try {
SpringApplication.run(DevProxyApp.class, args);
}catch(Exception e) {
}
}
}
Below is my build.gradle :
buildscript {
ext {
springBootVersion = '2.0.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id 'pmd'
id 'org.sonarqube' version '2.6.2'
}
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'jacoco'
mainClassName = 'com.siemens.mindsphere.devproxy.DevProxyApp'
group = 'mindsphere'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
ext {
springCloudVersion = 'Finchley.BUILD-SNAPSHOT'
}
jar {
baseName = 'sdk-devproxy'
doLast {
}
destinationDir = file("$buildDir/libs/mindsphere/sdk-devproxy/$project.version/")
}
dependencies {
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-parent', version: 'Edgware.SR3', ext: 'pom'
compile('org.springframework.cloud:spring-cloud-starter-oauth2')
compile('org.springframework.cloud:spring-cloud-starter-netflix-zuul')
compile('org.springframework.boot:spring-boot-starter-web')
compile group: 'com.auth0', name: 'java-jwt', version: '3.3.0'
compile('com.auth0:java-jwt')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
i have also tried with below dependencies:
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-parent', version: '1.2.1.RELEASE', ext: 'pom'
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-parent', version: 'Edgware.SR3', ext: 'pom'
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-oauth2', version: '1.0.0.RELEASE'
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-zuul', version: '1.4.4.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.9.RELEASE'
implementation 'org.slf4j:slf4j-api:1.7.25'
compile group: 'com.auth0', name: 'java-jwt', version: '3.3.0'
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
}
Whenever I run the application as spring boot app its giving me the below error:
Error: Could not find or load main class com.siemens.mindsphere.devproxy.DevProxyApp
While running as java application, it is working starting but with this kind of launch functionalities(oauth2, zuul routing functionalities) are not working.
i have tried below things, but still issue is there:
Refreshing , rebuilding, updating gradle
removing all the dependencies manually, removing gradle repo manually
installed new STS.
If you need any other info to address this issue please let me know.
FYI Previously it was a maven project and working fine, now I am making it as gradle project by adding build.gradle, gradle project and etc. and removed pom.xml. Gradle build is happening properly.
Is the issue with any jar compatibility ????
Try to add a manifest attribute:
jar {
manifest {
attributes(
'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
'Main-Class': 'com.siemens.mindsphere.devproxy.DevProxyApp'
)
}
}
Try to downgrade the springBootVersion.
I had this issue and this resolved it.

java.sql.SQLException: No suitable driver found for jdbc:mariadb in a Gradle project (Intellij)

I have a Gradle project in IntelliJ which I am using to control my dependencies but I am new to Gradle so I am probably doing something wrong.
I am getting this error when running my code:
java.sql.SQLException: No suitable driver found for jdbc:mariadb://<db address>
the build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.10.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')
}
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
dependencies {
// https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client
compile group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '1.1.7'
}
The jdbc.properties file that I am also using:
jdbc.drivers=com.mariadb.jdbc.Driver
jdbc.url=jdbc:mariadb://<db address>
jdbc.user=root
jdbc.password=password
What am I doing wrong?
You are missing a dependency on the MariaDB Java driver hence this message:
java.sql.SQLException: No suitable driver found for jdbc:mariadb://<db address>
You can add the MariaDB Java driver to your classpath by updating the dependencies block of build.gradle ...
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.mariadb.jdbc:mariadb-java-client")
testCompile('org.springframework.boot:spring-boot-starter-test')
}

Spring configuration: Unable to locate Spring NamespaceHandler

Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx] Offending resource: ServletContext resource [/WEB-INF/spring-servlet.xml]
In gradle you can use shadowJar plugin for fix that. My build.gradle file for compile fat jar:
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'application'
mainClassName = 'ru.antowka.Initializer'
buildscript {
repositories { jcenter() }
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.2'
}
}
jar {
manifest {
attributes 'Main-Class': mainClassName
}
}
shadowJar {
mergeServiceFiles('META-INF/spring.*')
}
// JDK 8
sourceCompatibility = 1.8
targetCompatibility = 1.8
compileJava.options.encoding = 'UTF-8'
repositories {
maven {
url 'http://repo.spring.io/snapshot'
}
mavenLocal()
mavenCentral()
jcenter()
}
dependencies {
compile 'org.jsoup:jsoup:1.8.3'
compile 'org.springframework:spring-context:4.2.2.BUILD-SNAPSHOT'
compile 'log4j:log4j:1.2.17'
compile 'org.quartz-scheduler:quartz:1.8.6'
compile 'org.springframework:spring-support:2.0.8'
compile 'org.springframework:spring-tx:2.5.4'
compile 'org.springframework:spring-orm:4.1.7.RELEASE'
compile 'org.hibernate:hibernate-core:4.3.10.Final'
compile 'org.postgresql:postgresql:9.4-1201-jdbc41'
compile 'org.apache.commons:commons-dbcp2:2.1'
testCompile 'org.springframework:spring-test:4.2.0.RELEASE'
testCompile 'org.mockito:mockito-core:1.+'
testCompile 'junit:junit:4.12'
}

Categories

Resources