2011-08-22 26 views
29

Maven POM bazı yapılandırmayla ve bölüm eklentileri dosyasını var, maven tomcat bu gibi bazı yapılandırmayla eklentisi var dosya Maven POM dosyasınıOkuma özellikleri

url=http://localhost:8080/manager/html 

Ve nasıl geri POM dosyasında bu anahtarı okuyabilir: Örneğin bazı özellik dosyasına bu anahtarla tomcat.properties?

cevap

32

Maven, projenin POM'undaki özellikleri tanımlamanıza olanak tanır. Aşağıdakine bu benzer bir POM dosyasını kullanarak yapabilirsiniz:

<project> 
    ... 
    <properties> 
     <server.url>http://localhost:8080/manager/html</server.url> 
    </properties> 
    ... 
    <build> 
     <plugins> 
      <plugin> 
      ... 
       <configuration> 
        <url>${server.url}</url> 
        <server>tomcat</server> 
       </configuration> 
      ... 
      </plugin> 
     </plugins> 
    </build> 
</project> 

Sen properties etiketi içinde özelliğini belirterek önlemek ve aynı komut satırından değer iletebilirsiniz:

mvn -Dserver.url=http://localhost:8080/manager/html some_maven_goal 

Şimdi, bunları komut satırından belirtmek istemiyorsanız ve bu özellikleri proje POM'undan bir özellik dosyasına ayırmak isterseniz, Properties Maven plugin'u kullanmanız ve initialize phase of the Maven lifecycle numaralı telefondan read-project-properties hedefini çalıştırmanız gerekir. . Eklenti sayfasından örnek burada üretilir: kullanılmadan olsa

<project> 
    <build> 
    <plugins> 
     <plugin> 
     <groupId>org.codehaus.mojo</groupId> 
     <artifactId>properties-maven-plugin</artifactId> 
     <version>1.0-alpha-2</version> 
     <executions> 
      <!-- Associate the read-project-properties goal with the initialize phase, to read the properties file. --> 
      <execution> 
      <phase>initialize</phase> 
      <goals> 
       <goal>read-project-properties</goal> 
      </goals> 
      <configuration> 
       <files> 
       <file>etc/config/dev.properties</file> 
       </files> 
      </configuration> 
      </execution> 
     </executions> 
     </plugin> 
    </plugins> 
    </build> 
</project> 
+0

Bu çalışır! .. Teşekkür ederim!! –

4

özelliklerin, pom dosyasında bulunmayan olarak kabul edilen yanıt talimatları kullanarak bir dosyadan özelliklerini yüklemek için aslında mümkün değildir filtreleme için. az karşı örnek: pom.xml olarak

:

<build> 
    <plugins> 
    <plugin> 
     <groupId>org.codehaus.mojo</groupId> 
     <artifactId>properties-maven-plugin</artifactId> 
     <version>1.0-alpha-2</version> 
     <executions> 
     <!-- Associate the read-project-properties goal with the initialize phase, to read the properties file. --> 
     <execution> 
      <!-- Apart from this test, the phase must be initialize --> 
      <phase>validate</phase> 
      <goals> 
      <goal>read-project-properties</goal> 
      </goals> 
      <configuration> 
      <files> 
       <file>dev.properties</file> 
      </files> 
      </configuration> 
     </execution> 
     </executions> 
    </plugin> 
    <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-antrun-plugin</artifactId> 
     <version>1.6</version> 
     <executions> 
     <execution> 
      <phase>validate</phase> 
      <goals> 
      <goal>run</goal> 
      </goals> 
      <configuration> 
      <target> 
       <echo>Displaying value of properties</echo> 
       <echo>[foo] ${foo}</echo> 
      </target> 
      </configuration> 
     </execution> 
     </executions> 
    </plugin> 
    </plugins> 
</build> 

dev.properties de sahip:

[echo] Displaying value of properties 
[echo] [foo] bar 
+0

** [echo] [foo] $ {foo} ** dizesinde başarısız oluyorsunuz. Grrr .... – gavenkoa

+0

Bunun nedeni ** validate **'nin ** initialize ** 'dan önce çalıştırılmasıdır. – gavenkoa

+0

@gavenkoa Bu dizede nasıl başarısız olduğumu açıklayabilir misiniz? –

8

tam bir çalışma örneği:

foo=bar 

sonra komut akıyor mvn validate çıktı üretir mevcut: pom.xml ait http://hg.defun.work/exp/file/tip/maven/properties

İşte parçasıdır: Eğer alfa aşamada hala özellikleri-maven-plugin görebileceğiniz gibi ben inşa araçları gibi Maven neden nefret

<plugins> 
    <plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>properties-maven-plugin</artifactId> 
    <version>1.0-alpha-2</version> 
    <executions> 
     <execution> 
     <phase>initialize</phase> 
     <goals> 
      <goal>read-project-properties</goal> 
     </goals> 
     </execution> 
    </executions> 
    <configuration> 
     <files> 
     <file>dev.properties</file> 
     </files> 
    </configuration> 
    </plugin> 

    <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-antrun-plugin</artifactId> 
    <version>1.6</version> 
    <executions> 
     <execution> 
     <phase>compile</phase> 
     <goals> 
      <goal>run</goal> 
     </goals> 
     <configuration> 
      <target> 
      <echo>project.build.sourceEncoding is "${project.build.sourceEncoding}"</echo> 
      <echo>foo is "${foo}"</echo> 
      <echo>with-spaces is "${with-spaces}"</echo> 
      <echo>existent.property is "${existent.property}"</echo> 
      <echo>nonexistent.property is "${nonexistent.property}"</echo> 
      </target> 
     </configuration> 
     </execution> 
    </executions> 
    </plugin> 
</plugins> 

, olmasıdır. ..