Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting default values for custom Maven 2 properties

I have a Maven pom.xml with a plugin that I want to be able to control on the command line. Everything works otherwise fine, except even after searching the net a while I can't figure out how to set a default value for my control property:

<plugin>     ...     <configuration>         <param>${myProperty}</param>     </configuration>     ... </plugin> 

So if I run Maven with

mvn -DmyProperty=something ... 

everything's fine, but I'd like to have a specific value assigned to myProperty also without the -DmyProperty=... switch. How can this be done?

like image 989
Eemeli Kantola Avatar asked May 22 '09 18:05

Eemeli Kantola


People also ask

How do I set system properties in Maven?

To provide System Properties to the tests from command line, you just need to configure maven surefire plugin and use -D{systemproperty}={propertyvalue} parameter in commandline. Run Single Test with Maven : $ mvn test -Dtest=MessageUtilTest#msg_add_test -Dmy_message="Hello, Developer!"

What is Maven default packaging value?

Every Maven project has a packaging type. If it is not specified in the POM, then the default value "jar" would be used.

What are maven properties?

Maven properties are value placeholders, like properties in Ant. Their values are accessible anywhere within a POM by using the notation ${X}, where X is the property. Or they can be used by plugins as default values, for example: In your case you have defined properties as version of java.


2 Answers

You can have the property default value defined in <build>/<properties> or in a profile like shown below. When you supply the property value on command line with -DmyProperty=anotherValue then it will override the definition from the POM. That is, all definitions of property values in the POM are set only a default value for the properties.

<profile>     ...     <properties>         <myProperty>defaultValue</myProperty>                 </properties>     ...        <configuration>           <param>${myProperty}</param>        </configuration>     ... </profile> 
like image 60
akostadinov Avatar answered Sep 22 '22 00:09

akostadinov


Taylor L's approach works fine, but you don't need the extra profile. You can just declare property values in the POM file.

<project>   ...   <properties>     <!-- Sets the location that Apache Cargo will use to install containers when they are downloaded.           Executions of the plug-in should append the container name and version to this path.           E.g. apache-tomcat-5.5.20 -->      <cargo.container.install.dir>${user.home}/.m2/cargo/containers</cargo.container.install.dir>    </properties>  </project> 

You can also set properties in your user settings.xml file in the event that you want each user to be able to set their own defaults. We use this approach to hide credentials that the CI server uses for some plug-ins from regular developers.

like image 36
DavidValeri Avatar answered Sep 18 '22 00:09

DavidValeri