maven 编译报错MojoFailureException

本文介绍了解决Maven项目在使用高版本JDK时出现的编译错误问题,特别是针对com.sun.image.codec.jpeg包不存在的情况。通过替换JPEG编码方式为ImageIO.write方法,成功解决了编译错误。

相关环境:

1、maven:apache-maven-3.0.5

2、jdk:1.7

3、eclipse

背景:在eclipse整合maven的前提下,使用eclipse将maven项目编译发布到私服,执行clean install deploy之后,install失败,报如【图 1】的错


图1

图1显示的是乱码,其实提示的是:程序包com.sun.image.codec.jpeg不存在;具体的原因是我使用了高版本的jdk,在高版本的jdk中com.sun.image.codec.jpeg有可能被删除掉,最终会导致编译的时候报错。

               BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);
 
//Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
Image image = img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
tag.getGraphics().drawImage(image, 0, 0, null);
String outputDir = destPath.substring(0, destPath.lastIndexOf("\\"));
File f=new File(outputDir );
if (!f.exists()){
f.mkdirs(); 
}
try {
FileOutputStream out = new FileOutputStream(destPath);
com.sun.image.codec.jpeg.JPEGImageEncoder encoder = com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);

out.close();
} catch (Exception e) {
e.printStackTrace();
//logger.error(e.getMessage(), e);
}

将上面红色代码替换成:

  Image image = img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
tag.getGraphics().drawImage(image, 0, 0, null);
String outputDir = destPath.substring(0, destPath.lastIndexOf("\\"));
File f=new File(outputDir );
if (!f.exists()){
f.mkdirs(); 
}
try {
FileOutputStream out = new FileOutputStream(destPath);
ImageIO.write(tag, "jpg", out);
out.flush();

out.close();
} catch (Exception e) {
e.printStackTrace();
//logger.error(e.getMessage(), e);
}

然后重新编译,如图2:


图2

到此maven项目可用正常编译。注:我使用的maven-compiler-plugin版本是2.3.2



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值