Maven file structure basics

This is an old blog post that sat in draft for years, int looks complete, so I thought I’d publish it. Hopefully it’s still upto date.

As I’m working in Java again and using Maven a lot to get my projects up and running. Whilst I’ve covered some of this stuff in other posts, they’ve tended to be part of working with some specific code. So this post is mean’t to be more about using Maven itself.

Maven convention file structure

By default the following file structure is expected

/src/main/java
/src/main/resources
/src/test/java

Optionally we might have the test/resources

/src/test/resources

POM starting point

Maven (be default) expects a file name pom.xml to exist which contains version information and may include plugins, code generation, dependencies etc.

<?xml version="1.0" encoding="UTF-8"?>
<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>4.0.0</modelVersion>

    <groupId>com.putridparrot.common</groupId>
    <artifactId>JsonPatch</artifactId>
    <version>1.0-SNAPSHOT</version>
</project>

Naming conventions (see references)

groupId – “identifies the project uniquely across all projects” hence might best be represented by the package name.
artifactId – is the name of the JAR
version – standard numbers with dots, i.e. 1.0, 1.0.1 etc. This is a string so in situations where we want a version to include the word SNAPSHOT (for example)

Maven commands

Before we get into more POM capabilities, let’s look at the basic set of Maven command’s we’ll use most.

Compiling to specific Java versions

<properties>
   <maven.compiler.source>1.8</maven.compiler.source>
   <maven.compiler.target>1.8</maven.compiler.target>
</properties>

OR

<build>
   <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
</build>

References

Guide to naming conventions on groupId, artifactId and version
Setting the -source and -target of the Java Compiler