1

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

2 Answers 2

2

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')
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

It helps, thank you! But can I create a dependency between projects, not tasks?
No you cannot, there is no such concept in Gradle, you can only specify a dependency between tasks. What would you need a project dependency for?
Thank you, I do not need. I just thought that just as for external dependency we define dependency on the artifact (i.e. result of build), in the same way I can define it for project dependency - i.e. my project depends on another project artifact (build result)
Oh, now I know what you mean. You are referring to trying to add a runtime dependency on project(':flex:FlexManager') as in your question. This is completely valid as described here: gradle.org/docs/current/userguide/…. The problem is that you used the runtime configuration which is not resolved when running war task. You would need to create a custom configuration and add the project dependency to it and then in your war task use the configuration in the from call as a configuration is a FileCollection and can be used that way
I tried also to do it with 'compile' configuration, which is supported by war plugin, but again - w/o success
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.