Skip to content

Instantly share code, notes, and snippets.

@gkhays
Last active May 2, 2017 19:39
Show Gist options
  • Save gkhays/fcc60d661526312a1235 to your computer and use it in GitHub Desktop.
Save gkhays/fcc60d661526312a1235 to your computer and use it in GitHub Desktop.
Maven configuration to create an uber JAR

Maven Artifacts

Record the steps to create an uber jar and to add information to a jar manifest. Below, I add a time stamp and "codification" information.

Manifest-Version: 1.0
Implementation-Title: Maven Custom JAR Manifest
Implementation-Version: 1.0.0-SNAPSHOT
Archiver-Version: Plexus Archiver
Built-By: gkh
codification: UTF-8 <-----
Implementation-Vendor-Id: org.gkh
Created-By: Apache Maven 3.2.1
Build-Jdk: 1.8.0_74
Implementation-Vendor: GKH
Timestamp: 20160505-0903 <-----

Add the following to the build/plugins section of pom.xml.

	<properties>
		<jarTarget>${project.build.directory}/bin</jarTarget>
	</properties>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>2.4</version>
				<configuration>
					<outputDirectory>${jarTarget}</outputDirectory>
					<useDefaultManifestFile>true</useDefaultManifestFile>
					<archive>
						<addMavenDescriptor>false</addMavenDescriptor>
						<manifest>
							<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
						</manifest>
						<manifestEntries>
							<Timestamp>${maven.build.timestamp}</Timestamp>
							<codification>${project.build.sourceEncoding}</codification>
						</manifestEntries>
					</archive>
				</configuration>
			</plugin>
		</plugins>
	</build>

Settings

By default Maven will download snapshots daily.

<updatePolicy>interval:5</updatePolicy>

Is there a way to make Maven download snapshot versions automatically?

To update snapshots, try with the -U option

-U,--update-snapshots                  Forces a check for updated
                                       releases and snapshots on remote
                                       repositories

http://stackoverflow.com/a/7714033/6146580 http://stackoverflow.com/a/4098009/6146580

Each repository in the project has its own update policy:

  • always - always check when Maven is started for newer versions of snapshots
  • never - never check for newer remote versions. Once off manual updates can be performed.
  • daily (default) - check on the first run of the day (local time)
  • interval:XXX - check every XXX minutes

Use always if you always want Maven to download a newer version of snapshots, if available (Maven will always check the remote repository but only download if the version is newer).

References

  1. If you use a version of Maven >= 2.1.0-M1, then you can use the ${maven.build.timestamp} property. For more info, see: Available Variables.
  2. Answer from How do I add time-stamp information to Maven artifacts?.
  3. Maven jar plugin example, by Francisco Hernandez
  4. Inject Build Time (timestamp) Property Using Maven, by Aaron Gadberry
  5. Manifest Sections from The Apache Maven Project
  6. How to create a jar file with Maven, by mkyong
  7. Settings Reference
  8. How does the updatePolicy in maven really work?, answer by Pascal Thivent
<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>
<!--<parent>
<groupId>org.gkh.demo</groupId>
<artifactId>gkh-top</artifactId>
<version>0.1</version>
<relativePath>../build/gkh-top</relativePath>
</parent>-->
<artifactId>mvn-demo</artifactId>
<groupId>org.gkh.demo</groupId>
<version>0.1</version>
<name>Maven Demo</name>
<organization>
<name>GKH</name>
<url>http://gkhays.github.io</url>
</organization>
<inceptionYear>2015</inceptionYear>
<issueManagement>
<system>GitHub</system>
<url>https://github.com/gkhays/JavaSnippets/issues</url>
</issueManagement>
<properties>
<jersey.version>1.19</jersey.version>
</properties>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source />
<target />
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>org.gkh.demo.Main</mainClass>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>postgresql-9.2-1003.jdbc4.jar jersey-bundle-${jersey.version}.jar jettison-1.1.jar jsr311-api-1.1.jar</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>uber</shadedClassifierName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.gkh.demo.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment