Resolving Maven JAR Packaging and Dependency Issues for Third‑Party Consumption
This article explains common Maven commands, describes why a JAR built for execution cannot be imported by a third‑party project, and provides step‑by‑step solutions using the maven‑compiler‑plugin and maven‑assembly‑plugin to create dependency‑inclusive JARs.
The author encountered a requirement to deliver a utility JAR to a third‑party that could not access the private repository, leading to import failures and missing dependencies.
1. Maven common commands – A quick reference of frequently used Maven goals such as clean , validate , compile , test , package , install , verify , site , and deploy is provided.
2. Unable to import after packaging – After building the project with mvn package , the generated JAR could be copied to the third‑party's libs directory and referenced with systemPath , but the IDE still reported import errors because the default packaging plugin creates an executable JAR, not a library.
Solution: replace the packaging plugin with the standard maven-compiler-plugin :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>After rebuilding, the import error disappears, but a new NoClassDefFoundError for org/apache/commons/codec/binary/Base64 appears, indicating a missing transitive dependency.
3. Missing additional JAR dependencies – The original JAR (≈14 KB) does not contain the commons-codec library (≈339 KB). Two approaches are suggested: bundle the dependencies into the JAR or ask the third‑party to add them manually.
Recommended solution: use the maven-assembly-plugin to create a “jar‑with‑dependencies” package.
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
</dependency>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>Running mvn assembly:assembly produces a large JAR (≈15.4 MB) that includes all required dependencies, which can be safely handed to the third‑party without further errors.
The article concludes with reference links to a CSDN blog and the official Maven dependency mechanism guide.
Wukong Talks Architecture
Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.