techsjs2012
techsjs2012

Reputation: 1737

Can Maven ask the user for values and enter them into a file?

I have a lot of config files that values have to change in. I would like to know if someone runs the "package" command can it ask for some values and insert them into my project files?

Upvotes: 1

Views: 440

Answers (4)

maba
maba

Reputation: 48075

You cannot really make maven prompt for input unless you do some ant stuff suggested by noahz.

What you can do if you don't want to play around with Profiles is to use properties in your pom file.

Example:

<project>
    <groupId>abc</groupId>
    <artifactId>def</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <myProperty>someValue</myProperty>
    </properties>

    <build>
        <plugins>
            <plugin>
                ...
                <configuration>
                    <outputDir>${myProperty}</outputDir>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

You can use the property wherever you want within the pom file and you can even use the property when filtering resources.

The property could be empty by default and then have a new value set from command line:

mvn package -DmyProperty=anotherValue

And the anotherValue would be propagated to wherever it is used in pom.


You can read about Maven Resource Filtering here.

If you place a file in src/main/resources it could be filtered with the above property:

src/main/resources/important-stuff.properties

some.nice.property = Nice!
some.variable.property = ${myProperty}

And this should be added to the pom:

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

The filtered important-stuff.properties would end up in target/classes and in the jar and look like this:

some.nice.property = Nice!
some.variable.property = anotherValue

Resource filtering is really handy.

Upvotes: 0

basiljames
basiljames

Reputation: 4847

You can use maven -P to select a maven profile and in turn selec the property files for it.

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <resource.prefix>dev</resource.prefix>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <resource.prefix>prod</resource.prefix>
        </properties>
    </profile>
</profiles>

Now access your resource file as ${resource.prefix}_config.properties. So when the profile is prod, the resource file prod_config.properties will be taken.

Upvotes: 0

Dinesh
Dinesh

Reputation: 1128

Better Approach would be have different property/config file depending upon environment.

  1. Prod
  2. Dev

Keep the two set of values in two different file. At times specify the file name.

Upvotes: 3

noahlz
noahlz

Reputation: 10311

For 90% of build tasks, there's Maven. For everything else, there's maven-antrun-plugin.

I suggest creating a custom ant script (which can be embedded in your pom.xml) that prompts for user input and writes out your config files using the Ant Input Task

Upvotes: 1

Related Questions