Maven 多profile及指定编译问题的解决

作者:hongweigg 时间:2022-04-22 23:43:24 

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>

来源:https://blog.csdn.net/hongweigg/article/details/106576883

标签:Maven,profile,编译
0
投稿

猜你喜欢

  • SpringBoot详细讲解静态资源导入的实现

    2023-07-26 13:23:21
  • Android如何让WebView中的HTML5页面实现视频全屏播放

    2023-07-29 00:32:06
  • 基于Hadoop实现Knn算法

    2023-11-27 04:01:20
  • SpringBoot2 参数管理实践之入参出参与校验的方式

    2022-12-25 02:44:30
  • 关于C#中的Invoke示例详解

    2023-06-20 10:17:14
  • 高并发下如何避免重复数据产生技巧

    2022-04-17 07:08:57
  • Android实现的数字格式化用法示例

    2023-09-30 09:03:28
  • 详解@ConfigurationProperties实现原理与实战

    2023-11-24 05:19:26
  • Spring boot如何快速的配置多个Redis数据源

    2023-05-12 18:27:32
  • java操作json对象出现StackOverflow错误的问题及解决

    2023-03-04 20:06:14
  • IDEA 2019.1.3 激活码大全

    2023-05-09 05:20:37
  • java实现五子棋小游戏

    2021-12-25 06:58:56
  • Swing常用组件之多行文本区JTextArea

    2023-11-08 14:16:49
  • java代码执行字符串中的逻辑运算方法

    2023-11-29 12:13:06
  • java compare compareTo方法区别详解

    2022-06-26 08:13:55
  • java.sql.Date和java.util.Date的区别详解

    2023-11-28 16:15:09
  • SpringBoot通过@Value实现给静态变量注入值详解

    2022-04-30 14:30:37
  • Java经验点滴:类注释文档编写方法

    2023-11-06 03:50:11
  • spring boot 动态生成接口实现类的场景分析

    2022-12-14 05:09:33
  • 基于FeignException$InternalServerError的解决方案

    2023-04-25 15:50:45
  • asp之家 软件编程 m.aspxhome.com