软件编程
位置:首页>> 软件编程>> java编程>> Maven 多profile及指定编译问题的解决

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

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

标签:Maven,profile,编译

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

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com