2017-02-07 4 views
1

私は異なる環境のビルドスクリプトを書こうとしています。しかし、グローバルプロパティは特定のタスクのために更新されません。ここ はスクリプトです:gradleのグローバルextプロパティを変更する

ext { 
    springProfilesActive = 'development' 
    angularAppBasePath = '/test/' 
} 

task setActiveProfiles { 
    doLast { 
     if (project.hasProperty('activeProfiles')) { 
      springProfilesActive = project.property('activeProfiles') 
     } 
    } 
} 

task setProperties(dependsOn: ':setActiveProfiles') { 
    doLast { 
     if (springProfilesActive != 'development') { 
      angularAppBasePath = '/' 
     } 
     println springProfilesActive 
     println angularAppBasePath 
    } 
} 

task buildAngular(type: Exec, dependsOn: ':setProperties') { 
    workingDir angularPath 
    commandLine 'cmd', '/c', "npm run build.prod -- --base ${angularAppBasePath}" 
} 

私はbuildAngular -PactiveProfiles=integrationを実行する場合properiesが正しく設定されています。しかし、angularAppBasePathは、npmコマンドの古い/test/の値です。出力:properyはsetPropertiesタスクに変更はなくbuildAngularタスク内の古い値のままされているのはなぜ

Executing external task 'buildAngular -PactiveProfiles=integration'... 
:setActiveProfiles 
:setProperties 
integration 
/
:buildAngular 

> [email protected] build.prod C:\myapp\src\main\angular 
> gulp build.prod --color --env-config prod --build-type prod "--base" "/test/" 

答えて

1

は、次のようにあなたのsetActiveProfilessetPropertiesタスクを書き換えるようにしてください:

task setActiveProfiles { 
    if (project.hasProperty('activeProfiles')) { 
     springProfilesActive = project.property('activeProfiles') 
    } 
} 

task setProperties(dependsOn: ':setActiveProfiles') { 
    if (springProfilesActive != 'development') { 
     angularAppBasePath = '/' 
    } 
    println springProfilesActive 
    println angularAppBasePath 
} 

この動作は異なるビルドのライフサイクルによって引き起こされます。実行フェーズ(doLastクロージャ内)中に変数を変更しますが、実行の直前に発生するコンフィギュレーションフェーズで使用します。Gradle official user guideでビルドライフサイクルについて読むことができます。

+0

残念ながら、この変更は効果がありませんでした。 – deve

+0

確かに、 'setProperties'タスクは設定時にも実行されるべきです。コードスニペットにそれを追加するのを忘れてしまった – Stanislav

+0

ありがとう、それはトリックでした。 – deve

関連する問題