Add dependencies to jar Class-Path manifest - java

When I try to add dependencies to jar manifest class-path it is throwing an exception:
Use:
gradle 2.1
build:
apply plugin: 'java'
jar {
manifest {
attributes 'Implementation-Title': 'Main',
'Implementation-Version': version,
'Main-Class': 'com.apl.Main',
'Class-Path': configurations.compile.collect { it.getName() }.join(' ')
}
}
repositories {
flatDir {
dirs 'libs','test_libs'
}
}
dependencies {
compile name: 'bcmail-jdk16-dss'
compile name: 'bcprov-jdk16-dss'
compile name: 'bctsp-jdk16-dss'
}
Exception:
Caused by: org.gradle.api.InvalidUserDataException: You can't change configuration 'compile' because it is already resolved!
Can anyone help me resolve this issue please?

try to put
dependencies {
compile name: 'bcmail-jdk16-dss'
compile name: 'bcprov-jdk16-dss'
compile name: 'bctsp-jdk16-dss'
}
before
jar {
manifest {
attributes 'Implementation-Title': 'Main',
'Implementation-Version': version,
'Main-Class': 'com.apl.Main',
'Class-Path': configurations.compile.collect { it.getName() }.join(' ')
}
}

Related

GKE gradle no main manifest attribute

Hello i am testing GKE features and i created gradle java 17 init application, while deployment i have issue with manifest so i generated manually to artifact to resources :
Manifest-Version: 1.0
Main-Class: pl.kathelan.mdbackend.MdBackendApplication
here is my gradle build :
plugins {
id 'org.springframework.boot' version '2.7.3'
id 'io.spring.dependency-management' version '1.0.13.RELEASE'
id 'java'
}
group = 'pl.kathelan'
version = '0.0.1'
sourceCompatibility = '17'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
jar {
manifest {
attributes(
"Class-Path": configurations.runtimeClasspath .collect { it.getName() }.join(' '))
}
}
sourceSets.main.resources.srcDirs = [ "src/" ]
i have tried this solution Gradle - no main manifest attribute , but for me was exception class no found. Looking for any help thanks.
found mistake in docker file :
ENV ARTIFACT_NAME=md-backend-0.0.1.jar
was md-backend-0.0.1-plain.jar

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.

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

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'

use gradle to embed dependency information into manifest

I would like to embed dependency information into my manifest file, so that I can expose this information at runtime. i.e. I can see which version of a library is used by a particular running instance of my service.
I'm using gradle to build my 'fatjar':
shadowJar {
mergeServiceFiles()
archiveName "service.jar"
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
manifest {
attributes('Main-Class': "service.Service",
'Built-By': System.getProperty('user.name'),
'Built-Date': new Date(),
'Built-JDK': System.getProperty('java.version'),
'Implementation-Version': version,
'Implementation-Title': project.name)
}
}
And I have dependencies on various other libraries:
dependencies {
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.39'
...
}
How can I add the dependency information into my manifest file? For example:
Manifest-Version: 1.0
Implementation-Title: service
Implementation-Version: Local Build
Built-By: me
Built-Date: Wed Jun 22 14:13:53 BST 2016
Built-JDK: 1.8.0_91
Main-Class: service.Service
Dependency-mysql-connector-java: mysql:mysql-connector-java:5.1.39
It can be done in the following way:
buildscript {
repositories {
maven {
url 'https://plugins.gradle.org/m2/'
}
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
}
}
apply plugin: 'java'
apply plugin: 'com.github.johnrengelman.shadow'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.guava:guava:19.0'
compile 'com.google.inject:guice:4.1.0'
}
shadowJar {
mergeServiceFiles()
archiveName "service.jar"
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
manifest {
attributes(
[
'Main-Class': "service.Service",
'Built-By': System.getProperty('user.name'),
'Built-Date': new Date(),
'Built-JDK': System.getProperty('java.version'),
'Implementation-Version': 'version',
'Implementation-Title': project.name,
] +
project.configurations.compile.allDependencies.collect { d ->
[
("dependency-${d.group.replaceAll('\\.','-')}".toString()):"$d.group:$d.name:$d.version"
]
}.sum()
)
}
}
The script above produces the following MANIFEST.MF:
Manifest-Version: 1.0
Main-Class: service.Service
Built-By: opal
Built-Date: Mon Jul 04 17:27:05 CEST 2016
Built-JDK: 1.8.0_91
Implementation-Version: version
Implementation-Title: 37969253
dependency-com-google-guava: com.google.guava:guava:19.0
dependency-com-google-inject: com.google.inject:guice:4.1.0
Since attributes takes Map as an argument, you need to collect them dependencies, transform them to Map and sum the maps.

Adding Jersey MOXy fails Jetty run

I'm testing out REST using Jersey, Hibernate and embedded Jetty. Initially, I was able to start everything up using ./gradlew clean jar && java -jar build/libs/<project>.jar. However, I wanted to support JSON and read from docs that MOXy is Jersey's default JSON provider. So I added the following in my build.gradle file:
compile 'org.glassfish.jersey.media:jersey-media-moxy:2.19'
After which, I tried to build the jar and start up jetty but I get the following issue:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
at sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:284)
at sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:238)
at java.util.jar.JarVerifier.processEntry(JarVerifier.java:273)
at java.util.jar.JarVerifier.update(JarVerifier.java:228)
at java.util.jar.JarFile.initializeVerifier(JarFile.java:383)
at java.util.jar.JarFile.getInputStream(JarFile.java:450)
at sun.misc.URLClassPath$JarLoader$2.getInputStream(URLClassPath.java:940)
at sun.misc.Resource.cachedInputStream(Resource.java:77)
at sun.misc.Resource.getByteBuffer(Resource.java:160)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:454)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
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)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)
Here's my build.gradle file if it helps.
apply plugin: 'eclipse'
apply plugin: 'java'
apply plugin: 'jetty'
repositories {
jcenter()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.12'
// Jetty
compile 'org.eclipse.jetty:jetty-server:9.3.1.v20150714'
compile 'org.eclipse.jetty:jetty-servlet:9.3.1.v20150714'
// Jersey
compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.19'
compile 'org.glassfish.jersey.core:jersey-client:2.19'
compile 'org.glassfish.jersey.media:jersey-media-moxy:2.19'
compile 'org.glassfish.jersey.media:jersey-media-json-processing:2.19'
compile 'org.glassfish.jersey.media:jersey-media-multipart:2.19'
compile 'org.glassfish.jersey.media:jersey-media-sse:2.19'
// Hibernate
compile 'mysql:mysql-connector-java:5.1.36'
compile 'org.hibernate:hibernate-core:4.3.10.Final'
compile 'com.mchange:c3p0:0.9.5.1'
testCompile 'junit:junit:4.12'
}
jar {
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
manifest {
attributes 'Main-Class': 'com.domain.scaffolding.Main'
}
}
After looking a bit more into how gradle builds the fat jar, what solved the issue was to fix jar block:
jar {
from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
manifest {
attributes 'Main-Class': 'com.domain.scaffolding.Main'
}
}

Categories

Resources