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

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 { ... }.

Related

Multiple Gradle Files in the same project using apply from

I'm trying to segregate the gradle tasks to respective gradle files.
build.gradle
plugins {
id 'org.openapi.generator' version '4.3.1'
}
apply from: "$projectDir/gradle/script/openapi.gradle"
openapi.gradle
task buildSampleClient(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
generatorName = "spring"
inputSpec = "$rootDir/src/main/resources/sample.yaml".toString()
outputDir = "$buildDir/generated".toString()
modelPackage = "com.sample"
}
When gradle build is run, getting this error
A problem occurred evaluating script.
Could not get unknown property 'org' for root project 'sample' of type org.gradle.api.Project.
But If I move the content of openapi.gradle into build.gradle it works fine.
Not sure what is the issue, could anyone help here please?
You should add plugin dependencies before your task definition to your openapi.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.openapitools:openapi-generator-gradle-plugin:${openapiPluginDependencyVersion}"
}
}
apply plugin: "org.openapi.generator"
// your task goes here
task buildSampleClient(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
...
}
gradle.properties:
openapiPluginDependencyVersion=4.3.0

gradle - Add custom configuration to installDist for a specific task

I'm trying to make a custom runtime dependency configuration, so that the specified dependencies will only be installed for a specific task. The dependencies are installed using the installDist task. So it seems like I need the configuration to be added to the runtimeClasspath for one task and not the other. I'm thinking I need a custom distribution, but I'm not sure how to set that to have a different runtimeClasspath.
In the example below, I want the run2 task to have the myRuntimeDep dependencies installed, but for the run1 task I do not.
I've be struggling to figure this out all day, does someone know what I'm missing?
Example build.gradle:
configurations {
myRuntimeDep.extendsFrom runtimeOnly
}
dependencies {
...
myRuntimeDep 'the:dependency:1.0'
}
task run1(type: JavaExec, dependsOn: installDist) {
// does not need myRuntimeDep dependencies
}
task run2(type: JavaExec, dependsOn: installDist) {
// needs myRuntimeDep dependencies
}
So after a long weekend, I sort of got something working. Maybe someone can tell me if there's a better way? Also, it doesn't fully work because it doesn't follow transitive dependencies with the configuration (which is kind of a pain because all sub-dependencies need to be manually added).
Solution:
top-level build.gradle
...
subprojects {
configurations {
fooRuntime.extendsFrom runtimeOnly
fooClasspath.extendsFrom runtimeClasspath, fooRuntime
}
distributions {
foo {
contents {
from installDist.destinationDir
from(configurations.fooClasspath) {
into 'lib'
}
}
}
}
installFooDist.dependsOn installDist
}
project A build.gradle
dependencies {
fooRuntime project(':projectB')
fooRuntime project(':projectC') // only need this because transitive dependencies won't work
}
task run(type: JavaExec, dependsOn: installFooDist) {
classpath = fileTree("$installFooDist.destinationDir/lib")
}
project B build.gradle
dependencies {
fooRuntime project(':projectC')
}
task run(type: JavaExec, dependsOn: installFooDist) {
classpath = fileTree("$installFooDist.destinationDir/lib")
}

Gradle: Distributing Executable, Obfuscated Jar File

I'm trying to use gradle with proguard to obfuscate the code then generate a zip file to distribute. I'd like to use the distribution plugin, but it always includes the jar that is generated by the jar task. Is there some way to force the distribution plugin to omit the original (non-obfuscated) jar and only include the obfuscated jar? I can easily add the obfuscated jar in addition to the original, but I want to distribute the obfuscated jar instead of the original so the generated execution scripts run against the obfuscated version.
Here's my abridged build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'net.sf.proguard:proguard-gradle:5.3.3'
}
}
apply plugin: 'java'
apply plugin: 'application'
task obfuscate(type: proguard.gradle.ProGuardTask) {
configuration 'proguard.txt'
injars "build/libs/${rootProject.name}.jar"
outjars "build/libs/${rootProject.name}-release.jar"
}
jar.finalizedBy(project.tasks.obfuscate)
distributions {
main {
contents {
from(obfuscate) {
into "lib"
}
from(jar) {
exclude "*.jar"
}
}
}
}
I've tried a number of things in the distributions block to try to exclude the original jar, but nothing seems to work.
Any ideas would be greatly appreciated.
This isn't the best solution, but I was able to work around the issue by renaming the jars at the end of the obfuscation step. Now, I name the original jar something like <JAR_NAME>-original.jar and I give the obfuscated jar the original jar's name. I still wish there was a better way to do it, but this seems to work.
Here is the updated, abridged build.gradle file:
import java.nio.file.Paths
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'net.sf.proguard:proguard-gradle:5.3.3'
}
}
apply plugin: 'java'
apply plugin: 'application'
def jarNameWithoutExtension = jar.archiveName.with { it.take(it.lastIndexOf(".")) }
def obfuscatedJarName = "${jarNameWithoutExtension}-release.jar"
def jarFileLocation = jar.archivePath.parent
def obfuscatedFilePath = Paths.get(jarFileLocation, obfuscatedJarName)
task obfuscate(type: proguard.gradle.ProGuardTask) {
configuration 'proguard.txt'
injars jar.archivePath
outjars obfuscatedFilePath.toString()
// Rename the original and obfuscated jars. We want the obfuscated jar to
// have the original jar's name so it will get included in the distributable
// package (generated by installDist / distZip / distTar / assembleDist).
doLast {
jar.archivePath.renameTo(Paths.get(jarFileLocation, "$jarNameWithoutExtension-original.jar").toFile())
obfuscatedFilePath.toFile().renameTo(jar.archivePath)
}
}
jar.finalizedBy(project.tasks.obfuscate)

Control the gradle task execute order

I have a strange problem about gradle task recently.
Assume I have a simple gradle config as follows
apply plugin: "java"
apply plugin: "maven"
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.diffplug.gradle.spotless:spotless:2.0.0"
}
}
apply plugin: "com.diffplug.gradle.spotless"
spotless {
java {
eclipseFormatFile 'format.xml' // XML file dumped out by the Eclipse formatter
}
}
spotlessJavaCheck.dependsOn(processResources)
version = '1.0-SNAPSHOT'
I just want to set the depends on relationship for the spotless check. After I run a build, the error looks like this
> Could not find property 'spotlessJavaCheck' on root project 'gradle-helloworld'.
I have done something similar with other plugins, it works well, but not for this spotless plugin.
Br,
Tim
Spotless Gradle plugin does magic at configuration time.
You need to set the dependency after evaluation time, once the magic is done:
afterEvaluate {
tasks['spotlessJavaCheck'].dependsOn processResources
}

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.

Resources