Child pages
  • Tutorial: Build a JAR file with Maven in 5 minutes
Skip to end of metadata
Go to start of metadata

This is a continuation of the Maven in 60 seconds tutorial

Project model

Maven likes to give things coordinates to help locate them. Maven coordinates have three components:

  • the group identifier - this is usually a DNS name in reverse order
  • the artifact identifier - this is to identify artifacts within the group
  • the version - this is to distinguish different versions of the same artifact.

Before we can create a project model, we need to decide what the coordinates of the artifacts produced by the project model will be.

In this case, we are creating a .jar file for local consumption only, so we will use the coordinates

  • group id: localdomain.localhost.tutorial
  • artifact id: java-archive
  • version: 1.0-SNAPSHOT

This gives us the following project model

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>localdomain.localhost.tutorial</groupId>
<artifactId>java-archive</artifactId>
<version>1.0-SNAPSHOT</version>
</project>

We save the project model in a file called pom.xml

Convention over configuration

Maven uses convention over configuration, this means that you only need to tell Maven the things that are different from the defaults. 

For example, the default packaging is jar, so conveniently for us here, as we want to build a .jar file, we don't have to tell Maven what packaging to use.

There are similar conventions that Maven uses, for example Maven expects that the Java source code to be compiled and packaged within the .jar file will be found in the directory src/main/java and any additional resources that should be copied into the .jar file will be found in the src/main/resources directory

So we can use these defaults to get building our .jar file straight away, we can create a simple Java file, src/main/java/localdomain/localhost/tutorial/Main.java:

package localdomain.localhost.tutorial;
public class Main {
  public static void main(String[] args) {
    System.out.println("Hello world");
  }
}

And then we can ask Maven to package up the .jar file for us

$ mvn package
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building java-archive 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ java-archive ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ java-archive ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ java-archive ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ java-archive ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ java-archive ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ java-archive ---
[INFO] Building jar: target/java-archive-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.923s
[INFO] Finished at: Fri Jan 03 16:10:54 GMT 2014
[INFO] Final Memory: 12M/156M
[INFO] ------------------------------------------------------------------------

By convention Maven puts the .jar file in the target directory we can take a look at what is inside:

$ jar tvf target/java-archive-1.0-SNAPSHOT.jar 
     0 Fri Jan 03 16:10:56 GMT 2014 META-INF/
   133 Fri Jan 03 16:10:54 GMT 2014 META-INF/MANIFEST.MF
     0 Fri Jan 03 16:10:54 GMT 2014 localdomain/
     0 Fri Jan 03 16:10:54 GMT 2014 localdomain/localhost/
     0 Fri Jan 03 16:10:54 GMT 2014 localdomain/localhost/tutorial/
   577 Fri Jan 03 16:10:54 GMT 2014 localdomain/localhost/tutorial/Main.class
     0 Fri Jan 03 16:10:56 GMT 2014 META-INF/maven/
     0 Fri Jan 03 16:10:56 GMT 2014 META-INF/maven/localdomain.localhost.tutorial/
     0 Fri Jan 03 16:10:56 GMT 2014 META-INF/maven/localdomain.localhost.tutorial/java-archive/
   180 Fri Jan 03 16:10:22 GMT 2014 META-INF/maven/localdomain.localhost.tutorial/java-archive/pom.xml
   134 Fri Jan 03 16:10:54 GMT 2014 META-INF/maven/localdomain.localhost.tutorial/java-archive/pom.properties

And we can run the code from the .jar file

$ java -cp target/java-archive-1.0-SNAPSHOT.jar localdomain.localhost.tutorial.Main
Hello world

Understanding the build process

So what exactly happens when you type mvn package?

 

 

  • No labels