2011-08-22 11 views
29

ho il file Maven POM con un po 'di configurazione e nelle plugin sezione, ho tomcat Maven plugin con una certa configurazione come questa:proprietà lettura del file da Maven POM presentare

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

mi piacerebbe esportare impostazione url ad alcuni file di proprietà per esempio tomcat.properties con quella chiave:

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

E come posso leggere questa chiave di nuovo nel mio file POM?

risposta

32

Maven consente di definire le proprietà nel POM del progetto. È possibile farlo utilizzando un file POM simile al seguente:

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

si può evitare specificando la proprietà all'interno del tag properties, e passare il valore dalla riga di comando come:

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

Ora, se non si desidera specificarli dalla riga di comando e se è necessario isolare ulteriormente queste proprietà dal progetto POM, in un file di proprietà, sarà necessario utilizzare lo Properties Maven plugin ed eseguire l'obiettivo read-project-properties nello initialize phase of the Maven lifecycle . L'esempio dalla pagina dei plugin è qui riprodotto:

<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

Questo funziona !! .. Grazie!! –

4

Non è in realtà possibile caricare oggetti di un file seguendo le istruzioni nella risposta accettata come queste proprietà non sono disponibili nel file POM anche se possono essere utilizzati per il filtraggio. Minimal contatore esempio:

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

Tenendo dev.properties:

foo=bar 

Poi correndo il comando mvn validate produce output:

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

Errore in ** [echo] [pippo] $ {pippo} ** stringa. Grrr .... – gavenkoa

+0

Questo perché ** convalida ** eseguito prima ** inizializza **. – gavenkoa

+0

@gavenkoa potresti spiegare come fallisco in quella stringa? –

8

completo esempio di lavoro disponibile a: http://hg.defun.work/exp/file/tip/maven/properties

parte Qui essenziale della 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> 

Come si può vedere proprietà-maven-plugin ancora in alpha fase, è per questo che io odio Maven come strumenti di compilazione. ..

Problemi correlati