Maven为什么警告我有关编码的信息?


94

我的目标是从项目中创建原型。

当我运行不涉及maven-archetype-plugin的目标时,看不到任何警告:

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-archetype-base ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-archetype-base ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]

另一方面,当我运行archetype:create-from-project时,我得到了几个:

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-archetype-base-archetype ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 10 resources
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-archetype-base-archetype ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources

我知道“标准”行家方式是使用project.build.sourceEncoding属性。为了解决此问题,我尝试向pom添加更多属性,但是它们都不起作用。

有任何想法吗?谢谢。

我有以下pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.group.id</groupId>
<artifactId>my-artifact</artifactId>
<version>0.0.1</version>
<packaging>maven-archetype</packaging>

<properties>

    <!-- Compiler properties -->
    <maven.compiler.target>1.7</maven.compiler.target>
    <maven.compiler.source>1.7</maven.compiler.source>
    <encoding>UTF-8</encoding>
    <project.build.sourceEncoding>${encoding}</project.build.sourceEncoding>
    <project.reporting.outputEncoding>${encoding}</project.reporting.outputEncoding>
    <project.resources.sourceEncoding>${encoding}</project.resources.sourceEncoding>
    <archetype.encoding>${encoding}</archetype.encoding>

    <!-- Maven plugins version -->
    <maven-archetype-plugin-version>2.2</maven-archetype-plugin-version>
    <maven-resources-plugin-version>2.6</maven-resources-plugin-version>

    <!-- Maven extentions version -->
    <maven-archetype-packaging-extension-version>2.2</maven-archetype-packaging-extension-version>
</properties>
<dependencies>
[...]
</dependencies>

<build>
    <extensions>
        <extension>
            <groupId>org.apache.maven.archetype</groupId>
            <artifactId>archetype-packaging</artifactId>
            <version>${maven-archetype-packaging-extension-version}</version>
        </extension>
    </extensions>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>${maven-resources-plugin-version}</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-archetype-plugin</artifactId>
            <version>${maven-archetype-plugin-version}</version>
            <extensions>true</extensions>
        </plugin>

    </plugins>

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

Answers:


18

当您运行目标时archetype:create-from-project,Maven会生成一个POM文件来构建原型,target/generated-sources/archetype/pom.xml然后package在此POM上运行目标(默认情况下)。

生成的POM文件没有project.build.sourceEncoding定义编码的属性或任何其他属性,这就是为什么您会收到警告。

POM是通过原型该原型生成的org.apache.maven.archetype.creator.FilesetArchetypeCreator#createArchetypeProjectPom,并且从该代码中似乎没有一种方法可以将属性添加到生成的POM文件中。


140

您尚未像这样设置编码默认属性:

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

这种方法比为每个插件手动定义编码要好,因为所有插件都具有用于编码的默认值,例如maven-resources-plugin

encoding:

The character encoding scheme to be applied when filtering resources.
Type: java.lang.String
Required: No
User Property: encoding
Default: ${project.build.sourceEncoding}

因此,这意味着您只需要定义此属性,插件就会自动使用此编码。


1
我设置 看我的绒球。谢谢
Marco Ferrari

1
您可以删除插件配置中的条目<encoding>${encoding}</encoding>
khmarbaise 2014年

1
好。我添加了这些条目以查看是否解决了问题,但是没有运气
Marco Ferrari

我不明白为什么后备使用平台编码而不是UTF-8。旧版支持?
msa

@msa因为并非所有平台都将UTF-8作为默认值。最简单的示例Windows。
khmarbaise

33

我很生气,看到上面的条目之后,行家一直在抱怨

然后我意识到它是故障安全插件,它需要自己的属性

所以就这样

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.