`
blueram
  • 浏览: 757294 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

基于maven2打包不同jdk版本的包

 
阅读更多

指定修改jdk默认版本

修改{M2_HOME}/conf/settings.xml来指定默认的jdk。

 

<profiles>  
       <profile>    
            <id>jdk-1.6</id>    
            <activation>    
                <activeByDefault>true</activeByDefault>    
                <jdk>1.6</jdk>    
            </activation>    
            <properties>    
                <maven.compiler.source>1.6</maven.compiler.source>    
                <maven.compiler.target>1.6</maven.compiler.target>    
                <maven.compiler.compilerVersion>1.6</maven.compiler.compilerVersion>    
            </properties>    
    </profile>   
 </profiles>  

  通常在一些特别情况下,我们需要为单独某一个构件打包多个不同jdk版本的包,用来支持不同的jdk,基于maven我们就可以很方便的做到这点。

1、在项目的pom文件中加入如下配置

 

<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/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>
    <!-- 这里省略依赖 -->
  </dependencies>
  <properties>
    <!-- 这里省略属性 -->
  </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、然后执行命令打包

  1. # mvn clean jar
     
    # mvn clean jar -P jdk15
    第一条命令打包使用默认的profile,编译的版本是1.6,生成的文件是blog-0.1.jar;而第二条命令打包指定使用jdk15这个profile,编译版本是1.5,生成的文件是blog-0.1-jdk15.jar。
  2. 3、项目中引用

        对于项目中的引用,可以使用如下依赖写法:

    <!-- 引用1.6编译版本 -->
    <dependency>
      <groupId>cc.mzone</groupId>
      <artifactId>blog</artifactId>
      <version>0.1</version>
    </dependency>
     
    <!-- 引用1.5编译版本 -->
    <dependency>
      <groupId>cc.mzone</groupId>
      <artifactId>blog</artifactId>
      <version>0.1</version>
      <classifier>jdk15</classifier>
    </dependency>
     

     总结分析,为了达到对同一个构件使用不同的编译版本,我们综合使用了maven中的profile、classifier技术,按照我这里给出的例子,做法可以归纳为以下几点:

    1、在项目的pom文件中定义多个profile,每个profile规定一个编译版本;

    2、在profile中指定插件maven-jar-plugin的classifier配置,这个是指定筛选器的名称,即最后要附加在jar包名称后面的,比如这里的jdk15;

    3、打包时指定不同的profile打包不同的版本,通过-P参数指定profile进行编译打包;

    4、在其他项目中使用该构件时通过配置classifier属性来筛选不同的版本;

          技术因需要而改变,自己备份下,也希望对大家有帮助。

     

    本文部分来源于铁木箱子的博客http://www.mzone.cc

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics