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.
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
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.
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.
Related
I have seen many threads regarding deploying a jar file for maven projects. But I am having different requirements.
I don't have a main class in my maven project.
I have some dependencies in my pom.xml file.
I need to deploy a jar file with less size which can take the dependencies through its pom file. (I am currently deployed jar with dependencies using IntelliJ IDEA, now it is 56 MB. I need it to be less capacity, using the above way or an alternative way)
Thank you.
When you build a jar with maven (like clean install or clean deploy), the jar:
does not need to have the main class
uses the dependencies in the pom.xml
does not bundle external dependencies into that unless you specifically tell it to
So your requirements seem to be met automatically.
Please try below steps :- Hope so its will be help you
pack200 can drastically reduce the JAR size.
heck your dependencies via:
mvn dependency:analyze
or take a look at the dep tree like this:
mvn dependency:tree
do you have unused dependencies?if yes Remove the unused dependency in pom.xml file.
I have finally find the solution for this.
As #JF Meier explained mvn clean install does the job. But before that I had to move to the step definitions which was inside the test packages. Actually those step definitions are not belongs to the test package since it won't test my project.
Thanks All.
I'm looking for a way to create an executable jar file for my JavaFX application.
I'm currently using the zenjava maven plugin and I use the jfx:jar goal. Doing this, all the dependencies are generated on a folder called /lib.
The problem arises when one of the dependencies (a separate project handled by another group) is updated, I would need to rebuild my jar again.
Is it possible to just maybe refer to the dependencies using a pom? (not point to the lib) So I would only update the pom every time a dependency is updated?
Thanks in advance!
Short answer: nope, not possible
Long answer: the JavaFX-Maven-Plugin is usable for development (via mvn jfx:run) and deployment (via mvn jfx:jar or mvn jfx:native).
You are generating a potential deployable package, which already contains everything needed to execute on some targeted machine. You would have to encapsulate your javafx-application with a "pre-loader" which downloads all your required stuff to be executable.
What is the idea behind distributing an application which needs some internet-connection to gather all the required dependencies?
Disclaimer: I'm the maintainer of the javafx-maven-plugin.
I'm building a SonarQube plugin and I need to use Maven to achieve it, I must use the goals clean package to create the jar file that acts as the plugin. My problem is that I need to load either a local jar file, which didn't worked as I tried using: <systemPath>${project.basedir}/lib/epsilon-1.3-core.jar</systemPath> to load it as a dependency and Maven wasn't able to resolve it or I must load three external projects which are at my workspace but I don't know how to tell maven to include them.
Could someone help me to do this? I'd really appreciate it! Using my own Maven repo (with Nexus for example) isn't an option.
I had to do a local maven repository loading the plugins of Epsilon as jar files, it's a temp solution while the Epsilon Team setup a maven repo.
I have a project that generates some classes and resources that should then be packaged into a jar.
The class that does the generating has several dependencies. The generated classes have no dependencies.
My initial solution was to use the maven assembly plugin to only include the generated files. Unfortunately, the packaged pom includes all the dependencies required to do the generation. Consumers of this jar pull in a bunch of unnecessary dependencies.
I looked into the maven shade plugin. I compile once, run the generator class with mojo's exec plugin, the compile a final time. Then shade of course runs in the package phase. It creates a dependency-reduced-pom.xml without the excessive dependencies. So run mvn clean package and look in target/foo.jar. I expect the jar in the meta-inf folder to be the reduced dependency jar. But it's the full pom. I don't know how have the generated pom replace the one that is packaged.
Another poor solution I tried was using multiple profiles with their own dependency section. I run maven once with the generating profile, then again with the packaging profile. This works, but sucks because you have to run multiple maven commands. It's not jenkins friendly.
Am I going about this the wrong way? How should I go about arranging a project that generates some code and some text files that are then packaged in a maven friendly artifact?
1) One possibility is to make all your dependencies <optional>true</optional>.
The pom will still have the dependencies but the projects using your library won't try to download them.
2) Use plugin dependencies with the exec plugin.
That works well if the generator class is defined on another project.
Ideally you would call your generator using a plugin and avoid adding those dependencies to your project. That may not be worth doing depending on what your project is for and how reusable your generator has to be.
3) If you are using maven-shade-plugin the dependency-reduced-pom.xml is normally used. Check your .m2 repository to see the contents of the pom that is copied there when you do mvn install. It's likely that its contents will match the dependency-reduced-pom.xml
I'm hacking on a Maven-based project with a lot of dependencies; the project is normally meant to be developed in Eclipse but I need to work on it from the command line.
How to build+execute the project in a sane way? Something like mvn run, but of course Maven is not meant for running Java projects (for some reason).
The problem is specifying all the dependencies on java's commandline, I don't even know how to autogenerate that. I can currently deal with it using the assembly:single maven plugin (using the jar-with-dependencies descriptor) which will package the dependencies to a single .jar for me.
However, there really is a lot of dependencies and the assembly phase can take about two minutes, greatly disrupting my hack-test cycles so I'm looking for other ways to run the project with minimum build overhead - any recommendations, please?
Note: One possibility is running it in Eclipse once and capturing the java commandline. However, that's just a one-time hack, not a general solution in case I change pom.xml later or come to another project from the suite without Eclipse access anymore.
Have a look at the maven exec plugin
mvn exec:java -Dexec.mainClass="com.example.Main"
if you do this frequently, you can of course configure it via plugin configuration.
Regarding finding out project dependencies - you can use maven dependency plugin
http://maven.apache.org/plugins/maven-dependency-plugin/list-mojo.html
If you want to put them into file it'd be smth like
mvn dependency:list > dependencies.txt
See this question: How can I create an executable JAR with dependencies using Maven?. You can use the dependency-plugin to generate all dependencies in a separate directory before the package phase and then include that in the classpath of the manifest.
I see three solution to this:
onejar-maven-plugin - faster than assemlby with jar-with-dependencies descriptor
With onejar-maven-plugin, you'll (...) get a nice clean super jar with the dependency jars inside.
Spring Boot Maven Plugin - but this is dedicated to Spring projects
Maven Assembly Plugin with custom descriptor. This custom descriptor should grab all dependencies into lib folder, maven-jar-plugin should set Class-Path in Manifest.fm according to this new location. After this you can simply execute your program or zip your jar with lib folder as distribution.
After this is possible to run your program on your computer or any other with one command:
java -jar myjar.jar