1 Simple POM

Maven projects are defined with an XML file named pom.xml. This file gives the project’s name, version, and dependencies that it has on external libraries. Here is an example.

<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.springframework</groupId>
    <artifactId>gs-maven</artifactId>
    <packaging>jar</packaging>
    <version>0.1.0</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>hello.HelloWorld</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  • <modelVerion> POM model version
  • <groupId> Domain name of the group or organization.
  • <artifactId> Name to the project’s library artifact (name of JAR file)
  • <version> Version of the project
  • <packaging> How it should be packaged (in JAR or WAR file)

2 Basic command

mvn compile
mvn package
mvn install

The first command compiles the project, and the .classfiles shold be in the target/classes directory. The second command compile the code, run tests and finish by packaging the code up in a JAR file in the target directory. Tht third command does the same thing as the second command, then copy the JAR file into the local dependency repository, under the directories with name of groupId, artifactId and version. So on my machine, its location is ~/.m2/repository/org/springframework/gs-maven/0.1.0/gs-maven-0.1.0.jar.

Add dependencies of project into the pom.xml file, in the <project> element.

<dependencies>
     <dependency>
         <groupId>joda-time</groupId>
         <artifactId>joda-time</artifactId>
         <version>2.2</version>
     </dependency>
 </dependencies>

This description will tell the maven to get the joda-time package as external library. You can specify a <scope> element to specify if the dependencies are required for compiling the code but will be provided during runtime by provided; or decalre the dependencies are only necessary for compiling and running tests.

When I compile the command, maven downloads the joda-time pakcage from https://repo.maven.apache.org/maven2/joda-time/. Does the maven use its domain when the group id does not contain the domain by default?

You can also create a project from scratch

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

mvn scala:console can launch the scala console with the pom configurations (you have to specify maven-scala-plugin).

3 Directory layout

project
    src
        main
            java
            [scala]
            resources
                environment.properties
                environment.test.properties
                environment.prod.properties
                config
                    application.yml
                    application-dev.yml
                    application-[profile].yml
                [other]
        test
            java
            [scala]
    target
        classes
            the classes with the same structure as src/main
            the directories in src/main/resources
        test-classes
            the classes with the same structure as src/mainproject
        xxx.jar
    target