Question

I have a Gradle multi project.

One of projects is Flex project, which is built using gradleFx plugin.

The second is war project and I need include the SWF file from Flex project into the war.

I add

war { 
  from (project(":flex:FlexManager").buildDir) {into ('flash')}
}

and it does put the SWF into war, but only if the SWF already exists.

Then I want that gradle will build the :flex:FlexManager project during the build of my war. So I add

dependencies {
  ....
  runtime project(path : ':flex:FlexManager')
}

The only way I found to start the flex project build during war build was

war { 
  ...
   dependsOn ':flex:FlexManager:build'
}

but it looks like not the best way - I prefer define project dependencies instead of tasks dependencies

Was it helpful?

Solution

All that you need to change is to specify that you want to copy from a task or task outputs and not a directory - this will register an implicit dependency between your war task and whatever task that builds the swf file.

Said that it is necessary for the task that builds your swf file to specify its outputs. I had a quick look at GradleFx and unfortunately it looks like compileFlex task doesn't do it. So first you will need to specify the outputs for the task the following way:

compileFlex {
    outputs.dir project.buildDir
}

and then modify your war task configuration:

war {
    from project(":flex:FlexManager").compileFlex {
        into ('flash')
    }
}

You should probably also nag at the GradleFx devs to add outputs to their tasks. :)

EDIT:

From the discussion in the comments I've understood that instead of depending on a task you can depend on a project artifact. The cleanest way is to create a custom configuration, add the dependency to it and then use it in the from call when configuring the war task:

configuration {
    flex
}

dependencies {
    flex project(':flex:FlexManager')
}

war {
    from configurations.flex {
        into ('flash')
    }
}

You could probably also use the archives or default configuration of :flex:FlexManager directly:

war {
    from project(':flex:FlexManager').configurations.archives {
        into ('flash')
    }
}

OTHER TIPS

There was a similar requirement in my project. I need to include swf file which is an output of flex project into java project and build the war.

1. I created a two sub project under war project, flex and java.
2. Included them in settings file.
3. The main build.gradle file has basic configuration.
4. In flex sub project gradlefx plugin is applied and the output swf file is copied to the directory using a copy task.
5. In java sub project war plugin is applied and the source directory is mentioned from which war has to be generated.

Here is an example code for your reference:

settings.xml:

include myproject,
myproject:mysubproject:flex,
myproject:mysubproject:java

build.gradle file in myproject dir:

buildscript {
    dependencies {
        classpath project (":FlexProject") //include all the flex project from which the swf file to be included
    }
}
dependencies {
    classpath project (":JavaProject") //include all the dependent java project if any
}

sourceSets {
  main {
    output.classesDir   = 'root/WEB-INF/classes' //This is the directory from which I am going to generate war.
  }
}

build.gradle file in myproject/mysubproject/flex dir:

apply plugin: 'gradlefx'
type = 'swf'
dependencies{
    merged project (":FlexProject")
}

String rootProjectDir = rootDir
rootProjectDir = rootProjectDir.replaceAll("\\\\","/")

task copyTask <<{
    copy{
        from rootProjectDir +'/MyFlexProject1/build'
        into rootProjectDir +'/myproject/root'
        include '**/*.swf'
    }
}
build.finalizedBy(copyTask)

build.gradle file in myproject/mysubproject/java dir:

String rootProjectDir = rootDir
rootProjectDir = rootProjectDir.replaceAll("\\\\","/")
String rootProjectDirDocRoot = rootProjectDir + '/myproject/root'

dependencies {
    compile project (":JavaProject")    //to include dependent jars in war
}

group = "com.abc.enterprise"
archivesBaseName = "myprojet"
description = "myproject"
apply plugin: 'war'

war{
    from rootProjectDirDocRoot
    exclude('.gradle')
    exclude('.settings')
}

This will compile the flex project every time and he swf file will in be included into the directory from which the war has to be built. Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top