Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
869 views
in Technique[技术] by (71.8m points)

maven - Activate a profile based on environment

Here's my scenario:

  • Maven 2.0.9 is our build system
  • We install code to multiple environments
  • All of our environment-specific properties are contained in property files, one for each environment
    • We currently read these properties into maven using the properties-maven-plugin; this sub-bullet is not a requirement, just our current solution

Goal:

  • Perform certain parts of the build (ie. plugin executions) only for certain environments
  • Control which parts are run by setting values in the environment-specific property files

What I've tried so far:

  • Maven allows plugins executions to be put inside pom profiles, which can be activated by properties; unfortunately these must be system properties - ie. from settings.xml or the command-line, not from properties loaded by the properties-maven-plugin

If possible, we'd like to keep everything encapsulated within the build workspace, which looks something like this:

project
    pom.xml
    src
       ...
    conf
       dev.properties
       test.properties
       prod.properties
    build-scripts
       build.groovy <-- the script that wraps maven to do the build
       install.groovy <-- ... wraps maven to do the install

Running a build looks like:

cd build-scripts
./build.groovy
./install.groovy -e prod

Is there any possible way to accomplish these goals with the version of maven we are using? If not, is it possible with a newer version of maven?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This isn't possible using just Maven. (See also How to activate profile by means of maven property?) The reason is that profiles are the first thing evaluated before anything else to determine the effective POM.

My suggestion is to write some preprocessor that parses your environment specific property files and converts them to the required system properties before launching Maven. This script can be included in your ~/.mavenrc so that it runs automatically before Maven is launched. Here is an example script that that assumes the properties file is in a fixed location:

properties=`cat /etc/build-env.properties`
while read line; do
   MAVEN_OPTS="$MAVEN_OPTS -D$line"
done <<< "$properties"

If the properties file is not fixed, you'll just need to add something to the script to discover the location (assuming it is discoverable).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...