I have a Gradle project and I create a regular classes jar, and also a javadoc and sources jar.
In all three I need to include a META-INF folder that includes a LICENSE and NOTICE file.
I have this folder with files under src/main/resources/
the classes jar and the sources jar work correctly but I need to also get them added to the javadoc jar.
How can I fix the javadocJar task to include META-INF folder?
// custom tasks for creating source/javadoc jars
task sourcesJar(type: Jar, dependsOn:classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn:javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
// add javadoc/source jar tasks as artifacts
artifacts {
archives sourcesJar
archives javadocJar
}
I did get what I needed by adding a copy task but it would be good if I could select just the META-INF directory in case I add more directories under resources.
// custom tasks for creating source/javadoc jars
task sourcesJar(type: Jar, dependsOn:classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task copyResources(type: Copy, dependsOn:javadoc) {
into javadoc.destinationDir
from sourceSets.main.resources
}
task javadocJar(type: Jar, dependsOn:copyResources) {
classifier = 'javadoc'
from javadoc.destinationDir
}
Related
I need to publish a couple of obfuscated jar files with their POM (before obfuscation) to a Nexus repository with Gradle.
The problem is if I select components.java then the non-obfuscated jar file is deployed.
I am using yGuard for obfuscation, and now in my build file I have created pom file, clean jar file and obfuscated jar file, but cannot find a way to publish the pom and obfuscated jar file together to our Nexus repository.
By the way the project is a multi module java-library project.
In my main build.gradle file I have something like
configure(subprojects) {
apply plugin: 'java-library'
apply plugin: 'maven-publish'
jar {
archiveFileName = "${project.name}-${project.version}-clean.jar"
into("META-INF/maven/$project.group/$project.name") {
from { generatePomFileForCleanPublication }
rename '.*', 'pom.xml'
}
}
publishing {
publications {
clean(MavenPublication) {
from components.java
withBuildIdentifier()
}
}
repositories {
maven {
name 'nexus'
url 'SOME URL'
allowInsecureProtocol = true
credentials {
username 'someUserName'
password 'somePassword'
}
}
}
}
}
so the pom is created according to module dependencies, but now I cannot publish the obfuscated jar file with same POM file.
Notes: The obfuscated jar files are generated in same folder as 'build\lib' without '-clean.jar` suffix.
Notes: when I remove from components.java and add my own artifact (obfuscated file) the generated pom won't have dependencies, and I couldn't find a way to override the jar file added to publication.
There are lots of repeated answers on how to exclude an individual file from a fatJar. Typically, the file is excluded are in META-INF and they are excluded either because of a filename conflict, or because it is a signature copied from a dependency libarar Jar file which isn't valid for the newly created Jar file.
Example for maven:
How can I tell which signed jar is causing maven-shade-plugin to fail?
Example for gradle:
Removing Jar Signatures in Gradle Build
These solutions, however, only removed the offending file individually.
How can we make a fatJar with a specific dependency library (not individual files in that library) excluded?
For example, in question 36226033, it's easy to exclude the signature copied over from BouncyCastle, but is there a way to exclude the dependency library bcprov-jdk15on-*.jar entirely, so that the user must have the library available in order to execute the generated fat Jar?
This is proven not working:
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': version,
'Main-Class': 'com.alphawallet.scripttool.Main'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
exclude('**/bcprov-jdk15on-1.62.jar')
with jar
}
With exclude('**/bcprov-jdk15on-1.62.jar'), the content of that jar file is still copied over to the fat jar generated.
Thanks. The motivation is to ship my Java application to systems that provides their own security library BouncyCastle (e.g. Debian Linux), instead of embeding an unsigned copy of that security library.
I don't know how excludes and includes works, but I would expect that these configurations work on a class file level, because that what the jar is working on.
It's not working on jars.
I would go for this solution:
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': version,
'Main-Class': 'com.alphawallet.scripttool.Main'
}
baseName = project.name + '-all'
configurations.compile.findAll { file ->
// file is of type java.io.File
// when true, jar file is unziped and added
file.name != "bcprov-jdk15on-1.62.jar"
}.sort { it.name }
.collect { file ->
logger.info("Including file: ${file.name}")
file.isDirectory() ? file : zipTree(file)
}
with jar
}
Gradle war plugin: how to build a jar and add it to war?
projectRoot/
src/main/java
src/main/resources
src/main/webapp
build a jar (foo.jar) from the java source code and resources.
add the jar under the WEB-INF/lib of the war.
WEB-INF/lib/foo.jar
The war task will not build a jar by default, and add all java classes and resources under WEB-INF/classes.
UPDATE
The War plugin extends the Java plugin to add support for assembling web
application WAR files. It disables the default JAR archive generation of the
Java plugin and adds a default WAR archive task.
There is a way to enable the Jar generation and let task war depends on task jar?
not sure if eastwater still needs the answer, hope others with the same problem will find this helpful
you can add/configure the war task in build.gradle
war {
classpath = classpath - sourceSets.main.output
from (jar) {
into 'WEB-INF/lib'
}
}
once build succeed, in build/libs folder you'll see the generated jar and the war containing the generated jar instead of classes
Add those into your root build.gradle
plugins {
id 'java'
id 'idea'
}
jar{
String somestr=''
configurations.runtime.each{somestr=somestr+" lib\\"+it.name}
manifest{
attributes 'Main-Class':'your_class_name'
attributes 'Class-Path':somestr
}
}
task copyJar(type:Copy){
from configurations.runtime
into ('build/libs/lib')
}
task release(type: Copy,dependsOn:[build,copyJar]){
}
add finally run this command
gradle release
I am new to Gradle and to shadow jar (Gradle version of Maven's Shade plugin). I am building a fat jar, in which I want to merge service files (that's why I am using shadow jar in the first place).
According to the documentation shadowJar task inherits from gradle Jar task. So, one would assume that it will work exactly as a jar task.
Here is the snippet of the jar task:
jar {
zip64 true
from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
}
As a result, it produces a fat jar with all the dependencies exploded, what is anticipated. When I change task name from jar to shadowJar, like below:
shadowJar {
zip64 true
from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
}
I get a jar file that contains only files from the current project, dependencies are excluded. Why is this happening?
You don't need to define the from... part. The plugin will include all dependencies automatically.
I'm trying to add a task (gen or gen2) to my build.gradle that does exactly the same as the Jar-task:
version = "0.0.1"
apply plugin: 'java'
task('gen', type: Jar) {
}
task gen2(type: Jar)
Running
gradle jar
generates a JAR-file that contains .class-files, while running
gradle gen
or
gradle gen2
generate a JAR-file that does NOT contain any .class-files.
Whats wrong with my class definition?
To build a jar with all the classes from main, as a default jar task would, do this:
task gen2(type: Jar){
baseName = 'gen2Jar'
from sourceSets.main.output
}
You can also do from(sourceSets.main.output){ include "package" } to customize what packages are included.
Alternatively, to copy settings from the default jar task:
task gen(type: Jar){
baseName = 'genJar'
with jar
}
Infact you can have both of these in the same build.gradle. Running gradle jar builds default jar. gradle gen builds genJar.jar and gradle gen2 builds gen2Jar.jar, all of which contain all the classes from java.main