2011-08-22 28 views
29

मैं Maven पोम कुछ विन्यास के साथ और खंड प्लगइन्स में दाखिल है, मैं Maven बिल्ला इस तरह की कुछ विन्यास के साथ प्लगइन है:पढ़ना गुण Maven पोम से फ़ाइल

<configuration> 
    <url>http://localhost:8080/manager/html</url> 
    <server>tomcat</server> 
</configuration> 

मैं यूआरएल सेटिंग निर्यात करना चाहते हैं उदाहरण के लिए कुछ संपत्ति फाइल करने के लिए है कि कुंजी के साथ tomcat.properties:

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

और कैसे मैं इस कुंजी मेरी पोम फ़ाइल में वापस पढ़ सकते हैं?

उत्तर

32

मेवेन आपको परियोजना के पीओएम में गुणों को परिभाषित करने की अनुमति देता है। आप निम्न को यह एक पोम फ़ाइल समान का उपयोग कर सकते हैं:

<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> 

आप properties टैग के भीतर संपत्ति निर्दिष्ट करने से बच सकते हैं, और जैसा कि कमांड लाइन से मान पास:

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

अब, यदि आप उन्हें कमांड लाइन से निर्दिष्ट नहीं करना चाहते हैं और यदि आपको प्रोजेक्ट POM से प्रॉपर्टी फ़ाइल में इन गुणों को और अलग करना है, तो आपको Properties Maven plugin का उपयोग करना होगा, और इसे में read-project-properties लक्ष्य चलाने की आवश्यकता होगी । प्लगइन पेज से उदाहरण यहां गयी है:

<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

यह काम करता है !! .. धन्यवाद!! –

4

संभव नहीं होता स्वीकार किए जाते हैं जवाब में निर्देशों का उपयोग कर के रूप में इन गुणों पोम फ़ाइल में उपलब्ध नहीं हैं एक फ़ाइल से गुण लोड करने के लिए ही वे इस्तेमाल किया जा सकता है फ़िल्टरिंग के लिए। मिनिमल काउंटर उदाहरण:

pom.xml में:

<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 में होने:

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

आप ** [echo] [foo] $ {foo} ** स्ट्रिंग में विफल हो जाते हैं। Grrr .... – gavenkoa

+0

ऐसा इसलिए है क्योंकि ** ** प्रारंभ ** ** ** प्रारंभ करने से पहले निष्पादित **। – gavenkoa

+0

@gavenkoa क्या आप समझा सकते हैं कि मैं उस स्ट्रिंग में कैसे विफल रहता हूं? –

8

पूरा काम कर उदाहरण:

foo=bar 

फिर आदेश भाग रहा mvn validate उत्पादन का उत्पादन पर उपलब्ध: http://hg.defun.work/exp/file/tip/maven/properties

pom.xml के यहां अनिवार्य हिस्सा:

<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> 

आप गुण-Maven-प्लगइनअल्फा स्तर पर अभी भी देख सकते हैं, यही वजह है कि मैं निर्माण उपकरण के रूप में Maven से नफरत है। ..

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^