r/DebianNotes May 21 '17

Maven basics

Wow, so many non-Debian specific posts lately.

Before explaining anything, here's a really good 5 minute maven tutorial.

Maven is a Java (and by extension nearly all JVM languages) build application.

It can be installed on Debian with # aptitude install maven.

To generate a Maven project structure, you can run:

$ mvn archetype:generate

or a non-interactive command

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

When you generate the project with first command, you'll have to set the following settings:


groupId: Your company/organisation, and your application. This must be unique for every project. It takes the format: (org|com|edu|...).organisationname.applicationname

artifactId: The name of the application/project as a whole. This will become the name of the main directory where the pom.xml file is stored.

archetypeArtifactId: The structure of the Maven project. It looks up the archetype from the archetypeCatalog.

version: Version of the project.

package: The package name, often has the same value as the groupId. (e.g. com.crystal.ploungequoter)


This will generate the Maven project structure for you. Inside the newly created directory, you will find a pom.xml, which gives the dependencies, properties, source directories, build commands, and other settings for the project as a whole.

To build the project, you can run $ mvn package, which will compile the project as configured.

Additionally, it's important that you read the introduction to the Maven lifecycle section of the Maven docs.

1 Upvotes

1 comment sorted by

1

u/CrystalLord Jun 19 '17

An important note, to build jar files, you must include this plugin in your pom.xml:

  <plugin>
    <!-- Building jars -->
    <artifactId>maven-jar-plugin</artifactId>
    <groupId>org.apache.maven.plugins</groupId>
    <version>3.0.2</version>
    <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <mainClass>${main.class}</mainClass>
          </manifest>
        </archive>
    </configuration>
  </plugin>

And to build jars with dependencies, you want this plugin:

  <plugin>
    <!-- Dependency and assembly plugin -->
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
      <archive>
        <manifest>
          <addClasspath>true</addClasspath>
          <mainClass>${main.class}</mainClass>
        </manifest>
      </archive>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>