use gradle to embed dependency information into manifest - java

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.

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

error: package javafx.scene.media does not exist

I'm trying to create javafx media player as gradle project, but got this error.error: package javafx.scene.media does not exist on line: import javafx.scene.media.Media;
Here is my build.gradle
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.8'
}
group 'life.antonov'
version '1.0'
mainClassName='life.antonov.muza.Main'
sourceCompatibility = 11
jar {
manifest {
attributes "Main-Class": "$mainClassName"
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
javafx {
version = "13"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.openjfx', name: 'javafx-controls', version: '13'
compile group: 'org.openjfx', name: 'javafx-fxml', version: '13 '
compile group: 'org.openjfx', name: 'javafx-media', version: '13'
}
I'm tried to unzip downloaded javafx-media jar file and it is almost empty:
$ unzip javafx-media-13.jar
Archive: javafx-media-13.jar
creating: META-INF/
inflating: META-INF/MANIFEST.MF
Why?
Maybe I wrote wrong dependencies?
What I have to do to make my application compile and work?
I have not tested it, but:
Replace:
modules = [ 'javafx.controls', 'javafx.fxml' ]
With
modules = [ 'javafx.controls', 'javafx.fxml', 'javafx.media' ]
If you want to use MediaView control in javafx you must to do 2 steps
Step1: add requires javafx.media;
module com.codegym.videoplayerfx {
requires javafx.controls;
requires javafx.fxml;
requires javafx.media; // You must add this line
opens com.codegym.videoplayerfx to javafx.fxml;
exports com.codegym.videoplayerfx;
}
Step2: Add dependency in pom.xml for maven (or build.gradle for gradle). After that you must press build. Try orther dependency version if does't work
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>16</version>
</dependency>

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'

java.util.zip.ZipException: duplicate entry: META_INF/LICENSE.txt

This is my first time using Java Web Start. This is for a demo version of my application. I have uploaded the JAR to my server, and created a JNLP file. When I run the file locally, I get the exception shown below.
This was the reference I was reading, and below is my JNLP file.
DerbyPro.jnlp
<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="" href="">
<information>
<title>Derby Pro</title>
<vendor>Neon Orb</vendor>
<icon href="http://neonorb.com/images/derby-pro/derby-pro-icon-hd.png"/>
<offline-allowed/>
</information>
<resources>
<j2se version="1.8+" href=
"http://neonorb.com"/>
<jar href="http://neonorb.com/clientportal/derby-pro-demo.jar"
main="true" />
</resources>
<application-desc
name="Derby Pro"
main-class="com.neonorb.derbypro.main.DerbyPro"
width="300"
height="300">
</application-desc>
<update check="background"/>
</jnlp>
Here is my build.gradle file being called like this: ./gradlew -Pversion=0.0.0 -Pdemo=true fatJar
group 'com.neonorb'
apply plugin: 'java'
sourceCompatibility = 1.8
project.description = 'Derby Pro is pinewood derby management software.'
//create a single Jar with all dependencies
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Version': version,
'Main-Class': 'com.neonorb.derbypro.main.DerbyPro',
'Demo': demo
}
baseName = project.name
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
repositories {
mavenCentral()
maven {
url "http://www.sparetimelabs.com/maven2"
}
mavenLocal()
}
dependencies {
compile files('dfalex-0.9.2.jar')
compile 'com.neonorb:commons:+'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.5.4'
compile group: 'commons-cli', name: 'commons-cli', version: '1.3.1'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.4'
compile group: 'org.controlsfx', name: 'controlsfx', version: '8.40.10'
compile group: 'org.fxmisc.easybind', name: 'easybind', version: '1.0.3'
compile group: 'org.scream3r', name: 'jssc', version: '2.8.0'
compile group: 'com.sparetimelabs', name: 'purejavacomm', version: '0.0.28'
compile 'net.java.dev.jna:jna:4.2.1'
compile 'commons-io:commons-io:2.4'
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile 'net.jodah:concurrentunit:0.4.2'
}
//Native launchers
//Windows
buildscript {
repositories {
maven {
url 'https://plugins.gradle.org/m2/'
}
}
dependencies {
classpath 'gradle.plugin.edu.sc.seis.gradle:launch4j:1.6.1'
}
}
apply plugin: 'edu.sc.seis.launch4j'
launch4j {
//outputDir = 'native/windows'
bundledJrePath = 'jre'
dontWrapJar = true
jar = 'bin/derby-pro.jar'
mainClassName = 'com.neonorb.derbypro.main.DerbyPro'
icon = '../../src/main/resources/com/neonorb/derbypro/assets/derby-pro-icon-favicon.ico'
outfile = 'DerbyPro.exe'
companyName = 'Neon Orb'
productName = 'Derby Pro'
}
//OS X
/*plugins {
id "edu.sc.seis.macAppBundle" version "2.1.0"
}
macAppBundle {
mainClassName = "com.example.myApp.Start"
icon = "myIcon.icns"
bundleJRE = true
javaProperties.put("apple.laf.useScreenMenuBar", "true")
backgroundImage = "doc/macbackground.png"
}*/
Here is the manifest directory.
And here is the manifest content.
Manifest-Version: 1.0
Implementation-Version: 0.0.0
Main-Class: com.neonorb.derbypro.main.DerbyPro
Demo: true
Your jar file contains duplicate entries which probably cause problems when being deployed. If you run the following command with your jar file you will see the duplicate entries:
$ unzip -l derby-pro-demo.jar |grep META
0 01-26-16 11:47 META-INF/
116 01-26-16 11:47 META-INF/MANIFEST.MF
321 06-09-15 18:42 META-INF/LICENSE
825 06-09-15 18:42 META-INF/NOTICE
...
11358 06-14-15 12:06 META-INF/LICENSE.txt
172 06-14-15 12:06 META-INF/NOTICE.txt
...
11358 04-03-15 14:30 META-INF/LICENSE.txt
301 04-03-15 14:30 META-INF/NOTICE.txt
The problem is caused by the fatJar. See here and here.
When creating a fat jar, the duplicate entries should be resolved. I have no experience with gradle. The issue (first link) is still marked "open", so I assume there may not be an easy fix. As a work around (if this is possible and works for you), I could imagine to copy all jar content first to a temporary directory. This would overwrite duplicate entries. Then you could build the fat jar from the temporary directory. However, I'd be very careful with that. First some duplicate class files may be relevant, and second the LICENSE, NOTICE, etc. files certainly will cause a license problem.
The JNLP file contains a main class attribute, Java Web Start is trying to create the manifest with that main class. Therefore removing the main class attribute from the JNLP file allows it to run.

Add dependencies to jar Class-Path manifest

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(' ')
}
}

Categories

Resources