The recently developed project needs to write an SDK for a jdk1.7 project, and this jdk1.7 project is not a maven project, but a kind of original java web. In that case, the idea is there. When developing this SDK, I definitely want to use Maven. When packaging, it can be made into a common jar package, and the third-party dependencies can also be made into the jar package. It should also be noted that the development and compilation should use jdk1.7, so that this jar package can be directly provided to them without JDK version problems. With a clear idea, we began to develop a demo.
There are many scenarios in which a maven project and a third-party dependency are made into ordinary jars without main class. For example, many people like to package HttpClient by themselves, but they can't develop a new service. Just re package it or copy and paste it. For another example, I have written several tool classes, which are common. For example, these can be typed as ordinary jars without main class. The next time you develop a new service, you can directly reference them.
Let's take a look at the demo:
This is the directory of the demo. It's a simple ApplicationUtil. Take a look at this:
A test method returns a string.
You can see pom XML, a maven project, this pom XML is the key. Take a look:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hikvision</groupId> <artifactId>stair-client</artifactId> <version>1.0-SNAPSHOT</version> <name>stair-client</name> <description>PDM STAIR</description> <properties> <java.version>1.7</java.version> </properties> <dependencies> <!--JSON--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.70</version> </dependency> <!--guava--> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>27.1-jre</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> </dependency> <!--log4j--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.12.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.12.1</version> </dependency> <!--httpclient--> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.10</version> </dependency> </dependencies> <build> <plugins> <!--Specify the compilation level of the project--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <!-- Encoding format --> <encoding>UTF-8</encoding> </configuration> </plugin> <!--Enter dependencies together jar Package, i.e. heavyweight jar--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
You can see that jdk uses 1.7 and introduces many third-party dependencies. Finally, Maven assembly plugin is used to add the third-party dependencies into the jar package.
This is all right. Then, package it with maven's package. Take a look at the results after packaging:
You can see that there are two jars. The above one is a lightweight jar without third-party dependencies. The following one with jar with dependencies is a heavyweight jar that contains third-party dependencies. You can decompile this heavyweight jar:
You can see that the project you wrote and the external references are all tied together. Although it is a little messy, the goal has been achieved. The
You can test whether this heavyweight jar can be directly referenced by a non maven project:
This is an original java web project. The jar package is directly referenced. You can see that it is ok.