相关环境:
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