Maven插件的应用

Maven工程的标准架构

目录 备注
${basedir} 存放 pom.xml和所有的子目录
${basedir}/src/main/resources 项目的资源,如spring配置文件,properties 资源文件等
${basedir}/src/main/webapps web项目特有
${basedir}/src/test/java 项目的测试类,比如说 JUnit代码、TestNg代码
${basedir}/src/test/resources 测试代码使用的资源
  • 插件一 maven-resources-plugin

Maven可以区别对待Java代码文件和资源文件,默认的主资源文件目录是src/main/resources,我们可以通过这个插件实现资源文件过滤。资源文件过滤的意思是指我们可以在资源文件里用使用占位符${propertyName},然后开启对资源文件的过滤,pom.xml里再统一设置所有{propertyName}对应的值,就可以在构建过程中将值替换掉资源文件中对应的${propertyName},实现了代码配置分离、做到了参数的统一维护

示例用法

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>properties/*.properties</include>
        </includes>
        <filtering>true</filtering>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>*.xml</include>
            <include>mapper/**/*.xml</include>
            <include>mysqlMapper/**/*.xml</include>
            <include>*.properties</include>
        </includes>
    </resource>
</resources>
<properties>
    <runtime.env>local</runtime.env>
</properties>

我们的主应用集成后,会根据实际要求部署到不同的环境中,比如联调环境、测试环境、压力环境、预发布环境、生产环境等,而这些环境上的资源配置信息显然是不一样的,针对每套环境,每个具体占位符${propertyName}都会有不同的值,而这种场景可以使用Maven的profile来支持,每个profile都可以独立维护一套参数值,在mvn package的时候灵活指定;此外,maven也支持在package的时候指定多个profile,这个特性在执行自动部署的时候特别有用。使用这个插件,我们的项目可以做到多环境支持,参考命令

mvn package -Pnocheck,env-test 

示例用法

<profiles>
    <profile>
        <id>nocheck</id>
        <properties>
            <skipTests>true</skipTests>
            <checkstyle.skip>true</checkstyle.skip>
            <license.skip>true</license.skip>
            <notice.skip>true</notice.skip>
            <versions.skip>true</versions.skip>
        </properties>
    </profile>
    <profile>
        <!-- 本地环境,默认是windows -->
        <id>local</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <runtime.env>local</runtime.env>
        </properties>
    </profile>
    <profile>
        <id>env-test</id>
        <properties>
            <runtime.env>env-test</runtime.env>
        </properties>
    </profile>
</profiles>
  • 插件二 maven-jar-plugin

当我们将项目模块化后,有一些通用的资源文件基本上大多数模块都会用到,比如log4j.properties,jdbc.properties等,模块中有了这些资源文件,我们才能单独对该模块进行开发、调试。默认情况下maven-jar-plugin会将这些资源文件全部package成一个jar包进行发布,如果这样的jar包集成到一个主应用中部署,运行,很可能导致主应用的配置不生效,我称之为配置混乱,为了解决这个问题,可以开启maven-jar-plugin的排除功能,在执行mvn package之前排除指定的资源文件。

示例用法

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>applicationContext.xml</exclude>
            <exclude>properties/**</exclude>
            <exclude>log4j.properties</exclude>
        </excludes>
    </configuration>
</plugin>
  • 插件三 maven-war-plugin

项目如果是web主应用,我们可以使用maven-war-plugin来对webapps下各类文件进行过滤。用法参考maven-resources-plugin

示例用法

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <warName>demo-Rest</warName>
        <webResources>
            <resource>
                <directory>src/main/webapp/WEB-INF</directory>
                <filtering>true</filtering>
                <targetPath>WEB-INF</targetPath>
                <includes>
                    <include>web.xml</include>
                </includes>
            </resource>
        </webResources>
    </configuration>
</plugin>
  • 插件四 properties-maven-plugin

  • 插件五 maven-assembly-plugin

  • 插件六 maven-shade-plugin

  • 插件七 versions-maven-plugin

https://blog.csdn.net/enweitech/article/details/67631997

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 137,173评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,361评论 6 342
  • 所有项目的构建都是有生命周期的,这个生命周期包括:项目清理、初始化、编译、测试、打包、集成测试、验证、部署、站点生...
    zlcook阅读 3,087评论 0 21
  • 转自:http://www.cnblogs.com/crazy-fox/archive/2012/02/09/23...
    晴天哥_王志阅读 2,479评论 2 27
  • 人民的名义剧终了,看到最后祈同伟颤抖着嘴唇说回不去了,我突然开始同情和理解他,看电视前面是真的很讨厌他,虚伪,势力...
    NONO果阅读 475评论 0 0

友情链接更多精彩内容