This document describes how developers and contributors should write code. The reasoning of these styles and conventions is mainly for consistency, readability and maintainability reasons.
All working files (java, xml, others) should respect the following conventions:
and the following style:
Note: The specific styles and conventions, listed in the next sections, could override these generic rules.
The Maven style for Java is mainly:
public class MyMojo { // ---------------------------------------------------------------------- // Mojo components // ---------------------------------------------------------------------- /** * Artifact factory. * * @component */ private ArtifactFactory artifactFactory; ... // ---------------------------------------------------------------------- // Mojo parameters // ---------------------------------------------------------------------- /** * The POM. * * @parameter expression="${project}" * @required */ private MavenProject project; ... // ---------------------------------------------------------------------- // Mojo options // ---------------------------------------------------------------------- ... // ---------------------------------------------------------------------- // Public methods // ---------------------------------------------------------------------- /** * {@inheritDoc} */ public void execute() throws MojoExecutionException { ... } // ---------------------------------------------------------------------- // Protected methods // ---------------------------------------------------------------------- ... // ---------------------------------------------------------------------- // Private methods // ---------------------------------------------------------------------- ... // ---------------------------------------------------------------------- // Static methods // ---------------------------------------------------------------------- ... }
The following sections show how to set up the code style for Maven in IDEA and Eclipse. It is strongly preferred that patches use this style before they are applied.
Download maven-idea-codestyle.xml and copy it to ~/.IntelliJIDEA/config/codestyles then restart IDEA. On Windows, try C:\Documents and Settings<username>\.IntelliJIDEA\config\codestyles
After this, restart IDEA and open the settings to select the new code style.
Download maven-eclipse-codestyle.xml.
After this, select Window > Preferences, and open up the configuration for Java > Code Style > Code Formatter. Click on the button labeled Import... and select the file you downloaded. Give the style a name, and click OK.
For consistency reasons, our Java code convention is mainly:
TO BE DISCUSSED
The Maven style for XML files is mainly:
<aTag> <simpleType>This is a simple type</simpleType> <complexType> <simpleType>This is a complex type</simpleType> </complexType> </aTag>
In some cases, adding comments could improve the readability of blocks, for instance:
<!-- Simple XML documentation -->
or
<!-- ====================================================================== --> <!-- Block documentation --> <!-- ====================================================================== -->
No generic code convention exists yet for XML files.
The team has voted during the end of June 2008 to follow a specific POM convention to ordering POM elements. The consequence of this vote is that the Maven project descriptor is no more considered as the reference for the ordering.
The following is the recommended ordering for all Maven POM files:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion/> <parent/> <groupId/> <artifactId/> <version/> <packaging/> <name/> <description/> <url/> <inceptionYear/> <organization/> <licenses/> <developers/> <contributors/> <mailingLists/> <prerequisites/> <modules/> <scm/> <issueManagement/> <ciManagement/> <distributionManagement/> <properties/> <dependencyManagement/> <dependencies/> <repositories/> <pluginRepositories/> <build/> <reporting/> <profiles/> </project>
Comments:
Note: The team plans to create a Maven plugin with reorder and reformat goals (See MOJO-928).
For consistency and readability reasons, XDOC files should respect:
For readability reasons, FML files should respect: