Skip to content

Instantly share code, notes, and snippets.

@AndersDJohnson
Last active June 5, 2019 13:34
Show Gist options
  • Save AndersDJohnson/5653994 to your computer and use it in GitHub Desktop.
Save AndersDJohnson/5653994 to your computer and use it in GitHub Desktop.
apache maven notes

(tested with Maven 3.0.4, POM 4.0.0)

Resources

Init

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart # -DarchetypeVersion=5-SNAPSHOT

Effective POM

mvn help:effective-pom

Install

mvn clean install

Package

mvn package

Assemble jar with dependencies

via the maven assembly plugin

Add to pom/xml:

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <mainClass>fully.qualified.package.name.of.your.main.class</mainClass>
            </manifest>
          </archive>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
    </plugins>
  </build>

To assemble:

mvn clean compile assembly:single

Eclipse

(tested with Eclipse Juno 4.2)

m2eclipse plugin for Eclipse

(tested with org.eclipse.m2e.feature 1.3.0.20130129-0926)

Convert to Maven project

Eclipse -> Project Explorer -> project context menu -> Configure -> Convert to Maven Project

Make sure your pom.xml specifies the right directory for source code to prevent "Cannot nest '...' inside library 'Project/src" errors:

<project ...>
  ...
  <build>
    <sourceDirectory>src/main/java</sourceDirectory>
    ...

Updating dependencies

Eclipse -> Project Explorer -> project context menu -> Maven -> Update Project...

Manually via maven-eclipse-plugin for Maven

Init

Before importing into Eclipse:

mvn eclipse:eclipse

Clean

In case of errors:

mvn eclipse:clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment