Usually in some special cases, we need to package several different JDK versions of packages for a single component to support different JDK, and we can do this easily based on Maven.
1, add the following configuration in the project Pom file
<Projectxmlns= "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/maven-v4_0_0.xsd"> <modelversion>4.0.0</modelversion> <groupId>Cc.mzone</groupId> <Artifactid>Blog</Artifactid> <Packaging>Jar</Packaging> <version>0.1</version> <URL>http://www.mzone.cc</URL> <Build> <Plugins> <plugin> <Artifactid>Maven-compiler-plugin</Artifactid> <version>2.0.2</version> <Configuration> <Source>${jar.source}</Source> <Target>${jar.target}</Target> <encoding>UTF-8</encoding> </Configuration> </plugin> </Plugins> </Build> <Dependencies> <!--here to omit the dependency - </Dependencies> <Properties> <!--Omit attribute here - </Properties> <Profiles> < Profile> <ID>Default</ID> <activation> <Activebydefault>True</Activebydefault> </activation> <Properties> <Jar.source>1.6</Jar.source> <Jar.target>1.6</Jar.target> </Properties> </ Profile> < Profile> <ID>Jdk15</ID> <Build> <Plugins> <plugin> <Artifactid>Maven-jar-plugin</Artifactid> <executions> <Execution> <Phase>Package</Phase> <Goals> <goal>Jar</goal> </Goals> <Configuration> <classifier>Jdk15</classifier> </Configuration> </Execution> </executions> </plugin> </Plugins> </Build> <Properties> <Jar.source>1.5</Jar.source> <Jar.target>1.5</Jar.target> </Properties> </ Profile> </Profiles></Project>
2. Then execute the command package
# MVN Clean jar # mvn clean jar-p jdk15
The first command is packaged with the default profile, the compiled version is 1.6, the generated file is Blog-0.1.jar, and the second command package specifies that the JDK15 profile is used, the compiled version is 1.5, and the resulting file is Blog-0.1-jdk15.jar.
3. References in the project
For references in a project, you can use the following dependency notation:
<!--referencing the 1.6 compiled version -<Dependency> <groupId>Cc.mzone</groupId> <Artifactid>Blog</Artifactid> <version>0.1</version></Dependency> <!--referencing the 1.5 compiled version -<Dependency> <groupId>Cc.mzone</groupId> <Artifactid>Blog</Artifactid> <version>0.1</version> <classifier>Jdk15</classifier></Dependency>
Summing up the analysis, in order to achieve the same component using different compiled version, we use the Maven in the profile, classifier technology, according to the example I gave here, the practice can be summed up in the following points:
1, in the project Pom file definition of multiple profiles, each profile provides a compiled version;
2, specify the classifier configuration of plug-in Maven-jar-plugin in profile, this is the name of the specified filter, that is, the last to append to the jar package name, such as the jdk15 here;
3. When packaging, specify different profiles to package different versions, specify profile by-p parameter to compile and package;
4. Use this component in other projects to filter different versions by configuring the Classifier property;
Technology because of the need to change, their own back up, but also want to help everyone.
MAVEN2 package for different JDK versions