Managing properties defined in a properties file

In this recipe, we look at Roo commands, which you can use to add, remove, and modify properties defined in a properties file. We will use the log4j.properties file of the flight-app project to demonstrate the use of commands.

The following table shows the properties that we will add, modify, and remove from the log4j.properties file:

Property

Action

log4j.appender.R.File = application.log

Modified to log4j.appender.R.File = flightapp.log

log4j.rootLogger = debug, stdout

Modified to log4j.rootLogger = ERROR

log4j.appender.stdout = org.apache.log4j.ConsoleAppender

Removed from log4j.properties

log4j.logger.sample.roo.flightapp.service = ERROR

Added to log4j.properties

Getting ready

Start the Roo shell from the C: oo-cookbookch01-recipe directory, which contains the flight-app Roo project.

How to do it...

To manage the properties defined in a properties file follow the given steps:

  1. The properties set command is used to modify properties shown as follows:
    roo> properties set --name log4j.properties --path SRC_MAIN_RESOURCES --key log4j.appender.R.File --value flightapp.log
    .....
    roo> properties set --name log4j.properties --path SRC_MAIN_RESOURCES --key log4j.rootLogger --value ERROR
    
  2. The properties remove command is used to remove properties, shown as follows:
    roo> properties remove --name log4j.properties --path SRC_MAIN_RESOURCES --key log4j.appender.stdout
    
  3. The properties set can also be used to add a new property, shown as follows:
    roo> properties set --name log4j.properties --path SRC_MAIN_RESOURCES --key log4j.logger.sample.roo.flightapp.service --value DEBUG
    

How it works...

Like the properties list command, the properties set and properties remove commands are provided by Properties file add-on . The following table describes the arguments that both the properties set and properties remove commands accept:

Argument

Purpose

path

It is a mandatory argument that identifies a path to the properties file. Refer to the Viewing properties defined in a properties file and Creating a Roo project recipes for details on the values it can accept.

name

It is a mandatory argument that specifies the name of the properties file whose property you want to remove

key

It is a mandatory argument that specifies the key of the property that you want to remove from the properties file.

The properties set command accepts all the arguments that the properties remove command accepts. Additionally, it accepts a mandatory argument, value, which specifies a value of the property being set by the properties set command. If a matching property is found in the properties file, the existing property is updated with the new value. If no matching property is found, a new property is added to the properties file.

There's more...

You can also change the properties file using your favorite IDE. If you are creating a new Roo project which acts as a template for creating other projects, using properties commands to add, modify, and remove properties from a properties file can be valuable.

If you want to modify logging configuration, you should first consider using the logging setup command (explained earlier in the Configuring logging recipe). If you want to modify database properties, you should use database commands (explained in the Managing database configuration properties recipe in Chapter 2, Persisting Objects Using JPA).

See also

  • The Configuring logging recipe explains how to configure logging using Spring Roo commands
  • The Managing database configuration properties recipe explains how to configure database properties using Spring Roo commands
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.172.38