Maven archetype - optional property, empty by default -


i want able generate project archetype one of properties being empty default, or populated if specified on command line.

this archetype-metadata.xml:

<?xml version="1.0" encoding="utf-8"?> <archetype-descriptor name="basic">     <requiredproperties>         <requiredproperty key="compilerversion"/>         <requiredproperty key="dependencyclassifier">             <defaultvalue></defaultvalue>         </requiredproperty>     </requiredproperties> </archetype-descriptor> 

this not working. if there non-whitespace value in dependencyclassifier, works flawlessly. however, cannot work default value being "empty".

the error when defaultvalue empty/null is:

[error] failed execute goal org.apache.maven.plugins:maven-archetype-plugin:2.4:generate (default-cli) on project standalone-pom: archetype com.avast.archetype:compile-java:1.0.21 not configured [error] property dependencyclassifier missing. [error] -> [help 1] 

alternatively, accept being able pass empty-value (or "whitespace" value) on command line. again, using "-ddependencyclassifier= " not work.

how specify property should empty default?

thanks


what want achieve able create project archetype, contains dependency declaration:

    <dependencies>         <dependency>             <groupid>com.example</groupid>             <artifactid>artifact</artifactid>             <version>${dependencyversion}</version>             <classifier>${dependencyclassifier}</classifier>         </dependency>     </dependencies> 

and need able instantiate ${dependencyclassifier} on archetype creation. possible ${dependencyversion}, because never empty. classifier, however, should empty default.

this how successed meeting requirements:

in template pom.xml file to-be-generated maven project, place empty property, don't use placeholder of classifier value, there still use archetype requiredproperty placeholder following:

<properties>     <!-- empty placeholder, document accordingly -->     <empty.property></empty.property> </properties>  <dependencies>     <dependency>         <groupid>junit</groupid>         <artifactid>junit</artifactid>         <version>3.8.1</version>         <scope>test</scope>         <classifier>${dependencyclassifier}</classifier>     </dependency> </dependencies> 

then, in archetype-metadata.xml have following:

<requiredproperties>     <requiredproperty key="dependencyclassifier">         <defaultvalue>${empty.property}</defaultvalue>     </requiredproperty> </requiredproperties> 

note matchings:

  • the ${empty.property} default value here non existing property archetype plugin, accepted (an empty value not work indeed).
  • the dependencyclassifier has default value, ${empty.property}
  • on generated project though, empty.property empty (if dependencyclassifier not specified during mvn archetype:generate call), because exists maven property, empty value, makes whole valid maven: property value injected archetype declared property in final pom , such not breaking anything.
  • the classifier hence have either dependencyclassifier value (specified @ runtime) or empty.property (its default value), empty
  • an empty classifier treated maven no classifier, such providing both behaviors.

to summarize:

invoking

mvn archetype:generate ... -ddependencyclassifier=something 

would populate classifier element something, our desired classifier @ runtime.

instead, not specifying value leave classifier ${empty.property}, is, empty. maven no classifier (nor complain).

the drawback of approach keep empty.property property no meaning in final pom.xml, it's trick may shock purists, job.


Comments

Popular posts from this blog

android - How to read a column value in parse.com without accessing an object or a pointer -

timer - Java TimerTask cancel -

ios - NSDateFormatter: error in converting date -