Reading properties file from Maven POM file

前端 未结 3 1028
自闭症患者
自闭症患者 2020-12-01 07:26

I have Maven POM file with some configuration and in the section plugins, I have maven tomcat plugin with some configuration like this:

         


        
相关标签:
3条回答
  • 2020-12-01 08:08

    Complete working example available at: http://hg.defun.work/exp/file/tip/maven/properties

    Here essential part of 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>
    

    As you can see properties-maven-plugin still at alpha stage, that is why I hate Maven as build tools...

    0 讨论(0)
  • 2020-12-01 08:10

    Maven allows you to define properties in the project's POM. You can do this using a POM file similar to the following:

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

    You can avoid specifying the property within the properties tag, and pass the value from the command line as:

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

    Now, if you don't want to specify them from the command line and if you need to further isolate these properties from the project POM, into a properties file, then you'll need to use the Properties Maven plugin, and run it's read-project-properties goal in the initialize phase of the Maven lifecycle. The example from the plugin page is reproduced here:

    <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 讨论(0)
  • 2020-12-01 08:19

    It is not actually possible to load properties from a file using the instructions in the accepted answer as these properties are not available in the pom file though they can be used for filtering. Minimal counter example:

    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>
    

    Having in dev.properties:

    foo=bar
    

    Then runnin the command mvn validate produces output:

     [echo] Displaying value of properties
     [echo] [foo] bar
    
    0 讨论(0)
提交回复
热议问题