Using both Application and Distribution plugin in gradle - 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.

Related

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

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)
}
}
}

Using Gradle to build a jar with dependencies with Kotlin-DSL

There is already an answer to the question: how to include all the dependencies in a jar file though it's for Groovy
I'm using gradle with kotlin-dsl and the code is not compatible. I tried to make it work using a few ways including:
tasks.withType<Jar> {
configurations["compileClasspath"].forEach { file: File ->
copy {
from(zipTree(file.absoluteFile))
}
}
}
Though this doesn't work. So how to include the dependencies using kotlin-dsl in gradle?
This will work:
tasks.withType<Jar>() {
configurations["compileClasspath"].forEach { file: File ->
from(zipTree(file.absoluteFile))
}
}
There's no need in copy { ... }, you should call from on the JAR task itself.
Note: Gradle does not allow changing the dependencies after they have been resolved. It means that the block above should be executed only after the dependencies { ... } are configured.
my case
withType<Jar> {
enabled = true
isZip64 = true
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
archiveFileName.set("$project.jar")
from(sourceSets.main.get().output)
dependsOn(configurations.compileClasspath)
from({
configurations.compileClasspath.get().filter {
it.name.endsWith("jar")
}.map { zipTree(it) }
}) {
exclude("META-INF/*.RSA", "META-INF/*.SF", "META-INF/*.DSA")
}
}

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

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)
}}

How to copy to multiple destinations with Gradle copy task?

I am trying to copy one file to multiple destinations through a Gradle task. I found the following in other websites but I get an ERROR while running this task.
def filesToCopy = copySpec{
from 'somefile.jar'
rename {String fileName -> 'anotherfile.jar'}
}
task copyFile(type:Copy) {
with filesToCopy {
into 'dest1/'
}
with filesToCopy {
into 'dest2/'
}
}
ERROR
No signature of method: org.gradle.api.internal.file.copy.CopySpecImpl.call() is applicable for argument types
Is there a way to copy to multiple destinations in one Gradle task?
an alternative way
task myCustomTask << {
copy {
from 'sourcePath/folderA'
into 'targetPath/folderA'
}
copy {
from 'sourcePath/folderB'
into 'targetPath/folderB'
}
copy {
from 'sourcePath/fileA.java','sourcePath/fileB.java'
into 'targetPath/folderC'
}
}
If you really want them in one task, you do something like this:
def filesToCopy = copySpec {
from 'someFile.jar'
rename { 'anotherfile.jar' }
}
task copyFiles << {
['dest1', 'dest2'].each { dest ->
copy {
with filesToCopy
into dest
}
}
}
Here is a general snippet without copySpec for Gradle 4.1. As pointed out the trick is to use a base into and use relative into inside the closures (e.g. from closure).
task multiIntoCopy(type: Copy){
into(projectDir) // copy into relative to this
from("foo"){
into("copied/foo") // will be projectDir/copied/foo
// just standard copy stuff
rename("a.txt", "x.txt")
}
from("bar"){
into("copied/aswell/bar") // projectDir/copied/copied/aswell/bar
}
from("baz") // baz folder content will get copied into projectDir
//from("/bar"){ // this will mess things up, be very careful with the paths
// into("copied/aswell/bar")
//}
}
With a Common Destination Base Path
If your destination paths share a common path prefix (dest_base), then you can use something like this:
def filesToCopy = copySpec {
from 'somefile.jar'
rename { String fileName -> 'anotherfile.jar' }
}
task copyFile(type: Copy) {
into 'dest_base'
into('dest1') {
with filesToCopy
}
into('dest2') {
with filesToCopy
}
}
Compared to other answers which use the copy method, this approach also retains Gradle’s UP-TO-DATE checks.
The above snippet would result in output like this:
dest_base/
├── dest1
│   └── anotherfile.jar
└── dest2
└── anotherfile.jar
no there isn't a way to do that atm. I would create seperate gradle tasks for each target directory
def filesToCopy = copySpec{
from 'somefile.jar'
rename {String fileName -> 'anotherfile.jar'}
}
task copyFileDest1(type:Copy) {
with filesToCopy
into 'dest1/'
}
task filesToCopyDest2(type:Copy) {
with filesToCopy
into 'dest2/'
}
I needed to do this in a particular order to rewrite a particular string on a set of files.
task copyAtoB(dependsOn: [existingTask]) {
doLast {
copy {
from("folder/a") {
include "*.java"
}
// Have to use a new path for modified files
into("folder/b")
filter {
String line ->
line.replaceAll("changeme", "to this")
}
}
}
}
task overwriteFilesInAfromB(dependsOn: [copyAtoB]) {
doLast {
copy {
from("folder/b") {
include "*.java"
}
into("folder/a")
}
}
}
// Finally, delete the files in folder B
task deleteB(type: Delete, dependsOn: overwriteFilesInAfromB) {
delete("folder/b")
}
nextTask.dependsOn(deleteB)

Resources