Maven user properties for user

In my Maven assembly, I would like to be able to define default values โ€‹โ€‹(for example, to connect to the database) in pom.xml, but I would like the user to be able to override them without having to change them directly pom.xml.

As an example, in the Ant assembly, you can define the default properties in foo.properties, but Ant will look for overrides for each of them in foo.$USERNAME.properties. The latter, as a rule, is not checked for initial control, which eliminates the problem when developers accidentally fix their default property overrides. Does Maven have a similar object?

To make the problem more specific, suppose I have the following definition 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 directly editing pom.xml?

+5
source share
4 answers

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

+9
source

You can achieve this with build profiles. For more information, see Introduction to Profile Assembly .

+2
source

settings.xml, , <profiles>

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

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

, a -DrefreshDB mvn. , Maven, settings.xml

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

<activeProfiles>, <activation> .

+1

, ( ); , :

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

: ${variable}, /undefined, , , , ( , ). , , mvn clean install ( 20-40 , ). , , ...

By the way, if you are already used to Ant, I heard that you can call Ant tasks from maven in some way (I donโ€™t know how, though), maybe you could use it somehow?

0
source

All Articles