cyotee doge
cyotee doge

Reputation: 1138

Offline Caching of Maven Archetypes

I'd like to be able to start a project from a Maven archetype while being offline. But I can't find clear instructions on how to cache Maven archetypes for offline use. Does anyone have any advice?

Upvotes: 2

Views: 2044

Answers (2)

Behe
Behe

Reputation: 7940

Maven's archetype mechanism relies on catalogs. Catalogs are XML files that maven inspects to see what archetypes are available. As Maven: The complete reference states in Chapter 12.4:

An Archetype catalog is an XML file which the Maven Archetype plugin can consult to locate archetypes in a repository.

You can create a catalog of your cached archetypes (already downloaded) by crawling your local repository for available archetypes. This is by invoking the crawl mojo:

mvn archetype:crawl

This will create an XML file in ~/.m2/repository/archetype-catalog.xml. (You can specify a path by appending -Dcatalog=<filename>)

Once created, you can use the command

mvn archetype:generate -DarchetypeCatalog=file://<fullpath of catalog file> 

e.g.

mvn archetype:generate -DarchetypeCatalog=file:///home/username/.m2/repository/archetype-catalog.xml 

to generate a project using the newly created catalog. It will only show the available (cached) archetypes to you and these can be used offline.

Addendum:

I guess you will use the mvn archetype:generate more often, so you might want to minimize typing by using this:

1) Generate the XML archetype catalog under .m2/archetype-catalog.xml -

mvn archetype:crawl -Dcatalog=/home/<username>/.m2/archetype-catalog.xml

2) Use mvn archetype:generate -DarchetypeCatalog=local (this accesses .m2/archetype-catalog.xml)

mvn archetype:generate -DarchetypeCatalog=local

It is a good idea to use this once online, so that all necessary plugins are downloaded. E.g., the Archetype Plugin and potentially others.

Upvotes: 1

A_Di-Matteo
A_Di-Matteo

Reputation: 27822

A Maven archetype is an artifact after all and as such it will be cached automatically by Maven at its first usage. Later usages will always run fetched artifacts first (from local cache). We could also force Maven to only use the cache (offline mode, as explained below).

So you could simply invoke the concerned archetypes once (i.e. for a dummy project) and have them offline for further invocations.

If you really want to cache it upfront, you could use the Maven Dependency Plugin and its get goal to add to your local Maven cache the archetype artifact.

For instance, let's cache the Maven Quickstart Archetype as following:

mvn dependency:get -DgroupId=org.apache.maven.archetypes \
   -DartifactId=maven-archetype-quickstart -Dversion=1.0

It will hence store on your local Maven cache the maven-archetype-quickstart-1.0.jar artifact.

If you don't know where your local Maven cache is, you can use the Maven Help Plugin and run:

mvn help:evaluate -Dexpression=settings.localRepository

As part of the verbose output, you will get the full path to your local Maven cache.

Since now the QuickStart Archetype is on our cache, we can run it using the -o flag (go offline, forced) for the Maven invocation

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes \ 
  -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.0 \
  -DgroupId=com.sample -DartifactId=test -Dversion=1.0-SNAPSHOT -B -o

As such, Maven will run an off-line execution and only use its local cache.


Note that you could also use the archetypeCatalog option while invoking archetype:generate and set it to local to only check the local catalog, but a forced execution to offline mode (-o) would better suit your need (forcing local catalog AND local cached archetypes).

Upvotes: 3

Related Questions