目录
  • maven 多profile及指定编译
  • maven 指定编译版本

maven 多profile及指定编译

要点

项目a依赖项目b,项目a、b都有对应的多个profile,通过mvn –p参数指定profile,只对a生效,对b不生效

项目a、b模块位于同一父项目,父项目构建时指定profile,可以传给a,b项目,a、b都使用同一指定的profile。

也可在父项目中定义属性,激活子项目profile,意即父项目 profile属性可传给各个子项目。

项目中定义的profile, 若<activeprofiledefault>设置为false,则不指定profile的情况下,该profil不会被执行。

实例

项目a 定义2个profile(aprofile、bprofile), 项目b定义2个对应的profile(aprofile、bprofile),则可将项目a、b中的aprofile激活方式设置为:

<activeprofiledefault>true</activeprofiledefault>

bprofile profile激活方式设置为:

<activation> 
 <property>                     
  <name>bprofile</name>                
 </property>            
</activation>

编译项目a时使用参数可编译bprofile版本:

mvn clean install -dbprofile

编译项目a时不带参数可编译aprofile版本:

mvn clean install

maven 指定编译版本

javac

先从javac的编译选项-source,-target说起:

  • source:指定使用什么版本的jdk语法编译源代码。java语法总是向后兼容的,为何需要设置呢?不晓滴
  • target:指定生成特定于某个jdk版本的class文件。高版本的class文件不被低版本支持,因此需要该项。注意,最好设置-bootclasspath指定对应jdk版本的boot classes文件,否则即使设置了-target也不能在指定版本上运行class文件

一般情况下,-target与-source设置一致,可以不用设置-target,但最好设置它。

maven

maven中可以指定jdk编译版本,还需要确定一下ide中jdk的使用版本。

在最新的maven中,默认编译版本为1.6,所以需要自己设置为指定版本。

设置有两种方式:

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<plugins>
    <plugin>    
        <artifactid>maven-compiler-plugin</artifactid>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
</plugins>

两种一致,都是使用maven-compiler-plugin实现的,插件会在编译时添加source,target选项。通过插件可以配置更多的选项。

在java 9后,新增了选项release,同时指定编译和输出时的jdk版本。也能配置插件,但这里仅给出方便的方式:

<properties>
    <maven.compiler.release>9</maven.compiler.release>
</properties>

在spring boot中,有独属于它自己的配置方式,它也是通过插件实现的(spring boot项目默认添加了):

<properties>
     <java.version>1.8</java.version>
</properties>   

以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。