首先,查看编译之后的输出目录target/classes下的中文properties文件,可以确定这件文件本身就没有转码,是native2ascii插件没有正常工作吗?通过检查,发现问题不在native2ascii-maven-plugin的配置上。使用mvn clean package -X查看maven执行过程中的log信息,发现了下面一段有趣的输出:
[DEBUG] ----------------------------------------------------------------------- [DEBUG] Goal: org.apache.maven.plugins:maven-resources-plugin:2.6:resources (default-resources) [DEBUG] Style: Regular [DEBUG] Configuration: <?xml version="1.0" encoding="UTF-8"?> <configuration> <buildFilters default-value="${project.build.filters}"/> <encoding default-value="${project.build.sourceEncoding}">${encoding}</encoding> <escapeString>${maven.resources.escapeString}</escapeString> <escapeWindowsPaths default-value="true">${maven.resources.escapeWindowsPaths}</escapeWindowsPaths> <includeEmptyDirs default-value="false">${maven.resources.includeEmptyDirs}</includeEmptyDirs> <outputDirectory default-value="${project.build.outputDirectory}"/> <overwrite default-value="false">${maven.resources.overwrite}</overwrite> <project default-value="${project}"/> <resources default-value="${project.resources}"/> <session default-value="${session}"/> <supportMultiLineFiltering default-value="false">${maven.resources.supportMultiLineFiltering}</supportMultiLineFiltering> <useBuildFilters default-value="true"/> <useDefaultDelimiters default-value="true"/> </configuration> [DEBUG] ----------------------------------------------------------------------- [DEBUG] Goal: org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) [DEBUG] Style: Regular [DEBUG] Configuration: <?xml version="1.0" encoding="UTF-8"?> <configuration> <basedir default-value="${basedir}"/> <buildDirectory default-value="${project.build.directory}"/> <classpathElements default-value="${project.compileClasspathElements}"/> <compileSourceRoots default-value="${project.compileSourceRoots}"/> <compilerId default-value="javac">${maven.compiler.compilerId}</compilerId> <compilerReuseStrategy default-value="${reuseCreated}">${maven.compiler.compilerReuseStrategy}</compilerReuseStrategy> <compilerVersion>${maven.compiler.compilerVersion}</compilerVersion> <debug default-value="true">${maven.compiler.debug}</debug> <debuglevel>${maven.compiler.debuglevel}</debuglevel> <encoding default-value="${project.build.sourceEncoding}">${encoding}</encoding> <executable>${maven.compiler.executable}</executable> <failOnError default-value="true">${maven.compiler.failOnError}</failOnError> <forceJavacCompilerUse default-value="false">${maven.compiler.forceJavacCompilerUse}</forceJavacCompilerUse> <fork default-value="false">${maven.compiler.fork}</fork> <generatedSourcesDirectory default-value="${project.build.directory}/generated-sources/annotations"/> <maxmem>${maven.compiler.maxmem}</maxmem> <meminitial>${maven.compiler.meminitial}</meminitial> <mojoExecution>${mojoExecution}</mojoExecution> <optimize default-value="false">${maven.compiler.optimize}</optimize> <outputDirectory default-value="${project.build.outputDirectory}"/> <projectArtifact default-value="${project.artifact}"/> <showDeprecation default-value="false">${maven.compiler.showDeprecation}</showDeprecation> <showWarnings default-value="false">${maven.compiler.showWarnings}</showWarnings> <skipMain>${maven.main.skip}</skipMain> <skipMultiThreadWarning default-value="false">${maven.compiler.skipMultiThreadWarning}</skipMultiThreadWarning> <source default-value="1.5">1.7</source> <staleMillis default-value="0">${lastModGranularityMs}</staleMillis> <target default-value="1.5">1.7</target> <useIncrementalCompilation default-value="true">${maven.compiler.useIncrementalCompilation}</useIncrementalCompilation> <verbose default-value="false">${maven.compiler.verbose}</verbose> <mavenSession default-value="${session}"/> <session default-value="${session}"/> </configuration> [DEBUG] ----------------------------------------------------------------------- [DEBUG] Goal: org.codehaus.mojo:native2ascii-maven-plugin:1.0-beta-1:native2ascii (native2ascii-utf8) [DEBUG] Style: Regular [DEBUG] Configuration: <?xml version="1.0" encoding="UTF-8"?> <configuration> <encoding default-value="${project.build.sourceEncoding}">UTF8</encoding> <includes> <include>ApplicationResources_zh*.properties</include> <include>displaytag_zh*.properties</include> </includes> <tempDir default-value="${project.build.directory}"/> <workDir default-value="${project.build.outputDirectory}"/> </configuration> [DEBUG] ----------------------------------------------------------------------- [DEBUG] Goal: org.apache.maven.plugins:maven-resources-plugin:2.6:testResources (default-testResources) [DEBUG] Style: Regular [DEBUG] Configuration: <?xml version="1.0" encoding="UTF-8"?> <configuration> <buildFilters default-value="${project.build.filters}"/> <encoding default-value="${project.build.sourceEncoding}">${encoding}</encoding> <escapeString>${maven.resources.escapeString}</escapeString> <escapeWindowsPaths default-value="true">${maven.resources.escapeWindowsPaths}</escapeWindowsPaths> <includeEmptyDirs default-value="false">${maven.resources.includeEmptyDirs}</includeEmptyDirs> <outputDirectory default-value="${project.build.testOutputDirectory}"/> <overwrite default-value="false">${maven.resources.overwrite}</overwrite> <project default-value="${project}"/> <resources default-value="${project.testResources}"/> <session default-value="${session}"/> <skip>${maven.test.skip}</skip> <supportMultiLineFiltering default-value="false">${maven.resources.supportMultiLineFiltering}</supportMultiLineFiltering> <useBuildFilters default-value="true"/> <useDefaultDelimiters default-value="true"/> </configuration> [DEBUG] --- init fork of com.github:demo:1.0-SNAPSHOT for org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2ddl (default) --- [DEBUG] Dependencies (collect): [] [DEBUG] Dependencies (resolve): [] [DEBUG] ----------------------------------------------------------------------- [DEBUG] Goal: org.apache.maven.plugins:maven-resources-plugin:2.6:resources (default-resources) [DEBUG] Style: Regular [DEBUG] Configuration: <?xml version="1.0" encoding="UTF-8"?> <configuration> <buildFilters default-value="${project.build.filters}"/> <encoding default-value="${project.build.sourceEncoding}">${encoding}</encoding> <escapeString>${maven.resources.escapeString}</escapeString> <escapeWindowsPaths default-value="true">${maven.resources.escapeWindowsPaths}</escapeWindowsPaths> <includeEmptyDirs default-value="false">${maven.resources.includeEmptyDirs}</includeEmptyDirs> <outputDirectory default-value="${project.build.outputDirectory}"/> <overwrite default-value="false">${maven.resources.overwrite}</overwrite> <project default-value="${project}"/> <resources default-value="${project.resources}"/> <session default-value="${session}"/> <supportMultiLineFiltering default-value="false">${maven.resources.supportMultiLineFiltering}</supportMultiLineFiltering> <useBuildFilters default-value="true"/> <useDefaultDelimiters default-value="true"/> </configuration> [DEBUG] --- exit fork of com.github:demo:1.0-SNAPSHOT for org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2ddl (default) ---
这段log的有趣之处在于:resources:resources 被执行了两次。第一次是在native2ascii:native2ascii之前,第二次是在 native2ascii:native2ascii 执行之后执行hbm2ddl时又被重新调用了一次。为了准确地了解resources:resources和native2ascii:native2ascii的执行顺序,我们从它们各自的文档上找到它们默认绑定的周期说明:
resources:resources -> process-resources
native2ascii:native2ascii -> process-classes
maven的默认生命周期如下:
<phases> <phase>validate</phase> <phase>initialize</phase> <phase>generate-sources</phase> <phase>process-sources</phase> <phase>generate-resources</phase> <phase>process-resources</phase> <phase>compile</phase> <phase>process-classes</phase> <phase>generate-test-sources</phase> <phase>process-test-sources</phase> <phase>generate-test-resources</phase> <phase>process-test-resources</phase> <phase>test-compile</phase> <phase>process-test-classes</phase> <phase>test</phase> <phase>prepare-package</phase> <phase>package</phase> <phase>pre-integration-test</phase> <phase>integration-test</phase> <phase>post-integration-test</phase> <phase>verify</phase> <phase>install</phase> <phase>deploy</phase> </phases>
按照正常的逻辑, resources:resources会先于native2ascii:native2ascii将所有指定的resource文件进行filter处理之后放置于target目录下,之后native2ascii:native2ascii跟进,对指定的文件进行转码,这是非常合理的处理方式,而我们的问题就出现在了对resources:resources第二次不正常的调用,这导致了之前经过转码的文件又被未经转码的文件覆盖了!从日志上看,导致resources:resources第二次执行的原因应该是在进入process-test-resources阶段后执行hbm2ddl触发的,也就是说是hbm2ddl的某个动作主动调用了resources:resources,至于具体的细节,是bug还是与maven3的兼容问题,这里我们不再深究,但是解决方法已经变得很清晰上,就是前文提到的做法。