introduction
When building Java projects with Maven, proper file encoding settings are essential to ensure that the project can be compiled and run correctly. Especially when dealing with source code or resource files containing non-ASCII characters (such as Chinese, Japanese, etc.), if the encoding is set correctly, it may cause garbled code problems, affecting the function and user experience of the program.
This article will explain in detail how to set the encoding of compiled files in Maven to ensure that the project can correctly handle text content in various languages.
1. Why do you need to set file encoding?
The default file encoding may be different in different operating systems and development environments. For example, Windows systems use GBK encoding by default, while Linux and Mac systems usually use UTF-8 encoding. If the file encoding in the project is inconsistent with the encoding used when Maven compiles, it may lead to abnormal character display, such as Chinese garbled code.
2. Set the encoding in
Maven provides a variety of ways to set file encoding, most commonly by modifying the configuration in the file. Here are some commonly used configuration methods:
2.1 Set the encoding of the compiler plug-in
If you are using Maven Compiler Plugin to compile Java source code, you can add or modify the configuration of the compiler plug-in in the <plugins> section under the <build> tag to specify the encoding of the source code and the target code:
<build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>
2.2 Set the encoding of resource files
For resource files in the project (such as configuration files, pictures, etc.), they can be set by configuring the Maven Resources Plugin. This is also in<build>
Under the tag<resources>
Partially set:
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <encoding>UTF-8</encoding> </resource> </resources> <testResources> <testResource> <directory>src/test/resources</directory> <filtering>true</filtering> <encoding>UTF-8</encoding> </testResource> </testResources> </build>
2.3 Set the code of the entire project
In addition to setting encoding for specific plugins, you can also<properties>
The default encoding for the entire project is set in the tag, so that all plugins that support this property will use this encoding:
<properties> <>UTF-8</> <>UTF-8</> </properties>
3. Verify encoding settings
After completing the above configuration, you can verify whether the encoding settings are effective through the following steps:
- Check compiled output: When compiling a project, Maven will output the compilation information on the console, including the encoding used. Make sure the encoding shown in the output is what you expect.
- View the generated file: The compiled class file or resource file should be able to correctly display non-ASCII characters. These files can be checked through a text editor.
- Run the test: If there are unit tests in the project, running the test can further confirm whether the encoding settings are correct. Especially those test cases involving string processing.
Properly setting up file encoding in Maven projects is an important step in ensuring the smooth development and deployment of the project. Through the methods described in this article, you can easily
configures the encoding of compiler and resource files to avoid problems caused by inconsistent encoding. Hope this information will be helpful to you!
When building Java projects with Maven, it is important to ensure that the file is encoded correctly, especially when dealing with files containing non-ASCII characters. Maven provides a variety of ways to set file encoding, including
Configure plugins and properties in the file.
The following are some common application scenarios and their corresponding configuration examples:
1. Set the global project encoding
You can
of<properties>
Partially sets the global file encoding. This will affect all plugins that depend on this property.
<project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0."> <modelVersion>4.0.0</modelVersion> <groupId></groupId> <artifactId>my-project</artifactId> <version>1.0-SNAPSHOT</version> <properties> <>UTF-8</> <>UTF-8</> </properties> <!-- Other configurations --> </project>
2. Configure Maven Compiler Plugin
Maven Compiler Plugin is used to compile Java source code. You can explicitly set its encoding properties.
<build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>
3. Configure Maven Resources Plugin
Maven Resources Plugin is used to copy resource files (such as configuration files, static files, etc.). You can also set encoding properties for it.
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <encoding>UTF-8</encoding> </resource> </resources> <plugins> <plugin> <groupId></groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.2.0</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>
4. Configure Maven Surefire Plugin
Maven Surefire Plugin is used to run unit tests. You can also set encoding properties for it.
<build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <argLine>-=UTF-8</argLine> </configuration> </plugin> </plugins> </build>
5. Comprehensive examples
The following is a comprehensive
Example, including all the above configurations:
<project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0."> <modelVersion>4.0.0</modelVersion> <groupId></groupId> <artifactId>my-project</artifactId> <version>1.0-SNAPSHOT</version> <properties> <>UTF-8</> <>UTF-8</> <>1.8</> <>1.8</> </properties> <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <encoding>UTF-8</encoding> </resource> </resources> <plugins> <plugin> <groupId></groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId></groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.2.0</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId></groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <argLine>-=UTF-8</argLine> </configuration> </plugin> </plugins> </build> </project>
With the above configuration, you can ensure that the file encoding used in different stages and plugins are UTF-8, thus avoiding character encoding issues. When using Apache Maven for Java project construction, it is very important to set the encoding of the project correctly, especially when the project contains non-ASCII characters (such as Chinese, Japanese, etc.). If the encoding is not set properly, it may lead to compilation errors or garbled problems. Maven provides a variety of ways to set up the encoding of a project, mainly through the configuration in the file.
1. Set the encoding of the Maven compiler plugin in
Maven compiler plugin is used to compile Java source code. You can specify the encoding of the source code and resource files by setting the <encoding> tag. For example:
<build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>
In this example, <source> and <target> specify the Java version, while <encoding> specifies the character encoding used at compile time.
2. Set the encoding of the resource file
In addition to the encoding of the source code, you also need to set the encoding of resource files (such as attribute files, XML configuration files, etc.) in the project. This can be achieved through maven-resources-plugin:
<build> <resources> <resource> <directory>src/main/resources</directory> <encoding>UTF-8</encoding> </resource> </resources> <plugins> <plugin> <groupId></groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.2.0</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>
Here, the <encoding> under the <resources> tag specifies the default encoding of the resource file, and the <configuration> part of the maven-resources-plugin further ensures that the resource file uses the specified encoding when processing.
3. Set encoding globally using properties tags
You can also set the encoding globally in the <properties> tag, so that all relevant plugins will use this encoding setting:
<project> ... <properties> <>UTF-8</> <>UTF-8</> </properties> ... </project>
<>sets the encoding of the source code and resource files, while<>sets the encoding of the report output.
Summarize
Through the above method, you can effectively control the encoding settings in the Maven project to avoid problems caused by inconsistent encoding. It is recommended to clarify coding standards at the beginning of the project and be consistent throughout the project to reduce the complexity of later maintenance.
The above is the detailed content of the encoding setting method of Maven compiled files. For more information about the encoding setting of Maven compiled files, please pay attention to my other related articles!