5

I'm building a project where the final war/jar size is very big. I am not sure how to reduce the size of the jar.

How to build only the jars required for the project?

Currently we just given maven build plugin in the dependencies and artifact id on the top which builds the war or jar

Please advise.

Thanks.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
user2323036
  • 1,515
  • 5
  • 19
  • 27

3 Answers3

4

use dependency:analyze on your project to analyze used and unused dependencies, so you can exclude unwilling dependency files, i search a lot for a plugin to do it automatically, but I get disappointed in it. if you find please inform me.

https://maven.apache.org/plugins/maven-dependency-plugin/analyze-mojo.html

Mahdi Rajabi
  • 582
  • 1
  • 4
  • 11
3

JAR files typically don't include dependencies, so unless you're building a so-called fat JAR, they should never get very big.

For WAR files:

  • Remove unneeded dependencies from your POM.
  • Optionally, move static assets (images etc.) to a web server instead of including them in your application.
  • If you have dependencies that are being used by multiple applications, include them on the application server level instead of inside your WAR file.
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
1

You can use the combination of two mvn commands:

1] mvn dependency:tree -> to understand the entire structure of jars in your project

2] mvn dependency:analyze -> as mentioned above to understand used and unused dependencies

after that you can start removing unused dependencies and always make sure to do mvn clean install and run your project locally to see if there are dependency issues.