2013-02-14 258 views
1

我有一個Gradle多項目。Gradle項目依賴不會構建依賴項目

項目之一是Flex項目,它是使用gradleFx插件構建的。

第二個是戰爭項目,我需要將來自Flex項目的SWF文件包含到戰爭中。

我添加

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

,它並把SWF陷入戰爭,但只有當SWF已經存在。

然後,我希望Gradle能夠在構建戰爭期間構建flex:FlexManager項目。所以,我想補充

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

的唯一途徑,我發現戰爭構建過程中,啓動Flex項目生成已

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

但看起來不是最好的方式 - 我喜歡定義項目依賴任務的依賴性,而不是

回答

2

所有你需要改變的是指定你想從一個任務或任務輸出而不是一個目錄複製 - 這將在你的戰爭任務和構建swf文件的任何任務之間註冊一個隱式依賴。

表示構建swf文件的任務需要指定其輸出。我快速瀏覽了GradleFx,不幸的是,它看起來像compileFlex任務不行。所以首先你需要爲任務指定的輸出方式如下:

compileFlex { 
    outputs.dir project.buildDir 
} 

,然後修改你的戰爭任務配置:

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

你或許還應該嘮叨在GradleFx開發者增加產出到他們的任務。 :)

編輯:

從評論,我理解,除了取決於任務,你可以依賴於項目工件的討論。最徹底的方法是創建一個自定義配置,添加依賴於它,然後配置戰爭任務時,在使用它撥打電話:

configuration { 
    flex 
} 

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

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

你也許還利用:flex:FlexManager直接歸檔或默認配置:

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

它有幫助,謝謝! 但是,我可以創建項目之間的依賴關係,而不是任務? – 2013-02-14 20:17:22

+0

不,你不能,在Gradle中沒有這樣的概念,你只能指定任務之間的依賴關係。你需要什麼項目依賴關係? – erdi 2013-02-14 20:42:47

+0

謝謝,我不需要。我只是認爲,就像外部依賴關係一樣,我們定義了對構件的依賴性(即構建結果),就像我可以爲項目依賴性定義它一樣 - 即我的項目依賴於另一個項目構件(構建結果) – 2013-02-14 21:00:30

0

我的項目中有類似的要求。我需要包含swf文件,這是flex項目的輸出到java項目並構建戰爭。

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. 

這是給你參考的示例代碼:

設置。XML:在myproject的目錄

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

的build.gradle文件:在的myproject/mysubproject /柔性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文件:在myproject的

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文件/ mysubproject/java目錄:

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

這將每次編譯flex項目,他的swf文件將被包含在戰爭目錄中。希望這可以幫助。