IntelliJ非Maven环境下生成最小化含依赖单Jar的方法
Great question! Since you’re not relying on Maven for dependency management, here are some practical, no-Maven ways to shrink your fat jar down to only the classes your code actually uses:
1. Use ProGuard (Most Recommended)
ProGuard is the go-to tool for this—it not only obfuscates code but also removes unused classes, methods, and fields automatically. Here's how to set it up:
- Download the latest ProGuard release and extract it to a local folder.
- Create a ProGuard configuration file (e.g.,
proguard.cfg) in your IntelliJ project, with rules tailored for HtmlUnit and Apache POI:
Tweak the# Keep your main class entry point -keep public class com.yourpackage.YourMainClass { public static void main(java.lang.String[]); } # Keep POI classes needed for reflection and core functionality -keep class org.apache.poi.ss.usermodel.** { *; } -keep class org.apache.poi.xssf.usermodel.** { *; } # Adjust based on which POI modules you use -keep class org.apache.xmlbeans.** { *; } -keep class org.openxmlformats.schemas.spreadsheetml.x2006.main.** { *; } # Keep HtmlUnit classes for JS engine and DOM handling -keep class com.gargoylesoftware.htmlunit.html.** { *; } -keep class com.gargoylesoftware.htmlunit.javascript.** { *; } -keep class net.sourceforge.htmlunit.corejs.** { *; } # Strip out unnecessary logging code -assumenosideeffects class java.util.logging.Logger { public *** log(...); }-keeprules based on exactly which parts of POI/HtmlUnit your code uses—if you don't use, say, POI's HSSF (xls) module, you can skip keeping those classes to save more space. - In IntelliJ, add a new "ProGuard" run configuration: point it to your input fat jar (the one IntelliJ generated), specify an output jar path, and link to your
proguard.cfgfile. - Run the ProGuard task, and it will analyze your code's dependencies to produce a trimmed-down jar with only the classes you need.
2. Manual Dependency Trimming (Good for Small Projects)
If your project is relatively small, you can manually curate the dependencies:
- Use IntelliJ's Analyze > Run Inspection by Name feature, search for "Unused declaration", and run the inspection to identify which parts of HtmlUnit/POI your code never touches.
- Extract the full HtmlUnit and POI jars, then delete all unused classes, resources, and package folders.
- Repackage the remaining files into custom trimmed jars.
- Update your IntelliJ Artifact configuration to replace the full dependency jars with your custom trimmed versions, then rebuild the fat jar.
Heads up: This method is time-consuming and risky—you might accidentally remove classes that are called via reflection. Always test the trimmed jar thoroughly, and use IntelliJ's "Find Usages" tool to double-check each class's usage.
3. IntelliJ Shadow Jar Plugin (With Custom Rules)
While Shadow Jar is often paired with Maven, you can use it in non-Maven projects with some manual setup:
- Install the Shadow Jar plugin in IntelliJ.
- Create a minimal
build.gradlefile in your project root with shrinkage rules:plugins { id 'java' id 'com.github.johnrengelman.shadow' version '7.1.2' } shadowJar { minimize() // Enables automatic unused class removal // Exclude specific unused packages exclude 'org/apache/poi/hssf/**' exclude 'com/gargoylesoftware/htmlunit/phantomjs/**' } - Run the
shadowJartask from the Gradle tool window. This will generate a slimmed-down jar, though you may need to tweak the exclude rules to match your project's actual dependencies.
内容的提问来源于stack exchange,提问作者Can Kucukgul

