AppFuse 3的乱码问题
AppFuse 3的乱码问题
猪刚烈 发表于3年前
AppFuse 3的乱码问题
  • 发表于 3年前
  • 阅读 15
  • 收藏 0
  • 点赞 0
  • 评论 0

华为云·免费上云实践>>>   

书接上回:AppFuse 3常见问题与解决方法 ,一个新问题:乱码! 在3.0.0版本下,执行mvn appfuse:full-source命令后使用mvn jetty:run启动工程,页面上就出现了乱码。我要说的是这个乱码问题非常有趣,它的有趣之处在于最后的解决方法看上去与问题差的太远。这个解决方法是:找到pom中对hibernate3-maven-plugin的配置,将其中的<executions/>部分注释掉,即不在process-test-resources阶段执行hbm2ddl。(如果你需要导出schema到数据库,可以手动执行mvn hibernate3:hbm2ddl,这也更符合实际开发的需要)。是的,看上去没有任何关系,但是问题就是出在了这里!下面我们回退到问题发生时一步步地进行排查。对于本文列出之外的一些细节问题,可以参考:基于Appfuse 2.1快速构建工程的注意事项 本文原文出处: http://blog.csdn.net/bluishglc/article/details/38640715 严禁任何形式的转载,否则将委托CSDN官方维护权益!

首先,查看编译之后的输出目录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的兼容问题,这里我们不再深究,但是解决方法已经变得很清晰上,就是前文提到的做法。

共有 人打赏支持
粉丝 22
博文 708
码字总数 110
作品 1
×
猪刚烈
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: