更改maven打包JDK版本时遇到的问题汇总

本文介绍如何将Maven项目的Java编译版本从1.8降至1.6的方法,包括修改maven配置文件和Eclipse设置的具体步骤,并解决了过程中遇到的错误。

前言

在放假前,被安排对比SVN上与生产环境上的代码是否相同。为了对比的一致性,我在用maven打包SVN代码时需要将Java编译版本从8降低到6。直观一点的描述就是在生成的jar包中,将META-INF下MANIFEST.MF中的Build-Jdk修改成1.6版本,如下图所示:

  • JDK 1.8
    JDK1.8

  • JDK 1.6
    JDK1.6

一个看似很简单的问题我还是捣鼓了半天,现将其中修改方法和遇到的问题汇总如下:

通过配置文件来修改JDK版本

在网上搜索解决方法时,普遍是使用这类方法,主要有两种:

  • 往maven目录下conf/settings.xml文件添加如下代码,这种方法适用全局。
<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>  
  • 针对项目修改其中的pom.xml文件,然后用maven命令行执行(mvn clean, mvn package),即可获得指定jdk版本编译的jar包,这种方式只适用于单一项目。
  <build>  
    <plugins>  
      <plugin>  
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>  
        <version>2.0.2</version>  
        <configuration>  
          <source>1.6</source>  
          <target>1.6</target>  
          <encoding>UTF-8</encoding>  
        </configuration>  
      </plugin>  
    </plugins>  
  </build> 

然而通过修改配置这类方法并没有达到我的目的,生成的jar还是显示由JDK1.8生成不知为何…

通过Eclipse设置来修改

接着想到能不能通过修改Eclipse的编译环境来达到这个目的,即找到Window -> Preferences -> Java -> Compiler设置,将Compiler compliance level修改成1.6。再找到Window -> Preferences -> Java -> Installed JREs设置,将下载JDK1.6添加到Installed JREs中并勾选。
设置完成后,使用maven对项目进行打包,出现java.lang.UnsupportedClassVersionError错误。由于我使用的maven版本是3.3.3,而在官网上发现此版本最低需要JDK1.7,所以版本不一致时会报错。

这里写图片描述

从图中可以看出,支持Java 6的最新版本是3.2.5,所以我将Eclipse中maven版本修改为3.2.5后再次使用maven对项目进行编译,这个时候又出现如下错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project springside-core: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

再次去查资料,发现应该将Eclipse中JRE的路径修改成JDK的安装路径,而不是单独的JRE路径。通过修改路径之后,maven终于可以使用Java 6对项目进行编译和打包了。

### Maven 打包配置详解 Maven 是一种强大的构建工具,用于管理 Java 项目的依赖关系和生命周期。通过 `pom.xml` 文件可以定义项目的打包方式以及相关配置。 #### 1. 定义打包类型 在 `pom.xml` 中,可以通过 `<packaging>` 标签指定项目的打包类型。常见的打包类型有 `jar`, `war`, 和 `pom` 等[^1]。 ```xml <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.example</groupId> <artifactId>example-project</artifactId> <version>1.0-SNAPSHOT</version> <!-- 定义打包类型,默认为 jar --> <packaging>jar</packaging> </project> ``` 如果需要将 Web 应用程序打成 WAR 包,则应将 `<packaging>` 的值更改为 `war`。 --- #### 2. 配置插件实现自定义打包行为 为了支持复杂的打包需求,通常会在 `pom.xml` 中引入特定的 Maven 插件来扩展功能。例如: ##### 使用 Maven Compiler Plugin 编译源码 该插件允许开发者指定编译器版本和其他参数。 ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>17</source> <target>17</target> </configuration> </plugin> </plugins> </build> ``` 上述代码片段设置了 JDK 版本为 17 进行编译操作。 --- ##### 使用 Maven Assembly Plugin 创建可执行 JAR 或 ZIP 文件 此插件能够帮助创建包含所有依赖项的独立运行文件。 ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.example.MainApp</mainClass> </manifest> </archive> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 这段 XML 配置会生成一个带有所有依赖项的 JAR 文件,并指定了入口类作为启动点。 --- #### 3. 自动化资源处理 对于某些项目可能涉及静态资源(如 HTML, CSS, JavaScript),可通过如下方式进行自动化复制到目标目录中。 ```xml <resources> <resource> <directory>src/main/resources</directory> <includes> <include>*.properties</include> <include>*.xml</include> </includes> </resource> </resources> ``` 以上示例展示了如何仅包含 `.properties` 和 `.xml` 类型的文件至最终产物之中。 --- #### 总结 通过对 `pom.xml` 文件合理编辑,不仅可以控制基本的打包形式还能借助各类插件完成更加精细的任务定制工作。这使得整个开发流程变得更加高效便捷的同也增强了跨平台兼容能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值