In maven, by default, the dependent JAR package is not scored into the executable jar package, and if you need to break the dependency into the executable jar package, you need to
pom
added in
maven-assembly-plugin
plug-ins, this is easy to implement, but it is not recommended in the formal development of such use, why? Because a slightly larger project has at least dozens of dependencies, each package will break these jar packages into the executable jar, making the resulting executable jar very large in size. As a standard practice, all dependent jar packages are entered into the Lib directory, and in the executable jar
MANIFEST.MF
You can specify the Lib path in the This is also very easy to achieve, not the focus of this article, the focus of this article is how to do not in the MAVEN Central warehouse jar package, or rely on the local jar package into the executable jar, and update
MANIFEST.MF
file.
For example, in my MAVEN project, you need to rely on the local jar, first copy the dependent jar to the src/main/resources/lib
directory, and refer to the following
1234567 |
<dependency> <groupid>com.yuewen</ GROUPID> <artifactid>lucene</ Artifactid> <version>1.0.0-snapshort< span class= "tag" ></VERSION> < scope>system</SCOPE> <systempath>${project.basedir}/src/main/resources/lib/ Lucene-1.0.0-snapshort.jar</SYSTEMPATH> </DEPENDENCY> |
This scope can only be the system scope, and the Systempath property specifies the path to the jar package.
The next step is to enter all dependent jar packages into the Lib directory in the following manner:
123456789101112131415161718192021222324 |
<!--The resources you depend on are all in the Lib directory--<plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-dependency-plugin</artifactid> <configuration> <outputdirectory>${project.build.directory}/lib</outputdirectory> <excludetransitive>false</excludetransitive> <stripversion>false</stripversion> </configuration> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputdirectory>${project.build.directory}/lib</outputdirectory> <excludetransitive>false</excludetransitive> <stripversion>false</stripversion> </configuration> </execution> </executions> </plugin> |
Now that you're in the MAVEN project, all the jars you rely on are being driven into the target/lib
directory, and the next key step is how to add the MANIFEST.MF
files. Add the following plug-in to the POM
1234567891011121314151617181920212223 |
<!--packaging plug-ins, adding Class-path and main-class--> to the jar package<plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-jar-plugin</artifactid> <version>3.0.2</version> <configuration> <archive> <!--Use your own manifest file, run normal- -<!--<manifestfile>src/main/resources/meta-inf/manifest. Mf</manifestfile>--><!--add manifest files using plug-ins, it is important to note that the jar name in manifest and the jar name version number suffix in the Lib folder must be consistent, otherwise the dependent jar cannot be found, there is a pit-to -<manifest> <addclasspath>true</addclasspath> <!--Specify a dependent resource path prefix --<classpathprefix>lib/</classpathprefix> <mainclass>cn.codepub.maven.test.main</mainclass> </manifest> <!--can add a jar package that relies on the local system to the manifest file--<manifestentries> <class-path>lib/lucene-1.0.0-snapshort.jar</class-path> </manifestentries> </archive> </configuration> </plugin> |
Run mvn clean package
Execute package, after completion, will contain the dependent resources of the Lib directory and executable jar in the same level directory, so at runtime, the executable java -jar xxx.jar
jar package can accurately find the dependent jar package. And in this way the executable jar size is very small, generally hundreds of KB. The complete MANIFEST.MF
file is shown below
1234567 |
Manifest-version:1.0built-by:wangxuclass-path:lib/lombok-1.16.12.jar Lib/guava-20.0.jar Lib/log4j-api-2.7.jar Lib /log4j-core-2.7.jar Lib/lucene-1.0.0-snapshort.jarcreated-by:apache Maven 3.3.3build-jdk:1.8.0_45main-class: Cn.codepub.maven.test.Main |
This address:http://www.codepub.cn/2017/06/13/Maven-introduces-local-dependency-jar-to-executable-jar-packages/
Maven introduces a local dependency jar into the executable jar package