How to exclude urlrewriterules.xml and domainsplittings.xml from being copied to config dir on serverDeploy task - url-rewriting

We are trying to put our custom domainsplittings.xml and urlrewriterules.xml files wit our url rewrite rules to share/system/config/cluster folder, but every time we run deployServer gradle task, those files are copied from bc_urlrewrite.zip from local gradle repo.
We already tried to define custom deployment/deploy.gradle file in one of our cartridges with following code:
project(':bc_urlrewrite') {
afterEvaluate {
deployment.files.share {
exclude {
new File(destinationDir, it.path) == new File(target.shareDirectory, 'system/config/cluster/domainsplittings.xml')
new File(destinationDir, it.path) == new File(target.shareDirectory, 'system/config/cluster/urlrewriterules.xml')
}
}
}
}
as stated here: https://support.intershop.com/kb/index.php/Display/282B92#Cookbook-DeploymentToolsICM7.9-Recipe:ReplaceaFileDeployedbyAnotherComponent but this does not work. Files are still copied from bc_urlrewrite.zip on deployServer task.
Are we doing something wrong? We don't need those files because they contains url rewrite rules for demo intronics store.
Thank you for your help!

Yeah, the documentation isn't very clear and it use to be that you could simple overload the setting. Can you try the following configuration.
apply plugin: com.intershop.deploy.cartridge.CartridgeDeploymentPlugin
if (target.includeShare && findProject(':bc_urlrewrite')) {
project(':bc_urlrewrite') {
def excludeFiles = {
deployment.files.share {
exclude 'system/config/cluster/urlrewriterules.xml'
exclude 'system/config/cluster/domainsplittings.xml'
}
}
if (project.state.executed) {
excludeFiles()
} else {
afterEvaluate(excludeFiles)
}
}
}

Related

Weird behaviour of a simple gradle copy task

I am completely baffled by the gradle behaviour of a Copy task into multiple directories.
I intend to copy all files from src/common into
target/dir1/ps_modules
target/dir2/ps_modules
target/dir3/ps_modules
Below is how my build.gradle looks:
project.ext.dirs = ["dir1", "dir2", "dir3"]
// ensures that ps_modules directory is created before copying
def ensurePsModulesDir() {
dirs.each {
def psModules = file("target/$it/ps_modules")
if (!(psModules.exists())) {
println("Creating ps_modules directory $psModules as it doesn't exist yet")
mkdir psModules
}
}
}
task copyCommons(type: Copy) {
doFirst {
ensurePsModulesDir()
}
from("src/common")
dirs.each {
into "target/$it/ps_modules"
}
}
The result of running the command ./gradlew copyCommons is completely weird.
The folder creation works as expected, however, the contents/files are copied only in the target/dir3/ps_modules directory. The rest two directories remain empty.
Any help would be appreciated.
Below is the screen grab of target directory tree once the job is run:
I think you want to do something like:
task copyCommons(type: Copy) {
dirs.each {
with copySpec {
from "src/common"
into "target/$it/ps_modules"
}
}
}
I think you can get rid of the ensurePsModulesDir() with this change
* edit *
it seems that the copy task is forcing us to set a destination dir. You might think that setting destinationDir = '.' is ok but it's used in up-to-date checking so likely the task will NEVER be considered up-to-date so will always run. I suggest you use project.copy(...) instead of a Copy task. Eg
task copyCommons {
// setup inputs and outputs manually
inputs.dir "src/common"
dirs.each {
outputs.dir "target/$it/ps_modules"
}
doLast {
dirs.each { dir ->
project.copy {
from "src/common"
into "target/$dir/ps_modules"
}
}
}
}
You can configure a single into for a task of type Copy. In this particular example gradle behaves as expected. Since dir3 is the last element on the list it is finally configured as a destination. Please have a look at this question - which you can find helpful. Also this thread might be helpful as well.

How can I define two different 'distribution' tasks in gradle?

The normal behavior of the distTar and distZip tasks from the application plugin in gradle seems to be to copy the contents of src/dist into the zip and tar files, but I have a subfolder in src/dist that I want to exclude from the default distribution, and include it for a new (extended) task, possibly to be called distZipWithJRE.
I have been able to exclude this folder in the default task as follows:
distributions.main {
contents {
from('build/config/main') {
into('config')
}
from('../src/dist') {
exclude('jre')
}
}
}
How can I define a second task that behaves just like the original (unmodified) task?
Using Gradle 4.8 I had to tweak the answer to use 'with' from CopySpec instead
distributions {
zipWithJRE {
baseName = 'zipWithJRE'
contents {
with distributions.main.contents
}
}
}
It seems that what you're looking for is in the docs. You need to leave current settings as is and for zipWithJRE create and configure custom distribution:
distributions {
zipWithJRE {
baseName = 'zipWithJRE'
contents {
from { distributions.main.contents }
}
}
}

Using both Application and Distribution plugin in gradle

When using the distributions plugin I had the following code in order to set up the folder structure in the distribution output.
Now however I have to use the 'application' plugin.
1. Can these two plugins be used together? (complains about tasks with identical names)
2. If not, how does one implement the code below for the applications plugin?
distributions {
main {
baseName = appName
contents {
into('bin') { from jar.archivePath }
into('lib') { from configurations.runtime }
into('etc') { from project(':server').file('src/main/other') }
}
}
}
After discussion in comments, the following piece of code should help:
applicationDistribution.from(jar.archivePath) {
into "bin"
}
applicationDistribution.from(configurations.runtime ) {
into "lib"
}
applicationDistribution.from(project(':server').file('src/main/other')) {
into "etc"
}
Or (maybe) the shorter form (can't verify it)
with(applicationDistribution) {
from(jar.archivePath) { into "bin" }
from(configurations.runtime ) { into "lib" }
from(project(':server').file('src/main/other')) { into "etc" }
}
As already mentioned: Don't know what is baseName exactly, but suppose it also can be set.

Gradle copy without overwrite

Is there a way to avoid overwriting files, when using task type:Copy?
This is my task:
task unpack1(type:Copy)
{
duplicatesStrategy= DuplicatesStrategy.EXCLUDE
delete(rootDir.getPath()+"/tmp")
from zipTree(rootDir.getPath()+"/app-war/app.war")
into rootDir.getPath()+"/tmp"
duplicatesStrategy= DuplicatesStrategy.EXCLUDE
from rootDir.getPath()+"/tmp"
into "WebContent"
}
I want to avoid to specify all the files using exclude 'file/file*'.
It looks like that duplicatesStrategy= DuplicatesStrategy.EXCLUDE doesn't work. I read about an issue on gradle 0.9 but I'm using Gradle 2.1.
Is this problem still there?
Or am I misunderstanding how this task should be used properly?
Thanks
A further refinement of BugOrFeature's answer. It's using simple strings for the from and into parameters, uses the CopySpec's destinationDir property to resolve the destination file's relative path to a File:
task ensureLocalTestProperties(type: Copy) {
from zipTree('/app-war/app.war')
into 'WebContent'
eachFile {
if (it.relativePath.getFile(destinationDir).exists()) {
it.exclude()
}
}
}
You can always check first if the file exists in the destination directory:
task copyFileIfNotExists << {
if (!file('destination/directory/myFile').exists())
copy {
from("source/directory")
into("destination/directory")
include("myFile")
}
}
Sample based on Peter's comment:
task unpack1(type: Copy) {
def destination = project.file("WebContent")
from rootDir.getPath() + "/tmp"
into destination
eachFile {
if (it.getRelativePath().getFile(destination).exists()) {
it.exclude()
}
}
}

How to keep Java code and Junit tests together building with Gradle

I have a project in which the main source and the test cases for that source are kept in the same package/directory. Each test class is the name of the class which it is testing with "Test" appended on the end. So if I have a Foo.java there will be a FooTest.java right next to it.
My question is, how do I build this project with Gradle? I'd still like to keep the class files separate, i.e. a folder for main classes and a folder for test classes.
This should do the trick:
sourceSets {
main {
java {
srcDirs = ["some/path"]
exclude "**/*Test.java"
}
}
test {
java {
srcDirs = ["some/path"]
include "**/*Test.java"
}
}
}
For reference, here is the code I used to try to get around the Eclipse plugin's classpath issue. Using this in combination with Peter's answer above seems to work.
// The following ensures that Eclipse uses only one src directory
eclipse {
classpath {
file {
//closure executed after .classpath content is loaded from existing file
//and after gradle build information is merged
whenMerged { classpath ->
classpath.entries.removeAll { entry -> entry.kind == 'src'}
def srcEntry = new org.gradle.plugins.ide.eclipse.model.SourceFolder('src', null)
srcEntry.dir = file("$projectDir/src")
classpath.entries.add( srcEntry )
}
}
}
}
this work for me:
eclipse {
classpath {
file {
withXml {
process(it.asNode())
}
}
}
}
def process(node) {
if (node.attribute('path') == 'src/test/java' || node.attribute('path') == 'src/test/resources')
node.attributes().put('output', "build/test-classes")
else
node.children().each {
process(it)
}}

Resources