Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8424219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:55:45+00:00 2026-06-10T03:55:45+00:00

I need to read and filter a properties file from a location outside my

  • 0

I need to read and filter a properties file from a location outside my project, say ${user.home}/my.properties. This properties file looks like this:

res.dir=/my/stuff/here
resource.dir=C:/${res.dir}
bin.dir=${resource.dir}/bin
cfg.dir=${resource.dir}/config

I have to do this in both my build and in my application when it runs. This is easy to do in Ant, using the PROPERTY tag. However, there doesn’t seem to be a good way to do it in Maven.

So far I have tried the Maven <property> tag, the Maven <filter> tag and various permutations of other tags. Either my build fails or the unit tests fail, or both.

If I hardcode these properties into the POM, everything works, so I know the problem is just reading the properties.

I looked at the properties-maven-plugin but the plugin no longer seems to be maintained.

Is there a way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:55:46+00:00Added an answer on June 10, 2026 at 3:55 am

    You could simply implement your own maven-plugin that will take care of this for you.

    Here is an example with the following structure:

    .
     |-- pom.xml
     |-- plugin
     |   `-- pom.xml
     |   `-- src
     |       `-- main
     |           `-- java
     `-- app
         `-- pom.xml
         `-- src
             `-- main
                 `-- java
    

    You will need to create a Mojo that takes the properties file as an input and then propagates the properties to the pom.xml of the app. The pom.xml will actually not be updated but just the project data in it.

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.stackoverflow</groupId>
        <artifactId>Q12082277</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>pom</packaging>
    
        <name>${project.artifactId}-${project.version}</name>
    
        <modules>
            <module>plugin</module>
            <module>app</module>
        </modules>
    
    </project>
    

    plugin/pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <parent>
            <groupId>com.stackoverflow</groupId>
            <artifactId>Q12082277</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
    
        <artifactId>Q12082277-plugin</artifactId>
        <packaging>maven-plugin</packaging>
    
        <name>${project.artifactId}-${project.version}</name>
    
        <dependencies>
            <dependency>
                <groupId>org.apache.maven</groupId>
                <artifactId>maven-plugin-api</artifactId>
                <version>3.0.4</version>
            </dependency>
            <dependency>
                <groupId>org.apache.maven</groupId>
                <artifactId>maven-project</artifactId>
                <version>2.2.1</version>
            </dependency>
        </dependencies>
    </project>
    

    plugin/src/main/java/com/stackoverflow/Q12082277/plugin/PropertiesMojo.java

    package com.stackoverflow.Q12082277.plugin;
    
    import org.apache.maven.plugin.AbstractMojo;
    import org.apache.maven.plugin.MojoExecutionException;
    import org.apache.maven.plugin.MojoFailureException;
    import org.apache.maven.plugin.logging.Log;
    import org.apache.maven.project.MavenProject;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Properties;
    
    /**
     * @author maba, 2012-08-24
     *
     * @goal extract
     */
    public class PropertiesMojo extends AbstractMojo {
    
        private Log log;
    
        /**
         * The current project representation.
         * @parameter expression="${project}"
         * @required
         * @readonly
         */
        private MavenProject project;
    
        /**
         * A properties file
         *
         * @parameter expression="${propertiesFile}"
         * @required
         */
        private File propertiesFile;
    
        @Override
        public void execute() throws MojoExecutionException, MojoFailureException {
            log.info("Executing PropertiesMojo on " + propertiesFile.getAbsolutePath());
    
            try {
                Properties fileProperties = new Properties();
                fileProperties.load(new FileInputStream(propertiesFile));
                Properties projectProperties = project.getProperties();
                for (Object key : fileProperties.keySet()) {
                    projectProperties.setProperty((String)key, (String) fileProperties.get(key));
                }
                project.getProperties().list(System.out);
            } catch (FileNotFoundException e) {
                throw new MojoFailureException("The file " + propertiesFile.getAbsolutePath() + " was not found!", e);
            } catch (IOException e) {
                log.error("");
            }
    
        }
    
        @Override
        public void setLog(Log log) {
            this.log = log;
        }
    }
    

    You will use this plugin from the following app/pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <parent>
            <groupId>com.stackoverflow</groupId>
            <artifactId>Q12082277</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
    
        <artifactId>Q12082277-app</artifactId>
    
        <name>${project.artifactId}-${project.version}</name>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>com.stackoverflow</groupId>
                    <artifactId>Q12082277-plugin</artifactId>
                    <version>1.0-SNAPSHOT</version>
                    <executions>
                        <execution>
                            <phase>initialize</phase>
                            <goals>
                                <goal>extract</goal>
                            </goals>
                            <configuration>
                                <propertiesFile>${user.home}/my.properties</propertiesFile>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.2.1</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>java</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <mainClass>com.stackoverflow.Q12082277.App</mainClass>
                    </configuration>
                </plugin>
            </plugins>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
        </build>
    </project>
    

    And then you will have to add the following app.properties that will work as a template and take the values that we have just read from file and set them and create a concrete file app.properties that will be reachable from within the jar.

    app/src/main/resources/app.properties

    res.dir=${res.dir}
    resource.dir=${resource.dir}
    bin.dir=${bin.dir}
    cfg.dir=${cfg.dir}
    

    And finally here is a test application that just loads the app.properties from the classpath and prints the result.

    app/src/main/java/com/stackoverflow/Q12082277/App.java

    package com.stackoverflow.Q12082277;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    /**
     * @author maba, 2012-08-23
     */
    public class App {
    
        public static void main(String[] args) throws IOException {
            ClassLoader loader = App.class.getClassLoader();
            InputStream in = loader.getResourceAsStream("app.properties");
            Properties properties = new Properties();
            properties.load(in);
            properties.list(System.out);
        }
    }
    

    Now you can stand in the top directory and execute

    mvn install
    

    Then go down into the app folder and execute

    mvn exec:java
    

    And it will print

    -- listing properties --
    resource.dir=C://my/stuff/here
    cfg.dir=C://my/stuff/here/config
    bin.dir=C://my/stuff/here/bin
    res.dir=/my/stuff/here
    

    Which is exactly what you wanted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to read 16 bits from the binary file as std::string or char
I need to read data from XML to a List<>. The XML file contains
I need to read in an expression from a file using a string stream
I need to read line by line from text file (log files from server)
I need to read test.TXT file(tab delimited) into MATLAB. TXT file have form: Datum
I need to read certain statistics from iw_statistics structure, here's the code: struct net_device
I need to read a file line by line, and change a variable accordingly.
I need to read the editurl from one jqgrid for use in a DND
I need to read the size of a file but the server is forcing
I need to read and write some data on .mdb Access file and over

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.