因为编程环境一直是linux,我一直采用的是UTF-8万能编码。
最近svn co了一个GB18030的项目。mvn war:war打包部署发现了很多配置文件中文乱码,导致启动时解析出错。
我一直以为是maven-resource-plugin的问题,
/** * <b>If wrappers is null or empty, the file will be copy only if to.lastModified() < from.lastModified()</b> * @param from the file to copy * @param to the destination file * @param encoding the file output encoding (only if wrappers is not empty) * @param wrappers array of {@link FilterWrapper} * @throws IOException if an IO error occurs during copying or filtering */ public static void copyFile( File from, File to, String encoding, FilterWrapper[] wrappers ) throws IOException { copyFile( from, to, encoding, wrappers, false ); }
反复查看了这个plugin的源代码之后发现它确实是采用pom里的encoding过滤文件。但是出war包里就是乱码,没天理啊。
很久之后我才想到可能是maven-war-plugin的问题。google一搜在官网发现了一个todo
org.apache.maven.plugin.war.packaging.AbstractWarPackagingTask Line
add encoding support (null mean platform encoding) |
看了下源代码
if ( context.getWebappStructure().registerFile( sourceId, targetFilename ) ) { final File targetFile = new File( context.getWebappDirectory(), targetFilename ); try { // fix for MWAR-36, ensures that the parent dir are created first targetFile.getParentFile().mkdirs(); // TODO: add encoding support (null mean platform encoding) context.getMavenFileFilter().copyFile( file, targetFile, true, context.getFilterWrappers(), null ); } catch ( MavenFilteringException e ) { throw new MojoExecutionException( e.getMessage(), e ); } // Add the file to the protected list context.getLog().debug( " + " + targetFilename + " has been copied (filtered)." ); return true; } else { context.getLog().debug( " - " + targetFilename + " wasn't copied because it has already been packaged (filtered)." ); return false; } }
context.getMavenFileFilter().copyFile( file, targetFile, true, context.getFilterWrappers(), null );
encoding是null啊,默认用环境的encoding,坑爹啊