Java 为什么 Maven 会警告我编码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24144073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does Maven warn me about encoding?
提问by Marco Ferrari
My goal is to create an archetype from a project.
我的目标是从一个项目中创建一个原型。
When I run a goal that does not involve the maven-archetype-plugin, I can't see any warning:
当我运行一个不涉及 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]
On the other end, when I run archetype:create-from-project, I get a couple:
另一方面,当我运行 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
I know that the "standard" maven way is to use the project.build.sourceEncodingproperty. I tried adding some more properties to the pom in order to address this issue but none of them worked.
我知道“标准”maven 方法是使用该project.build.sourceEncoding属性。我尝试向 pom 添加更多属性以解决此问题,但它们都不起作用。
Any ideas? Thanks.
有任何想法吗?谢谢。
I have the following pom.xml:
我有以下 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>
采纳答案by izstas
When you run the goal archetype:create-from-project, Maven generates a POM file for building the archetype at target/generated-sources/archetype/pom.xmland then runs the packagegoal (by default) on this POM.
当你运行目标时archetype:create-from-project,Maven 会生成一个 POM 文件来构建原型 at target/generated-sources/archetype/pom.xml,然后package在这个 POM 上运行目标(默认情况下)。
The generated POM file doesn't have project.build.sourceEncodingor any other property defining encoding, and that's why you get the warning.
生成的 POM 文件没有project.build.sourceEncoding或任何其他定义编码的属性,这就是您收到警告的原因。
The POM is generated from this prototypeby org.apache.maven.archetype.creator.FilesetArchetypeCreator#createArchetypeProjectPom, and from that code there doesn't seem to be a way to add properties to the resulting POM file.
聚甲醛是从产生这个原型的org.apache.maven.archetype.creator.FilesetArchetypeCreator#createArchetypeProjectPom,并从该代码似乎没有被添加属性所得到的POM文件的方式。
回答by khmarbaise
You haven't set the encoding defaultproperty like this:
您还没有像这样设置编码默认属性:
<project>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
...
</project>
This approach is better than defining encoding manually for every plugin, cause all plugins having default values for encoding for example the maven-resources-plugin:
这种方法比为每个插件手动定义编码更好,因为所有插件都具有编码的默认值,例如 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}
So this means you only need to define this property and the plugin will automatically use this encoding.
所以这意味着你只需要定义这个属性,插件就会自动使用这个编码。
回答by Kalpesh Soni
I was annoyed to see that maven kept on complaining after the above entry
看到maven在上面输入后一直抱怨我很生气
Then I realized that its the failsafe plugin and it needs its own property
然后我意识到它是故障安全插件,它需要自己的属性
So here it goes
所以就到这里了
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

