Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the properties tag within maven profiles

I am in reference to "Maven: The Complete Reference" and especially the section regarding profiles which documents the use of a <properties... tag within the <profile... tag here: see here

 <profile>
            <id>development</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <property>
                    <name>environment.type</name>
                    <value>dev</value>
                </property>
            </activation>
            <properties>
                <database.driverClassName>com.mysql.jdbc.Driver</database.driverClassName>
                <database.url>
                    jdbc:mysql://localhost:3306/app_dev
                </database.url>
                <database.user>development_user</database.user>
                <database.password>development_password</database.password>
            </properties>
        </profile>

What I am not sure about is what happens when the mvn install -Denvironment.type=dev command is run:

  • Will this create a .properties file?
  • If not how and where will tomcat (for instance) read the individual properties when the app is tested in dev?
like image 379
balteo Avatar asked Aug 22 '12 08:08

balteo


People also ask

What is properties tag in Maven?

Maven properties are value placeholders, like properties in Ant. Their values are accessible anywhere within a POM by using the notation ${X}, where X is the property. Or they can be used by plugins as default values, for example: In your case you have defined properties as version of java.

What is the use of properties in Maven?

Properties can be defined in a POM or in a Profile. The properties set in a POM or in a Maven Profile can be referenced just like any other property available throughout Maven. User-defined properties can be referenced in a POM, or they can be used to filter resources via the Maven Resource plugin.

What is one of the advantages of using properties in Maven?

Maven allows us to define as well as use properties. Properties allow us to avoid hardcoding values in multiple places such as versions of dependencies. They also provide flexibility to the build tool by allowing values to be passed at runtime.


1 Answers

Will this create a .properties file?

No, it won't. This would set the properties used by maven. This is, with mvn install -Denvironment.type=development maven would use the value 'development_user' for the variable 'database.user' (that you can use as ${database.user} in poms and filtered resources).

If not how and where will tomcat (for instance) read the individual properties when the app is tested in dev?

The thing is to tell maven to filter (and modify) the resources that you want to customize depending on the profile (properties.files).

So, first you have to say maven to filter the resources:

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

Then modify your properties files to use maven variables. For example, your db properties file would look like this:

database.driverClassName=${database.driverClassName}
database.url=${database.url}
#...
like image 128
polypiel Avatar answered Oct 07 '22 17:10

polypiel