mentby.com
Blog | Jobs | Help | Signup | Login

Replacing properties with Maven.



All,

I have one requirement where I want to read all the parameterized properties
from one file and replace it in other file.

e.g.

parameterized.properties is having below two properties.

LOG_DIR=/application/logs
INSTALL_DIR = /application/config

Now I have other property file project.properties with below two properties.

logDirectory=@LOG_DIR@
installDir=@INSTALL_DIR@

After I replace strings from "parameterized.properties" to
"project.properties"  project.properties should have following value.

logDirectory==/application/logs
installDir=/application/config

I saw maven replace plugin is not sufficient for this ?

I can use ANT to perform this task but I donot want to use ANT inside MAVEN.

Any one has better approach for this.


MavenUser8979 Tue, 25 Oct 2011 07:37:19 -0700

Why not? This is perfectly acceptable to most pragmatic users of Maven...

Wayne


Wayne Fay Tue, 25 Oct 2011 08:54:04 -0700

Can't see why it should't. Can you explain?

Replace plugin should be fine. Properties plugin also.

Regards

Ansgar


Ansgar Konermann Tue, 25 Oct 2011 09:10:37 -0700

Hello!  I'm sorry you've gotten such short answers.  I'll see if I can be of
more help.

Property replacement is already available in Maven in two places:

   - As part of the resources mechanism in the POM (details here:
    http://www.sonatype.com/books/mvnref-book/reference/resource[..]
   )
   - Programmatically, as part of the maven-resources-plugin's copyResources
   goal (details here:
    http://maven.apache.org/plugins/maven-resources-plugin/examp[..]
   )

Besides those, you can of course use the maven-replacer-plugin (though I
don't really see it being any better than just straight resource filtering
for the problem you've described), or do properties replacement in Ant
(though that seems overkill, as you suggest).

Let's start there and see if either of those two approaches helps you out.
If not, we'll need more details about your project layout.

Good luck!

Best,
Laird

-- http://about.me/lairdnelson


ljnelson Tue, 25 Oct 2011 10:44:50 -0700

Thanks for your valuable help. This can be achived with  out ANT.

My goal is to migrate my project from ANT to MAVEN and donot want to use ANT
for this Proof of concept.

Given below are steps. Hope it will help some one.

1) Load all the properties in initialize phase using POM properties plugin.

   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-1</version>
    <executions>
        <execution>
        <phase>initialize</phase>
        <goals>
             <goal>read-project-properties</goal>
        </goals>
        <configuration>
            <files>
                        <file>local/global.properties</file>
            </files>
        </configuration>
        </execution>
    </executions>
</plugin>

2) Use POM's resource plugin with copy resource goal (process resource is
having some issue not sure)
    during Validate phase.

   <artifactId>maven-resources-plugin</artifactId>
   <version>2.5</version>
   <executions>
       <execution>
    <id>copy-resources</id>
    
    <phase>validate</phase>
        <goals>
        <goal>copy-resources</goal>
       </goals>
    <configuration>
                            <outputDirectory>target/classpath</outputDirectory>
    <resources>
    <resource>
                                    <directory>classpath</directory>
                                    <filtering>true</filtering>
    </resource>
    </resources>
    </configuration>
     </execution>
  </executions>
</plugin>

This will replace all the tokens from local/global.properties file to
classpath/*.* files.

Thanks.


MavenUser8979 Thu, 27 Oct 2011 02:36:45 -0700



Related Topics

Post a Comment