2011-11-07 5 views
9

私は、コマンドラインを介して私の変数を渡すように私は私のビルドのセットアップを持っている:Mavenで見つからない環境プロパティを特定して設定する方法は?

私は私のポンポンで
mvn clean install -DsomeVariable=data 

<someTag>${someVariable}</someTag> 

これは正常に動作しますが、私はしたいと思いますコマンドラインでsomeVariableが指定されていないかどうかを確認し、スクリプトを続行できるようにデフォルト値を設定します。

これはMavenで行うことができますか?

<properties> 
    <someVariable>myVariable</someVariable> 
</properties> 

あなたは、プロパティ値は、コマンドラインで供給常にであることを確認するには、あなたが使用することができます。

答えて

12

あなたのPOMファイルのpropertiesセクションのデフォルトプロパティの値を指定することができますmaven-enforcer-pluginです。私は上記のリンクが悪くなった場合にはそのままここにXMLをコピーします>http://maven.apache.org/enforcer/enforcer-rules/requireProperty.html

- ここ

は、システムプロパティのプレゼンスを強化する方法を示したリンクです。

<project> 
    [...] 
    <build> 
    <plugins> 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-enforcer-plugin</artifactId> 
     <version>1.0.1</version> 
     <executions> 
      <execution> 
      <id>enforce-property</id> 
      <goals> 
       <goal>enforce</goal> 
      </goals> 
      <configuration> 
       <rules> 
       <requireProperty> 
        <property>basedir</property> 
        <message>You must have a basedir!</message> 
        <regex>\d</regex> 
        <regexMessage>You must have a digit in your baseDir!</regexMessage> 
       </requireProperty> 
       <requireProperty> 
        <property>project.version</property> 
        <message>"Project version must be specified."</message> 
        <regex>(\d|-SNAPSHOT)$</regex> 
        <regexMessage>"Project version must end in a number or -SNAPSHOT."</regexMessage> 
       </requireProperty> 
       </rules> 
       <fail>true</fail> 
      </configuration> 
      </execution> 
     </executions> 
     </plugin> 
    </plugins> 
    </build> 
    [...] 
</project> 
+0

ありがとう、あなたのような、その値を上書きすることができます

<properties> <someTag>defaultValue</someTag> </properties> 

としてデフォルト値を指定することができます。プロパティを強制するのではなく、プロパティをデフォルトにしたいと思っていますが。したがって、指定されていない場合、スクリプトはデフォルト値を使用します。 @TERACytE。 – TERACytE

+0

。はい、 'properties'セクションの値はデフォルトであり、コマンドラインからいつでも上書きすることができます。 –

+0

ありがとうございます。 – TERACytE

2

あなたは代わりにプロファイルを使用することができますが、各 変数のプロファイルが必要になります。

<profile> 
    <id>default-value-1</id> 
    <activation> 
      <activeByDefault>false</activeByDefault> 
      <property> 
      <name>!someVariable</name> 
      </property> 
    </activation> 
    <properties> 
     <someVariable>DEFAULT-VALUE</someVariable> 
    </properties> 
</profile> 
4

あなたはMavenのコマンドを実行すると、あなたは洞察力の応答アレックスのために、この

mvn clean package -DsomeTag=newSpecificValue 
+0

シンプルなソリューション私はJenkinsビルドで使用する必要がありました – pdem

関連する問題