ChatGPT解决这个技术问题 Extra ChatGPT

How to read an external properties file in Maven

Does anyone know how to read a x.properties file in Maven. I know there are ways to use resource filtering to read a properties file and set values from that, but I want a way in my pom.xml like:

<properties file="x.properties"> 

</properties>

There was some discussion about this: Maven External Properties

If you have only a few values, and different users will need different values, consider instead putting the value in your settings.xml.

k
kapex

Try the Properties Maven Plugin


I think that's what I'm looking for I couldn't find the 1.0-SNAPSHOT in the maven repositories but there is a release: mvnrepository.com/artifact/org.codehaus.mojo/… <dependency> <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <version>1.0-alpha-1</version> </dependency>
Current version: <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <version>1.0-alpha-2-SNAPSHOT</version> from snapshots.repository.codehaus.org
The link in the Answer has been updated to the new link from @JesseGlick
I had problems with this plugin on Windows. If someone has problems too, try out kuali instead.
T
The Alchemist

Using the suggested Maven properties plugin I was able to read in a buildNumber.properties file that I use to version my builds.

  <build>    
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0-alpha-1</version>
        <executions>
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>read-project-properties</goal>
            </goals>
            <configuration>
              <files>
                <file>${basedir}/../project-parent/buildNumber.properties</file>
              </files>
            </configuration>
          </execution>
        </executions>
      </plugin>
   </plugins>

could you show the inside of the buildNumber.properties file? thank you!
Thanks for a working example. However, why I got an error of Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:properties-maven-plugin:1.0-alpha-1:read-project-properties (execution: default, phase: initialize)
When I enter this <plugin> section under <plugins> after </build> and before the regular maven plugins, I am getting this error: Plugin 'execution' not covered by lifecycle configuration: org.codehaus.mojo:properties-maven-plugin:1.0-alpha-1:read-project-properties (execution: default, phase: initialize)
@BorisBrodski could you show the inside of the buildNumber.properties file? It seems you deleted some details.
@MoustafaMahmoud why me? That's not my answer :) But I may speculate, that it could look like 'my.great.product.version=1.0.0'.
C
Community

This answer to a similar question describes how to extend the properties plugin so it can use a remote descriptor for the properties file. The descriptor is basically a jar artifact containing a properties file (the properties file is included under src/main/resources).

The descriptor is added as a dependency to the extended properties plugin so it is on the plugin's classpath. The plugin will search the classpath for the properties file, read the file''s contents into a Properties instance, and apply those properties to the project's configuration so they can be used elsewhere.