Maven 常用配置

常用命令

  • 打包指定模块、跳过测试
    mvn clean package -pl web -am -Dmaven.test.skip=true
  • 打包并构建docker镜像,详情参考:https://blog.csdn.net/u014438244/article/details/94436773
    mvn clean package docker:build -Dmaven.test.skip=true
  • 强制清理
    mvn clean package -U

-pl --projects <arg> 构建指定的模块,模块间用逗号分隔;
-am --also-make 同时构建所列模块的依赖模块;
-Dmaven.test.skip=true 构建时跳过测试
-U 参数会强制update本地的jar(不用再专门去删除)

Resource资源配置说明

完整请参考官方:http://maven.apache.org/pom.html

<build>元素的另一个特性是指定项目中资源的位置。 资源通常不是指代码,资源是不需要编译的,但因为各种理由,这些资源要绑定到项目中,例如代码生成。

例如,一个 Plexus 项目需要一个configuration.xml(配置指定组件容器)文件放到META-INF/plexus目录下,尽管我们很容易的就把文件放到src/main/resources/META-INF/plexus目录下,默认情况放在resources目录下的资源会自动识别并捆绑,但我们想要的是给Plexus另创建一个目录src/main/plexus,把文件放入其中。为了使其在打包编译后能正确的捆绑,我们要作如下配置:

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                      https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <build>
    ...
    <resources>
      <resource>
        <targetPath>META-INF/plexus</targetPath>
        <filtering>false</filtering>
        <directory>${basedir}/src/main/plexus</directory>
        <includes>
          <include>configuration.xml</include>
        </includes>
        <excludes>
          <exclude>**/*.properties</exclude>
        </excludes>
      </resource>
    </resources>
    <testResources>
      ...
    </testResources>
    ...
  </build>
</project>

各项配置说明:
resources :配置资源列表, 每个资源元素描述与此项目关联的文件的内容和位置。
targetPath:设置打包后资源的放置路径,打包jar后,默认路径是 META-INF
filtering : 值为true或false,设置为true时,所包含的资源文件可以通过占位符注入值,注意的是*.properties这类资源文件是默认为true的,即会自动识别配置注入值,无需配置filtering。配置项在POM文件中获取,资源文件通过${xxx.xxxx}占位符进行获取,更新版本的spring使用@xxx.xxx@
directory :这个元素定义了在哪里可以找到资源,默认的目录是 ${basedir}/src/main/resources
includes :通过通配符*匹配要包含哪些资源文件
excludes :与includes相反,通过*匹配要忽略哪些文件
testResources : The testResources element block contains testResource elements. Their definitions are similar to resource elements, but are naturally used during test phases. The one difference is that the default (Super POM defined) test resource directory for a project is ${basedir}/src/test/resources. Test resources are not deployed.

reesource配置实例2:

<!--仅针对当前模块生效,如果他模块也需要,则具体模块再配置一次-->
<!--filtering: MAVEN提供了一种过滤机制,这种机制能够在资源文件被复制到目标目录的同时,当filtering = true时替换资源文件中的占位符;当filtering = false时不进行占位符的替换。-->
<!--下面将"src/main/java"目录下的所有的.yml文件进行打包,并替换所有的.yml文件中的占位符。 例如让plugin.yml里的${global.version}配置自动获取值-->
<!--注意:一旦配置了resource,就会替代默认机制,每个资源文件都要指定,否则打包后无法包含进jar包中-->
 <resource>
     <directory>src/main/resources</directory>
     <includes>
         <include>**/*.yml</include>
     </includes>
     <filtering>true</filtering>
 </resource>

根据不同环境打包不同配置

使用profiles可为maven命令执行时,激活不同的变量

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <package.environment>dev</package.environment>
        </properties>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <package.environment>test</package.environment>
        </properties>
    </profile>
    <profile>
        <id>product</id>
        <properties>
            <package.environment>product</package.environment>
        </properties>
    </profile>
</profiles>

执行打包命令

mvn clean package
mvn clean package -Pdev
mvn clean package -Ptest
mvn clean package -Pproduct

使用属性

1.内置属性
${basedir} 表示项目根目录,即包含pom.xml文件的目录
${version} 等同于 ${project.version} 或者 ${pom.version} 表示项目版本

2.POM属性
${project.build.sourceDirectory}:项目的主源码目录,默认为src/main/java/.
${project.build.testSourceDirectory}:项目的测试源码目录,默认为/src/test/java/.
${project.build.directory}:项目构建输出目录,默认为target/.
${project.build.outputDirectory}:项目主代码编译输出目录,默认为target/classes/.
${project.build.testOutputDirectory}:项目测试代码编译输出目录,默认为target/testclasses/.
${project.groupId}:项目的groupId.
${project.artifactId}:项目的artifactId.
${project.version}:项目的version,等同于${version}
${project.build.finalName}:项目打包输出文件的名称,默认为${project.artifactId}${project.version}.

3.自定义属性
在pom中<properties>元素下自定义的Maven属性,例如:
<properties>
	<swagger.version>2.2.2</swagger.version>
</properties>
    
4.Settings属性
与POM属性同理。如${settings.localRepository}指向用户本地仓库的地址

5.Java系统属性
*所有Java系统属性都可以使用Maven属性引用,例如${user.home}指向了用户目录。
*可以通过命令行mvn help:system查看所有的Java系统属性
    
6.环境变量属性
所有环境变量都可以使用以env.开头的Maven属性引用。
例如${env.JAVA_HOME}指代了JAVA_HOME环境变量的值。
也可以通过命令行mvn help:system查看所有环境变量。
${env.M2_HOME } returns the Maven2 installation path. 代表Maven2的安装路径
${java.home } specifies the path to the current JRE_HOME environment use with relative paths to get for example:
<jvm>${java.home}../bin/java.exe</jvm>
    
7.父级工程属性
*上级工程的pom中的变量用前缀 ${project.parent } 引用.
*上级工程的版本也可以这样引用: ${parent.version }.maven的变量

指定项目java编译版本

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

Maven插件

基础概念:

Maven 有以下三个标准的生命周期:
    *clean:项目清理的处理
    *default(或 build):项目部署的处理
    *site:项目站点文档创建的处理

每个生命周期中都包含着一系列的阶段(phase)。这些 phase 就相当于 Maven 提供的统一的接口,然后这些 phase 的实现由 Maven 的插件来完成。

所以说 Maven 生命周期的每一个阶段的具体实现都是由 Maven 插件实现的。

Maven 实际上是一个依赖插件执行的框架,每个任务实际上是由插件完成。Maven 插件通常被用来:
	*创建 jar 文件
    *创建 war 文件
    *编译代码文件
    *代码单元测试
    *创建工程文档
    *创建工程报告

打包按时命名插件

....

使用特殊字符

在maven 中不能直接使用 < 、>、"、'、& 字符

解决方案1:使用转义字符

字符代码
<& l t ;
>& g t ;
"& q u o t ;
& a p o s ;
&& a m p ;

解决方案2 : 使用CDATA区(推荐)

<![CDATA[任意内容]]

例如:
<mybatis.generator.jdbcURL>
<![CDATA[jdbc:mysql://10.0.0.107:3306/issuetracker?useUnicode=true&characterEncoding=utf8]]>
</mybatis.generator.jdbcURL>

配置镜像加速

修改配置文件:maven安装目录/conf/settings.xml

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <mirrors>
		<!-- 配置阿里云镜像加速 -->    
        <mirror>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
 
 		<!-- 其他加速配置,可选 -->
        <mirror>
            <id>uk</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://uk.maven.org/maven2/</url>
        </mirror>

        <mirror>
            <id>CN</id>
            <name>OSChina Central</name>
            <url>http://maven.oschina.net/content/groups/public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
 
 		<!-- 内部使用的中央仓库 -->
        <mirror>
            <id>nexus</id>
            <name>internal nexus repository</name>
            <!-- <url>http://192.168.1.100:8081/nexus/content/groups/public/</url>-->
            <url>http://repo.maven.apache.org/maven2</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
 
    </mirrors>
</settings>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值