Giter Club home page Giter Club logo

portable-config-maven-plugin's Introduction

Build Status

Why Portable Config Maven Plugin?

When you want to deploy your application into different environments, say typical dev/test/staging/production, often you will want to change some configuration at build time. In Maven, this is a feature called Resource Filtering, which works quite well except you have to use Maven Property for every configuration you want to change. So you will have:

database.jdbc.connectionURL=${db.connectionURL}
database.jdbc.username=${db.username}
database.jdbc.password=${db.password}

So there's no default configuration in this case. With portable-config-maven-plugin, all configurations have default value, and you configure build to change the configurations only when you need to.

How to Use?

  1. You have a configuration file src/main/resources/db.properties with default values:
database.jdbc.username=dev    
database.jdbc.password=dev_pwd
  1. You want to change it in testing environment, so prepare a file for this change src/main/portable/test.xml:
  <?xml version="1.0" encoding="utf-8" ?>
  <portable-config>
    <config-file path="WEB-INF/classes/db.properties">
      <replace key="database.jdbc.username">test</replace>
      <replace key="database.jdbc.password">test_pwd</replace>
    </config-file>
  </portable-config>

Here the config_file is the file you want to change for testing environment, and path is its relative path in final war. Then you can use replace to change the configurations.

  1. Configure Maven to use portable-config-maven-plugin and apply your portable config:
   <plugin>
     <groupId>com.juvenxu.portable-config-maven-plugin</groupId>
     <artifactId>portable-config-maven-plugin</artifactId>
     <version>1.1.5</version>
       <executions>
         <execution>
           <goals>
             <goal>replace-package</goal>
           </goals>
         </execution>
       </executions>
       <configuration>
         <portableConfig>src/main/portable/test.xml</portableConfig>
       </configuration>
   </plugin>

You can also use maven property portableConfig to specify your portable config file: $ mvn clean package -DportableConfig="src/main/portable/test.xml"

  1. After running Maven, the file WEB-INF/classes/db.properties in the final war is now having:
database.jdbc.username=test
database.jdbc.password=test_pwd

Supported Configuration File Format

.properties

Properties files are most used configuration file format:

database.jdbc.username=dev
database.jdbc.password=dev_pwd

with

<replace key="database.jdbc.username">test</replace>
<replace key="database.jdbc.password">test_pwd</replace>

become

database.jdbc.username=test
database.jdbc.password=test_pwd

.sh

Unquoted, single quoted, double quoted and exported shell variables can be replaced:

BIN_HOME=/tmp/bin
OUT_HOME="/tmp/out"
LOG_HOME='/tmp/log'
export APP_HOME="/tmp/app"

with

<replace key="BIN_HOME">/home/juven/bin</replace>
<replace key="OUT_HOME">/home/juven/out</replace>
<replace key="LOG_HOME">/home/juven/log</replace>
<replace key="APP_HOME">/home/juven/app</replace>

become

BIN_HOME=/home/juven/bin
OUT_HOME="/home/juven/out"
LOG_HOME='/home/juven/log'
export APP_HOME="/home/juven/app"

.xml

Xml elements/attributes can be replaced via xPath:

<?xml version="1.0" encoding="UTF-8"?>
<server>
  <port>8080</port>
  <hosts>
    <host id="1">localhost</host>
    <host id="2">localhost</host>
  </hosts>
  <mode value="debug">
</server>"

with

<replace xpath="/server/port">80</replace>
<replace xpath="//host/[@id='1']">192.168.1.1</replace>
<replace xpath="//host/[@id='2']">192.168.1.2</replace>
<replace xpath="/server/mode/@value">run</replace>

become

<?xml version="1.0" encoding="UTF-8"?>
<server>
  <port>80</port>
  <hosts>
    <host id="1">192.168.1.1</host>
    <host id="2">192.168.1.2</host>
  </hosts>
  <mode value="run">
</server>"

FAQ

Q: I have properties file but it's extension is not .properties, how to make this plugin to recognize it?

A. You can specify file type in your portable config xml like this:

<?xml version="1.0" encoding="utf-8" ?>
<portable-config>
  <config-file path="db.ini" type=".properties">
    <replace key="mysql.host">192.168.1.100</replace>
  </config-file>
</portable-config>

Tutorial From Users

Acknowledgments

  • Thanks to Jacky Chan for initially inspiring me to write this tool with thoughtful ideas.

portable-config-maven-plugin's People

Contributors

juven avatar ethanfu avatar yuanqixun avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.