mave打包编译java生成的jdk版本 / Maven项目在刷新/导入项目时jdk版本过低的解决方案

1 pom.xml 决定代码版本
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>


2 执行编译打包

clean -U  package -Dmaven.test.skip=true

3 检查class

命令:javap -verbose ClassName.class


minor version 50.0
这是1.6

minor version 51.0
这是1.7

==========================================

1.案例引入
   导入一个maven的web项目,发现jdk版本过低,当然这里可以右键build path来修改,但是当我们update-project-configration时,版本又变化回来了。


2.解决方案
2.1方案一 添加如下配置后,update project configuration

[html] view plain copy
解决方法是,在maven的pom.xml中添加 
    <build> 
    <pluginManagement> 
        <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> 
    </pluginManagement> 
    </build> 


2.2方案2

2.2.1注意:eclipse的maven依赖必须要是外部的才行





在安装目录的D:\apache-maven-3.0.5\conf下的settings.xml中插入:红色部分

[html] view plain copy
<profiles> 
    <!-- profile 
     | Specifies a set of introductions to the build process, to be activated using one or more of the 
     | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/> 
     | or the command line, profiles have to have an ID that is unique. 
     | 
     | An encouraged best practice for profile identification is to use a consistent naming convention 
     | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc. 
     | This will make it more intuitive to understand what the set of introduced profiles is attempting 
     | to accomplish, particularly when you only have a list of profile id's for debug. 
     | 
     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo. 
    <profile> 
      <id>jdk-1.7</id> 
 
      <activation> 
        <jdk>1.7</jdk> 
      </activation> 
 
      <repositories> 
        <repository> 
          <id>jdk17</id> 
          <name>Repository for JDK 1.7 builds</name> 
          <url>http://www.myhost.com/maven/jdk17</url> 
          <layout>default</layout> 
          <snapshotPolicy>always</snapshotPolicy> 
        </repository> 
      </repositories> 
    </profile> 
    --> 
    <span style="color:#FF0000;"><span style="color:#FF0000;"><profile>   
    <id>jdk17</id>   
    <activation>   
        <activeByDefault>true</activeByDefault>   
        <jdk>1.7</jdk>   
    </activation>   
    <properties>   
        <maven.compiler.source>1.7</maven.compiler.source>   
        <maven.compiler.target>1.7</maven.compiler.target>   
        <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>   
    </properties>    
</profile></span></span>  
 
    <!-- 
     | Here is another profile, activated by the system property 'target-env' with a value of 'dev', 
     | which provides a specific path to the Tomcat instance. To use this, your plugin configuration 
     | might hypothetically look like: 
     | 
     | ... 
     | <plugin> 
     |   <groupId>org.myco.myplugins</groupId> 
     |   <artifactId>myplugin</artifactId> 
     |    
     |   <configuration> 
     |     <tomcatLocation>${tomcatPath}</tomcatLocation> 
     |   </configuration> 
     | </plugin> 
     | ... 
     | 
     | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to 
     |       anything, you could just leave off the <value/> inside the activation-property. 
     | 
    <profile> 
      <id>env-dev</id> 
 
      <activation> 
        <property> 
          <name>target-env</name> 
          <value>dev</value> 
        </property> 
      </activation> 
 
      <properties> 
        <tomcatPath>/path/to/tomcat/instance</tomcatPath> 
      </properties> 
    </profile> 
    --> 
  </profiles> 


第二种配置是属于全局的配置,第一种是针对一个项目的配置,大家可以在开发中根据需要选择使用。

猜你喜欢

转载自angie.iteye.com/blog/2391807