user26270
user26270

Reputation: 7074

how to inject a property at maven build time that is accessible later at runtime in a java project?

I'm trying to inject a property into a java project when it is built by maven, by injecting the property via the command line, like

mvn clean install -DMYPROP=myProperty  

I thought this was enough to have maven transform this into a System property, which could then be accessed in java via System.getProperty("MYPROP"). But this hasn't worked.

So I did more research, one post here pointed to Maven Properties, so I tried the following, in the pom.xml:

<properties>
    <MY.VARIABLE>${MYPROP}</MY.VARIABLE>
    <MY.ENV.VARIABLE>${env.MYPROP}</MY.ENV.VARIABLE>
</properties>

This is not working either. All the following properties are null

System.getProperty("MYPROP")
System.getProperty("MY.VARIABLE")
System.getProperty("MY.ENV.VARIABLE")

I've also tried using a properties-maven-plugin, based on another question. That property was null as well.

Any suggestions?

Upvotes: 1

Views: 5874

Answers (4)

Petr Aleksandrov
Petr Aleksandrov

Reputation: 1514

You can save the properties to the file inside the build artifact.

E.g. file src/main/resources/myProperties.properties

mavenProperty=${MYPROP}

You need to tune filtering in pom.xml:

<project>
...
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

Then, when you run maven with -DMYPROP=some-value, myProperties.properties will contain:

mavenProperty=some-value

So you will be able to get some-value in runtime by loading the property resource (and not with System.getProperty(..)):

public class App {                                                             

  public static void main(String[] args) throws IOException {                  
    Properties properties = new Properties();                                  
    properties.load(App.class.getResourceAsStream("/myProperties.properties"));
    properties.get("mavenProperty");                                             
  }                                                                            

}                                                                                                                          

Upvotes: 3

Michael
Michael

Reputation: 1237

There are several ways of achieving your goal. Below is one of them.

No matter what you decide to do, using a properties file to store your properties is a good practice.

With maven you can use profiles and create different properties files for different profiles. To make maven pass the property value to the property file, use resource filtering. Example below:

<project ... >
    .
    .
    .
    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <my.prop>dev_value</my.prop>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <my.prop>prod_value</my.prop>
            </properties>
        </profile>
    </profiles>
    .
    .
    .
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

Inside the src/main/resources/my_properties.properties file, use:

my.prop=${my.prop:default_value}

This will eventually replace the value of my.prop and if none is found, the default value default_value will be used.

Hope this helps.

Upvotes: 1

KevinB
KevinB

Reputation: 1202

I use Maven Resources Plugin to write the Maven properties into a .properties file.

In your POM, specify the directory that contains the properties file and enable filtering:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

Then in a properties file, like: src/main/resources/application.properties, reference a property:

my.property=${my.maven.property}

Then, when you build the project your property will be written into the properties file in the target/ dir. Then in your application, at runtime load the properties file and access the value.

Upvotes: 1

J Fabian Meier
J Fabian Meier

Reputation: 35795

As far as I know, there is no way to set a System property for the Java application that you build.

If you want to save information for later use inside the Java application, you can use properties files as resources that are packed into the jar.

Upvotes: 0

Related Questions