maven-compiler-plugin 指定jdk的版本和编码

Posted bronk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了maven-compiler-plugin 指定jdk的版本和编码相关的知识,希望对你有一定的参考价值。

为了让maven的jdk编译版本一致, 使用maven-compiler-plugin插件来协助管理

建议新建maven项目后的第一步就是配置该插件

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

 

使用encoding参数课解决java文件的编码问题

<plugins>  
          <plugin>  
              <groupId>org.apache.maven.plugins</groupId>  
              <artifactId>maven-compiler-plugin</artifactId>  
              <executions>  
                  <execution>  
                      <id>default-compile</id>  
                      <phase>compile</phase>  
                      <goals>  
                          <goal>compile</goal>  
                      </goals>  
                  </execution>  
              </executions>  
              <configuration>  
                  <encoding>UTF-8</encoding>  
              </configuration>  
          </plugin>  
</plugins>  

 

合起来就是

<plugins> 
         <plugin> 
            <groupId>org.apache.maven.plugins</groupId> 
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version> 
            <configuration> 
                <source>1.7</source> 
                <target>1.7</target> 
                <encoding>UTF8</encoding> 
            </configuration> 
         </plugin> 
    </plu

 

默认jdk版本不匹配出现的异常信息为

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project springJMS: Compilation failure: Compilation failure:  
[ERROR] /home/frank/programcode/SpringJMSSample/src/main/java/huangbowen/net/jms/MessageSender.java:[6,1] error: annotations are not supported in -source 1.3  
[ERROR]   
[ERROR] (use -source 5 or higher to enable annotations)  
[ERROR] /home/frank/programcode/SpringJMSSample/src/main/java/net/EmbedBrokerApp.java:[5,7] error: static import declarations are not supported in -source 1.3  
[ERROR] -> [Help 1]  
[ERROR]   
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.  
[ERROR] Re-run Maven using the -X switch to enable full debug logging.  
[ERROR]   
[ERROR] For more information about the errors and possible solutions, please read the following articles:  
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException  

 

以上是关于maven-compiler-plugin 指定jdk的版本和编码的主要内容,如果未能解决你的问题,请参考以下文章

maven-compiler-plugin 插件详解

maven-compiler-plugin和maven-jar-plugin报错飘红

解决:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile

maven报 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.0:compile(defalut-com

如何禁用 maven-compiler-plugin?

父pom中的maven-compiler-plugin