How Do I Change The Gradle Output Directories? - gradle

Maven puts its output in "target". Gradle puts its output in "build".
How can I make gradle put its output in "target" too?
The output of the war plugin is "build/libs/project.war"; how can I make it "target/project.war"?

Have a look at the javadocs or the userguide. There's a method setBuildDir. You need to pass a path where you want artifacts to be placed.
e.g.
// Use maven output dir
setBuildDir 'target'
Wicket is an example of a project that does this.

Related

Gradle: Include dynamically generated file in build?

During my gradle build, I generate a temporary buildinfo.properties file containing things like Git commit info, build time, etc. I would like to include this file in my output *.jar / *.war files as a resource. However, I do not want to put this file in my project src/ folder (this would require fiddling with .gitignore and in general it just seems unnecessary to me). Ideally, the developer shouldn't even see this file at all, it should just be contained in the output archive.
How would you include a dynamically generated text file in a Gradle build?
Add that file in a jar task (Kotin DSL):
tasks {
val jar by getting(Jar::class) {
from("build/buildinfo.properties")
}
}
It will add build/buildinfo.properties file (assuming you generate it there with another taks) to the root of your JAR.
For dynamically generated file, standard gradle way to process resources is the gradle task called processResources. You can do something like this:
processResources {
dependsOn taskThatGeneratesYourBuildinfo
from("build/buildinfo.properties") {
into("desired/path/in/jar")
}
}

How can I list all transitive dependencies of a multi-project build

I have a multi-project build with a lot of sub-projects.
How can I list all transitive dependencies of the multi-project build.
Ideally, this would be possible with some command line argument, i.e. without modifying any gradle files, and list a nice dependency tree on the command line.
One solution which does modify the parent build.gradle (but minimally) is from this blogpost:
subprojects {
task allDeps(type: DependencyReportTask) {}
}
Since my parent build.gradle has a subprojects section already, this is a one-liner. Furthermore, I can restrict the output by specifying a configuration. Hence this is an acceptable solution, so a command line option would be nicer.
You can use -I (or --init-script) at command line to specify an Init Script
You can then add whatever tasks etc you like in the init script without altering build.gradle

Gradle Plugin output name

How could I set output name when Gradle builds custom plugin artifact?
I mean when I create a custom plugin and build it Gradle puts plugin-name-1.0.0.jar into build/libs folder.
By default it gets final .jar name from the project folder name.
But I'd like to set it a custom name.
Thanks.
try this :
jar {
baseName = 'custom-name'
}
You will get custom-name-1.0.0.jar

How to override Gradle `rootProject.name` from the command line?

I'm writing a test that does a build and publish to Artifactory. Since I don't want the test to fail if it's run concurrently (eg by separate build jobs or developers), I'd like to override rootProject.name. Can this be done from the command line? I've tried -ProotProject.name=${module} and -Pproject.archivesBaseName=${module} but they're not working (the latter does have some effect, but the artifact is still published with the rootProject.name setting in settings.gradle).
You'll have to script settings.gradle. For example:
rootProject.name = System.getProperty("rootProjectName")
Now you can run with gradle build -DrootProjectName=foo.
The following is a slightly simpler version when you need the default behavior that just passes through the default when it's not being overwritten.
rootProject.name = System.getProperty('rootProjectName') ?: rootProject.name

Including a second jar file that's not a dependency into a fat onejar

I have a project that only builds the fat onejar file for testing purposes. Thus, there's a separate testing class that I don't want as a dependency to the main source, but I do want it included into the onejar. Odd scenario, I know.
I'm using the com.smokejumperit.gradle.OneJarPlugin plugin (source here), and clearly it gets the list of files to include in the onejar here:
project.task('oneJar', dependsOn:[project.tasks.jar, project.tasks.typedefOneJar]) {
...
inputs.files([jar.outputs.files, project.configurations.getByName("compile"), project.configurations.getByName("runtime")])
jar.output.files is used for publishing, so I don't want a this second jar file being published, and the two project.configurations would define dependencies for the main source jar, and I don't want this second jar to be a dependency of that either.
This second jar file is built with a task:
task integrationJar(type: Jar) {
from sourceSets.integrationTest.output
classifier = 'integration'
}
... so I can access the resulting FileCollection via integrationJar.outputs.files. If there was a clear way to add that to oneJar.input.files, I'd be golden, but I can't figure out how to do that. I've tried something like this:
oneJar {
dependsOn 'integrationJar'
println integrationJar.outputs.files.getAsPath()
inputs.files.add(integrationJar.outputs.files)
println inputs.files.getAsPath()
}
... but the result for the last print is still missing the integration jar file.
Ideas?
I'm not familiar with the implementation of that plugin, but I'd be surprised if inputs.files determined what gets included. (Usually, inputs is just consumed by Gradle's up-to-date check.) I recommend to try the gradle-one-jar plugin, which appears to be more flexible.

Resources