开发者

per-user Maven properties

In my Maven build I would like to be able to define default values (e.g. for database connection) in pom.xml, but I would like the user to be able to override these without having to modify pom.xml directly.

By way of example, in an Ant build you can define default properties in foo.properties, but Ant will look for overrides for each of these in a foo.$USERNAME.properties. The latter is generally not checked into source control, which eliminates the problem of developers accidentally committing their overrides of the default properties. Does Maven offer a similar开发者_Go百科 facility?

To make the problem a bit more concrete, assume I have the following defined in pom.xml

<properties>
  <db.url>jdbc:jtds:sqlserver://10.10.10.10:1433/somedb</db.url>
  <db.driver>net.sourceforge.jtds.jdbc.Driver</db.driver>
  <db.username>default_user</db.username>
  <db.password>secret</db.password>
</properties>

Can a user override these properties without editing the pom.xml directly?


You can specify properties on the command line using -Dpropertyname=value, or the user can specify properties in their .m2/settings.xml.


You can achieve this with build profiles. See Introduction to build profiles for more information.


I used settings.xml to override these properties by adding the following to the <profiles> section

<profile>
  <id>override-database-properties</id>
  <activation>
    <property>
      <name>refreshDB</name>
    </property>
  </activation>

  <properties>
    <db.schema_name>store_don</db.schema_name>
  </properties>
</profile>

In this case the overrides will only take effect if a -DrefreshDB is passed to the mvn command. To activate these overrides every time Maven is invoked also add the following to settings.xml

<activeProfiles> 
  <activeProfile>alwaysActiveProfile</activeProfile> 
</activeProfiles>

If the profile is added to <activeProfiles> then the <activation> element should be removed.


If you don't want to add properties on the command line, but set some settings once (and if you can accept the default being empty strings); you could use environment variables like this:

<something>${env.ENVNAME}</something>
<!-- something will be blank "" unless ENVNAME is set -->

Edit: I've been trying to find a way of defining a default values for ${variable} if the variable is empty/undefined, but I've not found anything, this seems to be missing (the frequent suggestion is to use profiles but it's not quite the same). I'd prefer if you could just set some environment variables and have sensible defaults so that you could just run mvn clean install (and not the 20-40 character strings I normally use to build our project). If default values is possible I'd love hear how...

BTW, if you're already used to Ant, I've heard that you can call Ant tasks from maven somehow (don't know how, though), maybe you could use that somehow?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜