how to copy directory & contents of directory with parent directory structure at root level in generated war file using gradle script - gradle

Hi I am newbie to gradle script, I am trying to copy directory to root directory of war file, but I ended up copying only the contents of the folder to root directory, but I want the whole directory structure with parent directory also to be copied.
Folder structure to copy
polymer-client
├── file1.txt
├── index.html
Gradle script
apply plugin: 'war'
war {
archiveName = 'WebDeployment.war'
from 'polymer-client'
}
Generated folder structure
WebDeployment
├── WEB-INF
├── META-INF
├── file1.txt
├── index.html
Expected folder structure
WebDeployment
├── WEB-INF
├── META-INF
├── polymer-client
├── file1.txt
├── index.html
Please let me know, how to get it done with gradle script.

You can try to do it with into as follows:
apply plugin: 'war'
war {
archiveName = 'WebDeployment.war'
into 'polymer-client', {
from 'polymer-client'
}
}
It should create the polymer-client subdirectory within war-archive and copy all the content of the polymer-client directory into it.

Related

gradle could not find other gradle script

/--common
/--common/build.gradle
/--common/deploy.gradle
/--project1
/--project1/build.gradle
I have a multi-project structure and have extracted repeating code from my build.gradle file and placed this into another file deploy.gradle.
I have placed the deploy.gradle file into the common project at the same folder level as the build.gradle file. The folder structure is shown above.
In the build.gradle file of the common project I can reference the file using the statement,
apply from: 'deploy.gradle'
This works like a dream and the common project build works perfectly calling in the tasks from the deploy.gradle file.
The problem comes when I try to reference deploy.gradle file from one of the other projects. When I add the apply... statement to the build.gradle of project1 I get the compilation error,
Error:(23, 0) Could not read script
'C:\path-to-project1-script-file\deploy.gradle' as it does not exist.
So Gradle is looking for the deploy.gradle file in project1 only even though I have a dependency set to the common project in the project1 build.gradle file.
Question is how can I make deploy.gradle from common project visible to project1.
We successfully use the following project layout
├── a
│   └── build.gradle
├── b
│   └── build.gradle
├── build.gradle
├── gradle-scripts
│   └── deploy.gradle
└── settings.gradle
The rootproject's build.gradle defines
ext.gradleScript = { scriptName ->
file("${rootProject.projectDir}/gradle-scripts/${scriptName}.gradle")
}
Subprojects use the scripts within gradle-scripts this way
apply from: gradleScript('deploy')
Whole content of the project:
$> find . -type f | while read file; do echo "--- $file ---" && cat $file; done
--- ./a/build.gradle ---
apply from: gradleScript('deploy')
--- ./b/build.gradle ---
apply from: gradleScript('deploy')
--- ./build.gradle ---
// Where common build logic is found
ext.gradleScript = { scriptName ->
file("${rootProject.projectDir}/gradle-scripts/${scriptName}.gradle")
}
--- ./gradle-scripts/deploy.gradle ---
task myDeployTask {
doLast { println 'common deploy logic goes here' }
}
--- ./settings.gradle ---
include 'a', 'b'
$> gradle -q b:myDeployTask
common deploy logic goes here
$>
Here is an example project1/build.gradle that references common/deploy.gradle:
// import myDeploy task
apply from: "${rootDir}/common/deploy.gradle"
task myProject1Task(dependsOn: 'myDeploy') {
doLast {
println 'TRACER myProject1Task'
}
}
It is often important to distinguish projectDir from rootDir in multi-project builds. projectDir is the specific subproject; rootDir is where settings.gradle lives.

How to include a Gradle distribution in the project to be used for the wrapper

I want to include a specific version of Gradle in the project folder so that when I use the Gradle wrapper it doesn't download it from the remote repository.
I downloaded the version of Gradle I need (gradle-4.0-bin.zip) and I put that zip fine inside of gradle/wrapper/ folder of the project (created with the gradle wrapper command).
Then I edited the gradle-wrapper.properties file in this way:
distributionUrl=file:///Users/pathj/to/the/project/gradle/wrapper/gradle-4.0-bin.zip
But when I run the first command, such as gradle task it returns:
What went wrong: A problem occurred configuring root project '03-gradle-wrapper-local'.
java.io.FileNotFoundException: /Users/myself/.gradle/wrapper/dists/gradle-4.0-bin/3p92xsbhik5vmig8i90n16yxc/gradle-4.0/lib/plugins/gradle-diagnostics-4.0.jar
(No such file or directory)
How do I tell Gradle to get the zip file from the current project folder, with a relative path, instead of downloading it, and to use that zip file to create a wrapper to be used in my builds?
Apart from storing gradle wrapper locally make sense or not it is possible. I assume that gradle-4.0-rc-3-bin distro is used.
Here is the project structure:
.
├── gradle
│   └── wrapper
│   ├── gradle-4.0-rc-3-bin.zip
│   ├── gradle-wrapper.jar
│   └── gradle-wrapper.properties
├── gradlew
└── gradlew.bat
And here the content of gradle-wrapper.properties:
distributionBase=PROJECT
distributionPath=gradle
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=gradle-4.0-rc-3-bin.zip
Since wrapper files will be downloaded to the project dir adding gradle/gradle-4.0-rc-3-bin to SCM ignore file is recommended.
Demo can be found here.

Delete subdirectory excluding files in gradle

I have a directory, lets say DirA. There is a subdirectory called DirB and some files in DirA. Now, when I use the Gradle task to delete only the subdirectory (DirB), it is deleting the files also. I don't want it to delete the files. Can anyone please help me achieve this?
task deleteFolder(type: Delete){
delete "full path of subdirectory relative to build.gradle location"
}
With the following structure:
.
└── dira
├── a
├── b
├── c
└── dirb
and script:
task clearDirA(type: Delete) {
delete project.file('dira/dirb')
}

How to copy file and rename depending on subfolder

I am trying to copy a file in gradle and rename it at the same time, where the new name of the file should be dependent of the name of the subfolder it was in.
My file structure looks like this for example:
├── apk
│   └── app-envTest-normalBuild-release.apk
├── logs
│   └── manifest-merger-envTest-normalBuild-release-report.txt
└── mapping
└── envTestNormalBuild
└── release
├── dump.txt
├── mapping.txt
├── seeds.txt
└── usage.txt
I'd like to copy the mapping.txt and rename it. The new name should be mapping-envTestNormalBuild.txt - i.e. should include the name of the subdirectory it was released in.
Can someone tell me if/how this is possible using the gradle copy+rename feature?
I have seen that the reverse is possible, i.e. copying a file like foo-bar.txt to a directory/path foo/bar.txt and similar.
You can try e.g.:
task clean {
doLast {
project.file('dest').deleteDir()
}
}
task cp(type: Copy) {
dependsOn clean
from 'mapping/envTestNormalBuild/release'
into 'dest'
include 'mapping.txt'
eachFile { fcp ->
fcp.name = "$fcp.file.parentFile.parentFile.name-$fcp.name"
}
}
A demo can be found here.

How to set a flattened test/source folder structure?

Using gradle for a Java+Groovy+JUnit project, I get this source folder structure by default:
prjroot-src
├── main
│ ├── groovy
│ └── java
└── test
├── groovy
└── java
I wanted to have Java and Groovy sources in one folder, so I set the sourceSets like this:
sourceSets.main.java.srcDirs = []
sourceSets.main.groovy.srcDirs += ["src/main/java"]
Which resulted in the same directory structure with the difference that I could put my .groovy files in the java folder. Also, the unused Groovy folder still is there.
I'd like a much flatter, cleaner directory structure that looks like this:
prjroot
├── src
└── test
The packages, .groovy and .java files should be directly under src, and the test sources accordingly under test.
How can I achieve this using gradle?
Replace your sourceSets code with:
sourceSets.main.groovy.srcDirs = ["src"]
sourceSets.test.groovy.srcDirs = ["test"]

Resources