Is there a plugin like shadow for gradle 4.4 (fatJar) - gradle

I am trying to make a fatJar and then move it. The shadow plug fails on me saying it cannot find the plugin. I am trying to create a fatjar.
I have this which moves the file just fine but it includes EVERY single jar file, I just want one big one and then move it:
task copyJarIntoDockerDirectory(type: Copy) {
dependsOn jar
from configurations.runtime
from jar
into '../docker/ekomi-import'
rename '(.*)-[0-9]+\\..*.jar', '$1.jar'
doFirst { println 'before copy' }
doLast { println 'after copy' }
}

You can use com.github.jengelman.gradle.plugins:shadow:2.0.2 plugin with Gradle 3.0+. Your minimal build.gradle file could look like this:
buildscript {
repositories {
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.github.jengelman.gradle.plugins:shadow:2.0.2"
}
}
apply plugin: "com.github.johnrengelman.shadow"
task copyJarIntoDockerDirectory(type: Copy) {
dependsOn shadowJar
from 'build/libs/'
into 'docker/ekomi-import'
}
In my case final JAR is called products-service-all.jar. When I run:
gradle clean copyJarIntoDockerDirectory && tree docker
I get following output in bash console:
products-service [master●●●] % gradle clean copyJarIntoDockerDirectory && tree docker
BUILD SUCCESSFUL in 3s
5 actionable tasks: 5 executed
docker
└── ekomi-import
└── products-service-all.jar
1 directory, 1 file
Hope it helps.

Related

How to collect javadoc jars into a zip file

For a multi-project Gradle project, foo, bar and baz. I'm trying to create a task which creates a zip file with both libraries and javadoc, i.e. foo.jar, AND foo-javadoc.jar..
./build.gradle
./settings.gradle
./foo/build.gradle
./bar/build.gradle
./baz/build.gradle
settings.gradle
include ":foo"
include ":bar"
include ":baz"
Top level build
allprojects {
apply plugin: 'java'
task generateJavadoc (type : Javadoc) {
source = sourceSets.main.allJava
classpath = sourceSets.main.compileClasspath
failOnError = false
}
task javadocJar(type: Jar, dependsOn: generateJavadoc) {
classifier = 'javadoc'
from generateJavadoc.destinationDir
}
artifacts {
archives javadocJar
}
}
task buildZip(type: Zip, dependsOn: build) {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
from (project(':foo').configurations.runtime) {
into 'jars'
}
from (project (':foo').configurations.archives.all) {
into 'jars'
}
}
When I invoke this with gradle clean buildZip a zip file is created, but without the any -javadoc JARs I was expecting... The JavaDoc jars are generated into the project build directories, e.g. foo/build/lib/foo-javadoc.jar I've tried multiple combinations of from project (':foo').artifacts etc.
This is possible using the following. Note javadocJar is the name of the task defined in the allprojects block
from (rootProject.allprojects.javadocJar.outputs) {
into 'javadoc'
}

Extracting files downloaded as a dependency in Gradle

I've got a gradle script that goes something like the following:
apply plugin: 'java'
apply plugin: 'maven'
defaultTasks 'build'
ext.basedir = file('.').getAbsolutePath()
repositories{
maven { url "http://package.repo.com:8081/nexus/content/repository
}
configurations.all {
// check for updates every build
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
dependencies {
compile group:'com.repo.wwpd', name:'kernel_utilities', version:'3.0.0', changing:true
}
task copy_dependencies(type: Copy) {
from configurations.compile
into basedir+'\\install'
include '*'
{ FileTree ioTree = fileTree(dir: "C:\\Users\\username\\install") }
ioTree.each { f ->
copy {
from tarTree(resources.gzip(f))
into "C:\\Users\\user\\test"
}
}
}
The goal is to get the dependencies, move them to the install folder, and extract them from the tar files to the test folder.
The problem appears to be that the task is executed before the dependencies are downloaded. So if the files already exist in install it works fine, but if the install folder is empty the result is an empty test folder but a full install folder.
[EDIT - updated with comments Peter N.]
This should be one way to solve your case ; note it contains two tasks, choose the one(s) that fulfill your needs: simple copy VS full extraction
def installDir = "${buildDir}/install"
def extractDir = "${buildDir}/extract"
// task to copy dependencies
task copyDependencies(type: Copy) {
from configurations.compile
into installDir
}
// task to extract dependencies
task extractDependencies(type: Copy) {
from configurations.compile.collect{tarTree (it)}
into extractDir
}

Gradle war ignores transitive dependencies when using 'configurations.runtime.asPath' in custom task

I'm facing behavior that I can't explain, using gradle 1.10 I have:
settings.gradle:
include('lib1', 'lib2', 'web')
build.gradle:
subprojects {
apply plugin: 'java'
}
project(':web') {
apply plugin: 'war'
dependencies {
compile project(':lib1')
}
task myTask(type: JavaExec, dependsOn: 'compileJava') {
main = "some.thirdparty.Class"
args "--searchPath", configurations.runtime.asPath
}
}
project(':lib1') {
dependencies {
compile project(':lib2')
}
}
project(':lib2') {
}
When I run gradle clean war I only have lib1.jar in war/build/libs/web.war/WEB-INF/lib.
To make WEB-INF/lib contain both lib1.jar and lib2.jar I have to:
move project('web') block to the end of the file
update configurations.runtime.asPath to configurations.runtime (but I need to provide class path as a path, so it is not a solution)
I read the build lifecycle description, tried to compare --debug outputs but that didn't help.
Why is this happening? And what would be a good solution to provide the module runtime class path as a path in JavaExec task please?
asPath resolves the configuration, but resolution will only work correctly if it happens at execution time rather than configuration time (in particular in the presence of project dependencies). Try to wrap the args line with doFirst { ... }.

How to refactor uploadArchives for some subprojects at root build.gradle?

I have a multi project, and I want to upload some of the subprojects artifact to the maven repository.
For now, I wrote the following code into the main build.gradle:
task sourcesJar(type: Jar, dependsOn: classes) { ... }
project(':subProjName1') {
apply plugin: 'java'
apply plugin: 'maven'
configurations {
subProjName1Archives
}
uploadSubProjName1Archives {
repositories {
mavenDeployer {
repository(url: "file:///$rootDir/mvn-repo/")
}
}
}
artifacts {
subProjName1Archives jar
subProjName1Archives sourcesJar
}
}
project(':subProjName2') { ... }
...
project(':subProjNameN') { ... }
And do following for upload archives:
gradlew.bat uploadSubProjName1Archives
gradlew.bat uploadSubProjName2Archives
...
gradlew.bat uploadSubProjNameNArchives
It's doing what I want, but how can I generalize it into one task in the main build.gradle?
If you put the above code into a subprojects { .. } block in the root build script, you can invoke all tasks at once with gradle uploadMyConfiguration. (Only) if you have a concrete need for a single task (e.g. because another task depends on all artifacts being uploaded), you can add a further lifecycle task:
task uploadAll {
dependsOn { subprojects.uploadMyConfiguration }
}
PS: Unless you have a good reason not to, you can reuse the existing archives configuration and uploadArchives task. The archives configuration already contains the Jar produced by the jar task, so you just have to add the sources Jar.

How to generate multiple jar files with gradle's java plugin

I have a multi-project gradle build using the java plugin setup as follows:
myProj/
settings.gradle
build.gradle
util/
build.gradle
In my util project, I would like to generate 2 jars... one for packageA and one for packageB. I'm a noob with gradle so any help here would be much appreciated. Here are my settings and gradle files:
myProj/settings.gradle
include 'util'
myProj/build.gradle
subprojects {
apply plugin: 'java'
repositories {
maven {
url "http://mymavenurl"
}
}
sourceSets {
main {
java {
srcDir 'src/java'
}
}
}
}
myProj/util/build.gradle
dependencies {
.
.
.
}
jar {
baseName = 'packageA'
includes = ['com/mycomp/packageA']
}
task packageBJar(type: Jar) {
dependsOn classes
includes = ['com/mycomp/packageB']
baseName = 'packageB'
}
When I try to build my project here is the output:
:util:compileJava
:util:processResources UP-TO-DATE
:util:classes
:util:jar
:util:assemble
:util:compileTestJava UP-TO-DATE
:util:processTestResources UP-TO-DATE
:util:testClasses UP-TO-DATE
:util:test
:util:check
:util:build
I would hope to see :util:packageBJar after classes, but I'm not having any luck.
One way is to declare packageBJar as an artifact of, say, the archives configuration:
artifacts {
archives packageBJar
}
Now gradle assemble, and therefore also gradle build, will produce packageBJar.

Resources