How do I create a JAR and install it in my local repository?

Create a JAR file

Creating a JAR file from maven project is simple one step process. A single command makes it possible to package the project as JAR. Navigate to project folder in command prompt and provide below command.

[ In my case, project name : benchmarkApp]

mvn package

Above command will result in executing something like below,

[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< com.talksinfo.benchmark:benchmarkApp >----------------
[INFO] Building benchmarkApp 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ benchmarkApp ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ benchmarkApp ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ benchmarkApp ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ benchmarkApp ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ benchmarkApp ---
[INFO]
[INFO] --- maven-jar-plugin:3.1.0:jar (default-jar) @ benchmarkApp ---
[INFO] Building jar: \\benchmarkApp\target\benchmarkApp-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.732 s
[INFO] Finished at: 2019-05-11T23:01:39+05:30
[INFO] ------------------------------------------------------------------------

We should be able to see benchmarkApp-0.0.1-SNAPSHOT JAR file in target folder of our project as below,

Installing JAR to my local repository

Now you’ll want to install the artifact you’ve generated (the JAR file) to your local repository (${user.home}/.m2/repository is the default location).  Following command will install the artifact to local repository,

mvn install

Above command will result in executing something like below,

[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< com.talksinfo.benchmark:benchmarkApp >----------------
[INFO] Building benchmarkApp 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ benchmarkApp ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ benchmarkApp ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ benchmarkApp ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ benchmarkApp ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ benchmarkApp ---
[INFO]
[INFO] --- maven-jar-plugin:3.1.0:jar (default-jar) @ benchmarkApp ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ benchmarkApp ---
[INFO] Installing \\\benchmarkApp\target\benchmarkApp-0.0.1-SNAPSHOT.jar to \talksinfo\.m2\repository\com\talksinfo\benchmark\benchmarkApp\0.0.1-SNAPSHOT\benchmarkApp-0.0.1-SNAPSHOT.jar
[INFO] Installing \\benchmarkApp\pom.xml to \talksinfo\.m2\repository\com\talksinfo\benchmark\benchmarkApp\0.0.1-SNAPSHOT\benchmarkApp-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.598 s
[INFO] Finished at: 2019-05-11T23:22:51+05:30
[INFO] ------------------------------------------------------------------------

This is likely the vast majority of what projects will be doing with Maven and if you’ve noticed, everything you’ve been able to do up to this point has been driven by project’s model or POM file. There are a great number of Maven plugins that work out of the box with even a simple POM like we have above.

There are plenty of other standalone goals that can be executed as well, for example:

mvn clean

This will remove the target directory with all the build data before starting so that it is fresh.

Conclusion

Hence we saw about mvn package, install, clean commands and how it works.

Please follow and like us: