在maven中为不同的文件类型配置编码? [英] Configure encoding for different filetypes in maven?

查看:476
本文介绍了在maven中为不同的文件类型配置编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 maven-resource-plugin 过滤我的maven项目中的一些资源。在我的父项目中,我有:

 < project.build.sourceEncoding> UTF-8< /project.build.sourceEncoding> 

在子项目中,我有一个 test.properties 文件,普通具有默认编码的java属性文件 = ISO-8859-1。该文件包含:

  aboutText = Version $ {project.version}©2012 blabla 

为确保此文件正确过滤,我已将maven-resource-plugin分为单独的执行,每个执行的编码:

 < plugin> 
< artifactId> maven-resources-plugin< / artifactId>
< configuration>
< nonFilteredFileExtensions>
< nonFilteredFileExtension> ico< / nonFilteredFileExtension>
< nonFilteredFileExtension> jar< / nonFilteredFileExtension>
< / nonFilteredFileExtensions>
< / configuration>
<执行>
< execution>
< id> filter-properties-files< / id>
< phase> generate-resources< / phase>
< goals>
< goal> copy-resources< / goal>
< / goals>
< configuration>
<! - java属性文件编码在ISO-8859-1中,所以当
过滤这些文件时,我们坚持使用该编码。 - >
< encoding> ISO-8859-1< / encoding>
< outputDirectory> $ {basedir} / after< / outputDirectory>
< resources>
< resource>
< filtering> true< / filtering>
< directory> $ {basedir} / before< / directory>
< includes>
< include> ** / *。properties< / include>
< / includes>
< / resource>
< / resources>
< / configuration>
< / execution>
< execution>
< id> filter-non-properties-files< / id>
< phase> generate-resources< / phase>
< goals>
< goal> copy-resources< / goal>
< / goals>
< configuration>
< encoding> $ {project.build.sourceEncoding}< / encoding>
< outputDirectory> $ {basedir} / after< / outputDirectory>
< resources>
< resource>
< filtering> true< / filtering>
< directory> $ {basedir} / before< / directory>
< includes>
< include> ** / *。product< / include>
< include> ** / *。inf< / include>
< / includes>
< / resource>
< / resources>
< / configuration>
< / execution>
< / executions>
< / plugin>

这似乎太过分了,我有一种感觉,我不是正确使用插件,或者这个问题应该以另一种方式处理。可能直接在属性文件中编辑特殊字符:

  aboutText = Version $ {project.version} \\\© 2012 blabla 

解决方案>

这个问题本身就是一个非常宝贵的答案,显然作者提供的复杂过程是为各种过滤文件类型配置不同编码的唯一方式。然而,给出的例子是作者非标准用例的具体例子,并且掩盖了一些重要的细节,没有实际使用的例子充满了陷阱:




  • 这不是很明显,但在作者的例子中,默认资源复制目标资源仍然启用并且运行在两个定义的目标之外!

  • 您会注意到,作者使用生命周期阶段 generate-resources 而不是默认的进程资源。这是绕过上面第一点的窍门;通过使两个复制资源目标发生在较早的生命周期阶段,资源将根据给定的规则进行复制,然后当$ code>资源目标来源于原始资源复制保持原样,显然是因为它的覆盖设置默认值 false 。但是最好完全禁用 default-resources 执行。

  • 作者提供了一个 outputDirectory 声明。自然认为作者仅提供这一点,因为需要一个自定义的输出目录;毕竟,资源目标提供了此设置的默认值。奇怪的是,对于 copy-resources 目标,这个设置实际上是必需的!然而,有一个标准的Maven变量 $ {project.build.outputDirectory} 可以用作值。



基于作者自己的示例,以下是使用ISO-8859-1过滤属性文件的剪切和粘贴方式,复制其他文件而不进行过滤,并防止默认资源复制发生;都使用标准的源目录和目标目录:

 < plugin> 
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-resources-plugin< / artifactId>
< version> 2.7< / version>
<执行>
< execution>
<! - 关闭默认资源复制 - >
< id> default-resources< / id>
< phase />
< / execution>
< execution>
<! - 过滤属性文件中的资源。 - >
< id> filter-properties-files< / id>
< phase> process-resources< / phase>
< goals>
< goal> copy-resources< / goal>
< / goals>
< configuration>
< encoding> ISO-8859-1< / encoding>
< outputDirectory> $ {project.build.outputDirectory}< / outputDirectory>
< resources>
< resource>
< directory> src / main / resources< / directory>
< filtering> true< / filtering>
< includes>
< include> ** / *。properties< / include>
< / includes>
< / resource>
< / resources>
< / configuration>
< / execution>
< execution>
<! - 不要在属性文件的文件(如二进制文件)中执行属性替换。 - >
< id> copy-other-resources< / id>
< phase> process-resources< / phase>
< goals>
< goal> copy-resources< / goal>
< / goals>
< configuration>
< outputDirectory> $ {project.build.outputDirectory}< / outputDirectory>
< resources>
< resource>
< directory> src / main / resources< / directory>
< filtering> false< / filtering>
< excludes>
< exclude> ** / *。properties< / exclude>
< / excludes>
< / resource>
< / resources>
< / configuration>
< / execution>
< / executions>
< / plugin>


I use the maven-resource-plugin to filter some resources in my maven project. In my parent project I have:

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

In a sub project I have a test.properties file which is a plain java properties file with default encoding=ISO-8859-1. This file contains:

aboutText=Version ${project.version} © 2012 blabla

To make sure this file filters correctly I have split the maven-resource-plugin into separate executions each with its encoding:

  <plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <configuration>
      <nonFilteredFileExtensions>
        <nonFilteredFileExtension>ico</nonFilteredFileExtension>
        <nonFilteredFileExtension>jar</nonFilteredFileExtension>
      </nonFilteredFileExtensions>
    </configuration>
    <executions>
      <execution>
        <id>filter-properties-files</id>
        <phase>generate-resources</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <!-- java properties files are encoded in ISO-8859-1 so when 
            filtering those files we stick with that encoding. -->
          <encoding>ISO-8859-1</encoding>
          <outputDirectory>${basedir}/after</outputDirectory>
          <resources>
            <resource>
              <filtering>true</filtering>
              <directory>${basedir}/before</directory>
              <includes>
                <include>**/*.properties</include>
              </includes>
            </resource>
          </resources>
        </configuration>
      </execution>
      <execution>
        <id>filter-non-properties-files</id>
        <phase>generate-resources</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <encoding>${project.build.sourceEncoding}</encoding>
          <outputDirectory>${basedir}/after</outputDirectory>
          <resources>
            <resource>
              <filtering>true</filtering>
              <directory>${basedir}/before</directory>
              <includes>
                <include>**/*.product</include>
                <include>**/*.inf</include>
              </includes>
            </resource>
          </resources>
        </configuration>
      </execution>
    </executions>
  </plugin>

This seems overkill and I have a feeling that I am either not using the plugin correctly or that this problem should be handled in another way. Maybe stick to encoding special characters in properties files directly:

aboutText=Version ${project.version} \u00a9 2012 blabla

?

解决方案

The question is an invaluable answer in itself, as apparently the complicated procedure the author provides is the only way to configure different encodings for various filtered file types. The example given, however, is specific to the author's non-standard use-case, and glosses over a few important details, without which actual use of the example is fraught with gotchas:

  • It isn't obvious, but in the author's example the default resource copying goal resources is still enabled and runs in addition to the two defined goals!
  • You'll notice that the author used the lifecycle phase generate-resources instead of the default process-resources. This is a trick to get around the first point above; by making the two copy-resources goals occur in an earlier lifecycle phase, the resources are copied according to the given rules, and then when the default-resources goal comes along the original resource copying is left intact, apparently because its overwrite setting defaults false. But it would be better to disable altogether the default-resources execution.
  • The author provides an outputDirectory declaration. It would be natural to think that the author only provided this because a custom output directory was desired; after all, the resources goal provides a default value for this setting. Strangely, though, for the copy-resources goal this setting is actually required! There is a standard Maven variable ${project.build.outputDirectory} which can be used as the value, however.

Building on the author's own example in the question, here is a cut-and-paste way to filter properties files using ISO-8859-1, copy other files without filtering, and prevent the default resource copying from occurring; all using the standard source and target directories:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <version>2.7</version>
  <executions>
    <execution>
      <!-- Turn off default resource copying -->
      <id>default-resources</id>
      <phase />
    </execution>
    <execution>
      <!-- Filter resources in properties files. -->
      <id>filter-properties-files</id>
      <phase>process-resources</phase>
      <goals>
        <goal>copy-resources</goal>
      </goals>
      <configuration>
        <encoding>ISO-8859-1</encoding>
        <outputDirectory>${project.build.outputDirectory}</outputDirectory>
        <resources>
          <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
              <include>**/*.properties</include>
            </includes>
          </resource>
        </resources>
      </configuration>
    </execution>
    <execution>
      <!-- Do not do property substitution in files that are not properties files, such as binary files. -->
      <id>copy-other-resources</id>
      <phase>process-resources</phase>
      <goals>
        <goal>copy-resources</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.build.outputDirectory}</outputDirectory>
        <resources>
          <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <excludes>
              <exclude>**/*.properties</exclude>
            </excludes>
          </resource>
        </resources>
      </configuration>
    </execution>
  </executions>
</plugin>

这篇关于在maven中为不同的文件类型配置编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆