2009-01-08 64 views
19

我設立一個Maven構建,和目的地服務器需要進行命令行作爲屬性(其隨後被用於選擇適當的配置文件)上指定,例如我可以強制Maven 2要求在命令行上指定屬性嗎?

mvn -Denv=test 

我想如果該屬性沒有設置,構建失敗 - 這是可能的嗎?

是的,我是Maven新手。

編輯:我見過this link,這似乎暗示它是不可能的,但我不知道它是如何最新的。

回答

2

我的第一個想法是創建一個配置文件,該配置文件在env屬性未設置時處於活動狀態,並以某種方式失敗。也許你可以編寫一個測試該屬性的Maven插件,如果它不存在則失敗?

或者,你可以用一個非常小的ant-build腳本來測試它。

1

爲了詳細說明edbrannin的替代解決方案:

<project> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.yourcompany</groupId> 
    <artifactId>yourproject</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <build> 
    <plugins> 
     <plugin> 
     <artifactId>maven-antrun-plugin</artifactId> 
     <executions> 
      <execution> 
      <id>checkParam</id> 
      <phase>initialize</phase> 
      <goals><goal>run</goal></goals> 
      <configuration> 
       <tasks> 
       <fail message="'env' property must be set" unless="env"/> 
       </tasks> 
      </configuration> 
      </execution> 
     </executions> 
     </plugin> 
    </plugins> 
    </build> 
</project> 

會給你以下的輸出:

[INFO] ------------------------------------------------------------------------ 
[ERROR] BUILD ERROR 
[INFO] ------------------------------------------------------------------------ 
[INFO] An Ant BuildException has occured: 'env' property must be set 

恕我直言,最簡單的方法來做到這一點(一個我會去親自) 。

你可以使用包含<or><equals>標籤嵌套<condition>甚至控制一組允許值(見Ant手冊:http://ant.apache.org/manual/Tasks/conditions.html

+0

這可以通過執行插件直接完成。 – 2009-04-10 23:47:56

5

也許你可以使用這樣的解決辦法:在Maven中,你可以激活,如果配置文件部分房產未設置:

<project> 
... 
    <profiles> 
     <profile> 
      <id>failure_profile</id> 
      <activation> 
       <property> 
        <name>!env</name> 
       </property> 
      </activation> 
     </profile> 
    </profiles> 
</project> 

然後您應該強制該配置文件總是失敗,例如使用Maven的實施者 - 插件:然後

<profile> 
    <id>failure_profile</id> 
    ... 
    <build> 
     <plugins> 
      <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-enforcer-plugin</artifactId> 
      <executions> 
       <execution> 
       <id>enforce</id> 
       <goals> 
        <goal>enforce</goal> 
       </goals> 
       <configuration> 
        <rules> 
        <AlwaysFail/> 
        </rules> 
        <fail>true</fail> 
       </configuration> 
       </execution> 
      </executions> 
      </plugin> 
     </plugins> 
     </build> 
</profile> 

如果不提供-Denv構建就會失敗:

[INFO] [enforcer:enforce {execution: enforce}] 
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.AlwaysFail failed with message: 
Always fails! 
[INFO] --------------------------------------------------------- 
[ERROR] BUILD ERROR 

嗯,這是更詳細的則Ant,但純粹的Maven :)

+0

優秀;非常感謝。正如你所說,它更加冗長,所以我會堅持Olivier的回答作爲我接受的答案。讓我說我想要一個純粹的Maven回答 - 道歉。我希望我能接受兩個答案。 – Hobo 2009-01-12 11:43:25

相關問題