Issue
Using java and Maven
, what is the convention for maven properties?
I am posting 2 examples here, both of which are in widespread usage. Which one is correct, according to convention?
Example A
<properties>
<hibernate.version>4.3.8.Final</hibernate.version>
<hsqldb.version>2.3.2</hsqldb.version>
<log4j2.version>2.0.2</log4j2.version>
</properties>
Example B
<properties>
<hibernateVersion>4.3.8.Final</hibernateVersion>
<hsqldbVersion>2.3.2</hsqldbVersion>
<log4j2Version>2.0.2</log4j2Version>
</properties>
Edit:
Here is a link to a Maven Properties Guide. Some examples of maven properties include ${project.build.directory}
(dot case) and ${project.build.outputDirectory}
(both dot case and camel case).
And the official documentation Maven POM Reference suggests an example property named <someVar>
(camel case).
Solution
After reading the relevant documentation, the answer to this was clear all along.
The apparent conflict between dot.case
and camelCase
is that one is used to reference the hierarchical structure within the POM whilst the other is used for variable naming.
For example, let us look at ${project.build.outputDirectory}
. The dot notation here, as far as I can understand, refers to the pom structure where the variable is located, whereas the variable name itself is indeed in camel case.
<project>
<build>
<outputDirectory>/opt/foo</outputDirectory>
</build>
</project>
In other words, the convention is as follows:
- To refer to variables located elsewhere in the POM, combine path segments such as
project
orbuild
with the.
separator, i.e. use dot.case. Examples:project.build.<variable>
maven.compiler.<variable>
- To name the actual path segments, including the variable name itself (last segment), use lowerCamelCase. Examples:
outputDirectory
(as inproject.build.outputDirectory
)target
(as inmaven.compiler.target
)
It is worth noting that most open source projects (including e.g. Spring Boot, pre-Gradle-migration - see here) use .version
as a path segment and not as an addition to the variable name.
Consistency is the most important consideration. If your codebase is using someDependencyVersion
, stick to that - else prefer someDependency.version
.
Answered By - vikingsteve
Answer Checked By - Marie Seifert (JavaFixing Admin)