Extract all files from dependency - gradle

I'm trying to build a gradle task which extracts all files in a dependency, so that I can modify them using bytecode enhancement and repackage them to a custom jar.
How can I extract those files to my classes folder?

Interesting question. So lets split this up,
If you want the dependencies of a project as a File collection, Look at configurations for Gradle. For example, using the java plugin gives you the configurations, configurations.compile and configurations.runtime. See configurations
You can loop through collections using a each closure
You can see the contents of an archive using the zipTree method Example 1 Reference
You may also unzip an archive with the ant support within gradle, e.g.
ant.unzip(src: war.archivePath, dest: destFile)
You can finally build a custom jar using the Jar task type Jar Task

Related

How to build gradle project before creating a distribution?

I have a gradle 6.x project, where I use the distribution plugin to create .zip distribution.
https://docs.gradle.org/6.9/userguide/distribution_plugin.html
I use the following command to create a ZIP dist:
gradle distZip
However, I have some jar files that I need to put inside a distribution, and when only running this command, it creates a basically empty zip file, because these jars would be located at /build/... .
How can I tell the distZip task, to build these jars before packaging, preferably without running tests?
Thanks!

How to include only specific files from the main/java to be part of jar to be published to using gradle?

I don't want the whole main/java to be pushed as Jar
Instead, I want only specific folders inside main/java to be part of the jar.
How do I achieve it?
Actually, this part of your build.gradle has nothing to do with creating the JAR file. It just defines a publication that refers to the Java component built by your Java project. One part of this Java project is the task jar, which actually creates the JAR file. This task is of type Jar, so it offers methods to either exclude files following a pattern or to only include files following a pattern:
jar {
include '**/path/to/included/files/*.class'
// alternatively
exclude '**/path/to/excluded/files/*.class'
}

Include the bytecode of a JAR file in a gradle library vs. just adding it as a dependency

If I add a JAR file to a gradle project's depenencies, the code compiles just fine, but after publishing to maven (publishToMavenLocal), the classes from the JAR are not found.
Obviously, this is because the jar is added as a "dependency" and not part of the project itself. Is there a way to get the contents of the JAR file to merge into the library? Do I need to make a separate maven repo for each JAR?
You can always try to create a fat jar which includes dependencies. You can follow the instructions provided here https://www.baeldung.com/gradle-fat-jar

Gradle > How to deploy a JAR with only its needed dependencies jars to a remote computer

I am a new Gradle user. My task is to build a Gradle based project and then copy the final jar along with its dependencies jars to a remote host.
My approach is to do a local build, and then copy dependencies jars from caches to a local directory with a gradle task, which looks like:
task copyDependencies(type: Copy) {
from configurations.runtime
into "build/libs/deps"
}
But this task copies all the dependencies, say including transitive dependencies to the target directory; moreover, the runtime group include compile dependencies by default. So I would like to exclude the transitive dependencies, which takes great amount of space and the compile time dependencies jars. Is there any solution?
At last I plan to copy the generated jar and dependencies jars to a remote host by means of ssh, but I guess there should a way to directly finish this by means of a gradle task
Faithfully hope someone can give some useful hint
you need to create a fatJar, you can find an example on how to do it here:
Building a uberjar with Gradle
For copying to a remote directory you can get an example using an ant task here :How to copy directory via scp within gradle task?
Or alternatively you can use an Exec task which will an external tool (scp for example) : https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Exec.html
Hope this helps

How can a Gradle plugin access its own files?

My custom Gradle plugin contains files which it wants to copy into the output produced by the build that uses my plugin.
How can my plugin access its own files?
All variables I have tried always refer to the project that applied my plugin, but not to the contents of the plugin.
It seems that if files are located in the jar itself it can be loaded via the following construct:
getClass().getResource("resourceName")

Resources