浅谈Maven在项目中的使用

        对于maven的概念一直处于只是解决传统导入jar包的繁琐中,随着maven的发展,能够提供的功能越来越全面,趁着项目中用到了一些maven稍微复杂点的应用完全摸不着头脑,静下心来好好看了一下,下面是这次整理的一些总结。

  • 1、maven基本概念
    • groupId
      • 组织Id,一般商业的都用是com,比如我们项目中com.bonade
    • artifactId
      • 项目Id,一般根据项目名称来定义,比如Bonade-XX-Service
    • version
      • 版本号,用来定义groupId+artifactId的版本号,可能有多个版本,这三个属性定义了一个依赖在maven库中的坐标
    • packaging
      • 打包方式,默认为jar,可选war,或者其他的
    • name
      • 项目名称,一般与artifactId一致
    • url
      • 项目主页地址
    • description
      • 项目描述
  • 以上属性是一个maven项目最基本的属性,除了groupId、artifactId、version是必选属性,其他都为可选属性。
  • <modelVersion>4.0.0</modelVersion>
    <artifactId>Bonade-IM</artifactId>
    <packaging>pom</packaging>
    <name>Bonade-IM</name>
    <description>IM社交系统</description>

     

 

  • 2、常用配置
    • modules
      • 聚合项目,为了更方面的管理分层项目以及管理通用依赖
      • <modules>
        	<module>Bonade-IM-Facade</module>
        	<module>Bonade-IM-Service</module>
        	<module>Bonade-IM-Web</module>
        </modules>

         

    • parent
      • 父依赖并且可以继承他的绝大部分属性,除name、artifactId等少数几个元素
      • <!-- 子module可以继承除了少部分属性之外的大部分属性,比如artifactId、name、prerequisites都是不能继承的 -->
        <parent>
        	<groupId>com.bonade</groupId>
        	<artifactId>parent</artifactId>
        	<version>3.0.0-SNAPSHOT</version>
        	<!-- 手动指定parentpom文件位置,默认是../即上一级目录 -->
        	<relativePath>../Bonade-common/</relativePath>
        </parent>

         

    • properties
      • 自定义属性值,可以在本级pom文件以及子module中引用
      • <properties>
        	<!-- <ibase4J-Common.version>1.1.0-SNAPSHOT</ibase4J-Common.version> -->
        	<Bonade-Common.version>3.0.1-SNAPSHOT</Bonade-Common.version>
        	<!-- 开发阶段配置文件位置, 发布时打包加参数:-P product -->
        	<sys.jdbc.config.path>classpath:config/jdbc.properties</sys.jdbc.config.path>
        	<system.config.path>classpath:config/system.properties</system.config.path>
        	<!-- 编码 -->
        	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        	<!-- spring版本号 -->
        	<!-- <spring.version>5.0.5.RELEASE</spring.version> -->
        	<spring.version>5.0.5.RELEASE</spring.version>
        	<spring-session.version>1.3.1.RELEASE</spring-session.version>
        	<!-- <spring-data-redis.version>2.0.5.RELEASE</spring-data-redis.version> -->
        	<spring-data-redis.version>1.8.7.RELEASE</spring-data-redis.version>
        </properties>

         

    • profiles
      • 可以手动配置多个profile,在打包时可以通过-P profile的id指定使用哪种配置
      • <profiles>
        	<profile>
        		<!-- 本地环境 -->
        		<id>local</id>
        		<properties>
        			<profiles.active>local</profiles.active>
        		</properties>
        		<activation>
        			<!-- 默认激活local -->
        			<activeByDefault>true</activeByDefault>
        		</activation>
        	</profile>
        	<profile>
        		<!-- 开发环境 -->
        		<id>dev</id>
        		<properties>
        			<profiles.active>dev</profiles.active>
        		</properties>
        	</profile>
        	<profile>
        		<!-- 测试环境 -->
        		<id>test</id>
        		<properties>
        			<profiles.active>test</profiles.active>
        		</properties>
        	</profile>
        </profiles>

         

    • build
      • build标签主要是指定在构建时指定需要加载的资源路径以及构建时需要使用的插件等等
      • <build>
        	<!-- 构建项目名称 -->
        	<finalName>${project.name}</finalName>
        	<!-- 构建项目需要加载的资源-->
        	<resources>
        		<resource>
        			<!-- 资源路径 -->
        			<directory>src/main/resources</directory>
        			<!-- 不加载以下路径的文件 -->
        			<excludes>
        				<exclude>config/local/*</exclude>
        				<exclude>config/dev/*</exclude>
        				<exclude>config/test/*</exclude>
        			</excludes>
        			<!-- 是否以filter中的配置文件替换其他文件中的属性引用 -->
        			<!-- 或者以properties中配置的以及profile中配置的properties替换其他配置文件中的属性引用 -->
        			<filtering>true</filtering>
        		</resource>
        		<resource>
        			<!-- 引用profile中配置的properties profiles.active -->
        			<directory>src/main/resources/config/${profiles.active}</directory>
        			<!-- 指定加载的资源文件的存放位置,默认为WEB-INF下的classes目录中,配置的config代表在classes创建目录config存放资源文件 -->
        			<targetPath>config</targetPath>
        		</resource>
        	</resources>
        	<!-- 构建项目时使用的插件 -->
        	<plugins>
        		<!-- tomcat7-maven插件,嵌入式的tomcat,无需重新配置或者指定tomcat -->
        		<plugin>
        			<groupId>org.apache.tomcat.maven</groupId>
        			<artifactId>tomcat7-maven-plugin</artifactId>
        			<version>2.2</version>
        			<executions>
        				<execution>
        					<id>run-war-only</id>
        					<phase>pre-integration-test</phase>
        					<goals>
        						<goal>run-war-only</goal>
        					</goals>
        				</execution>
        			</executions>
        			<configuration>
        				<warDirectory>target/${project.name}</warDirectory>
        				<path>/</path>
        				<contextReloadable>true</contextReloadable>
        				<uriEncoding>UTF-8</uriEncoding>
        				<port>${server.port}</port>
        				<url>http://localhost:${server.port}/manager</url>
        				<server>tomcat</server>
        				<username>admin</username>
        				<password>admin</password>
        				<systemProperties>
        					<webapps>
        						<webapp>
        							<groupId>${project.name}</groupId>
        							<artifactId>${project.name}</artifactId>
        							<version>${project.version}</version>
        							<type>${project.packaging}</type>
        							<asWebapp>true</asWebapp>
        							<contextPath>/</contextPath>
        						</webapp>
        					</webapps>
        				</systemProperties>
        			</configuration>
        		</plugin>
        		
        		<!-- maven编译插件,以把用于指定源文件和编译文件使用的jdk版本,以及使用的字符集编码 -->
        		<plugin>
        			<groupId>org.apache.maven.plugins</groupId>
        			<artifactId>maven-compiler-plugin</artifactId>
        			<configuration>
        				<source>1.8</source>
        				<target>1.8</target>
        				<encoding>UTF-8</encoding>
        				<testIncludes>
        					<testInclude>none</testInclude>
        				</testIncludes>
        			</configuration>
        		</plugin>
        	</plugins>
        </build>

         

    • resources
      • 用于指定构建时加载的资源,具体参考build部分的配置。
    • plugins
      • 用于指定构建需要使用的插件,具体参考build部分的配置。
      • 也可以在parent中配置pluginManagement来指定一些通用公用属性,子module就不用简单的引用了。
    • repositories
      • 用于指定依赖包的仓库地址,repositories也可以在profile中使用,表示使用此配置时使用指定的仓库。
      • <repositories>
        	<!-- 配置nexus远程仓库 -->
        	<repository>
        		<id>public</id>
        		<name>Nexus Repository</name>
        		<url>http://192.168.0.248:8081/nexus/content/groups/public/</url>
        		<!-- 依赖的release版本 -->
        		<releases>
        			<!-- true代表允许使用依赖的release版本 -->
        			<enabled>true</enabled>
        		</releases>
        		<!-- 依赖的snapshots版本 -->
        		<snapshots>
        			<!-- true代表允许使用依赖的snapshots版本 -->
        			<enabled>true</enabled>
        		</snapshots>
        	</repository>
        </repositories>

         

    • dependencies
      • 我们使用的最多的标签,指定依赖集合。我们也在parent中使用dependencyManagement来配置公共依赖属性,子module中可以不再指定version即可依赖,主要是为了保证各module间的依赖版本统一。

 

  • 3、maven常用插件
    • maven-compiler-plugin
      • 详情参考build部分的配置,具体配置可以参考maven-compiler-plugin官网文档。
    • tomcat7-maven-plugin
      • 详情参考build部分的配置,具体配置可以参考maven-compiler-plugin官网文档。

 

  • 4、maven常用命令
    • clean
      • 清除build指定的outputDirectory中的代码,outputDirectory默认为${basedir}/target
    • package
      • 也就是通常所说的打包,以pom文件中指定packing构建项目
    • install
      • 通常所说的将package的包部署到本仓库,比如我们的例子中的Facade项目,install之后,Service和Web项目才能在本地仓库中根据maven坐标找到Facade的依赖。
    • -P
      • 指定构建时的配置
      • <profiles>
        	<profile>
        		<!-- 本地环境 -->
        		<id>local</id>
        		<properties>
        			<profiles.active>local</profiles.active>
        		</properties>
        		<activation>
        			<!-- 默认激活local -->
        			<activeByDefault>true</activeByDefault>
        		</activation>
        	</profile>
        	<profile>
        		<!-- 开发环境 -->
        		<id>dev</id>
        		<properties>
        			<profiles.active>dev</profiles.active>
        		</properties>
        	</profile>
        	<profile>
        		<!-- 测试环境 -->
        		<id>test</id>
        		<properties>
        			<profiles.active>test</profiles.active>
        		</properties>
        	</profile>
        </profiles>
        
        <!-- 表示使用profiles配置中id为local的配置构件项目,并且profile中声明了profiles.active为local的属性值 -->
        clean package -P local tomcat7:run-war-only -f pom-im-web-server.xml
        
        <!-- 而在后面配置的构建中指定了加载${profiles.active}目录下的配置文件 -->
        <resource>
        	<!-- 引用profile中配置的properties profiles.active -->
        	<directory>src/main/resources/config/${profiles.active}</directory>
        	<!-- 指定加载的资源文件的存放位置,默认为WEB-INF下的classes目录中,配置的config代表在classes创建目录config存放资源文件 -->
        	<targetPath>config</targetPath>
        </resource>

         

    • -f
      • 指定使用的pom文件,网上搜索了一些,并没有搜到相关的详细解释
      • 个人的理解是,以指定的pom文件覆盖${basedir}中已有的同标签的配置项(没有验证,有懂的小伙伴请留言)

 

  • 5、maven管理多环境配置文件
    • 基于多个环境的jdbc的简单配置
      • 首先在pom文件中创建三个profile
        • <profiles>
          	<profile>
          		<!-- 本地环境 -->
          		<id>local</id>
          		<properties>
          			<profiles.active>local</profiles.active>
          		</properties>
          		<activation>
          			<!-- 默认激活local -->
          			<activeByDefault>true</activeByDefault>
          		</activation>
          	</profile>
          	<profile>
          		<!-- 开发环境 -->
          		<id>dev</id>
          		<properties>
          			<profiles.active>dev</profiles.active>
          		</properties>
          	</profile>
          	<profile>
          		<!-- 测试环境 -->
          		<id>test</id>
          		<properties>
          			<profiles.active>test</profiles.active>
          		</properties>
          	</profile>
          </profiles>
      • 在需要添加配置文件的项目中创建三个环境对应的配置文件
        • 4ac08660b5d2f4d91c9b6c893e7f509fdbc.jpg
      • 然后在build中指定resources,excludes是为了不加载不需要的配置文件,后面的resource指定只加载指定profile的jdbc
        • <resources>
          	<resource>
          		<!-- 资源路径 -->
          		<directory>src/main/resources</directory>
          		<!-- 不加载以下路径的文件 -->
          		<excludes>
          			<exclude>config/local/*</exclude>
          			<exclude>config/dev/*</exclude>
          			<exclude>config/test/*</exclude>
          		</excludes>
          		<!-- 是否以filter中的配置文件替换其他文件中的属性引用 -->
          		<!-- 或者以properties中配置的以及profile中配置的properties替换其他配置文件中的属性引用 -->
          		<filtering>true</filtering>
          	</resource>
          	<resource>
          		<!-- 引用profile中配置的properties profiles.active -->
          		<directory>src/main/resources/config/${profiles.active}</directory>
          		<!-- 指定加载的资源文件的存放位置,默认为WEB-INF下的classes目录中,配置的config代表在classes创建目录config存放资源文件 -->
          		<targetPath>config</targetPath>
          	</resource>
          </resources>

           

      • spring-config中指定jdbc的加载路径,config目录是在resource中指定的targetPath
        • <property name="locations">
          	<list>
          		<value>classpath:config/jdbc.properties</value>
          		<value>${system.config.path}</value>
          	</list>
          </property>

           

      • 使用clean package -P local命令加载指定的profile
        • 7159464b90f80494df9e999682c7f16ddbc.jpg

 

  • 6、maven仓库优先级顺序
    • 优先级顺序如下
      • 本地仓库 >profile > pom中的repository > mirror
      • settings中的mirrors中的mirror加载应该时按照先后顺序的(没有验证)
      • mirror的mirrorOf尽量不要使用*,一旦配置为*,代表是所有仓库的镜像且优先级最高,将会无视其他镜像。
      • <mirror>
        	<id>alimaven</id>
        	<name>aliyun maven</name>
        	<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        	<mirrorOf>central</mirrorOf>
        </mirror>
        <mirror>
        	<id>ui</id>
        	<mirrorOf>central</mirrorOf>
        	<name>Human Readable Name for this Mirror.</name>
        	<url>http://uk.maven.org/maven2/</url>
        </mirror>
        <mirror>
        	<id>sonatype</id>
        	<name>sonatype Central</name>
        	<url>http://oss.sonatype.org/content/groups/public/</url>
        	<mirrorOf>central</mirrorOf>
        </mirror>

         

 

  • 7、pom.xml与settings.xml
    • pom文件中与settings中配置的覆盖问题
      • settings中的同属性配置会覆盖pom文件中的配置
      • <!-- settings.xml中配置了activeProfiles为dev,无论在maven命令中怎么指定-P xxx,都会以settings中配置的为准 -->
        <!-- 这个问题坑了我好几个小时 -->
        <activeProfiles>
        	<activeProfile>dev</activeProfile>
        </activeProfiles>
  • 8、上传项目依赖至nexus私服
    • 配置nexus私服地址以及账户密码
      • pom.xml
        <distributionManagement>
        	<!--  maven会根据模块的版本号(pom文件中的version)中是否带有-SNAPSHOT来判断是快照版本还是正式版本 -->
        	<repository>
        		<id>nexus-releases</id>
        		<name>Nexus Releases Respository</name>
        		<url>http://ip:port/nexus/content/repositories/releases</url>
        	</repository>
        	<snapshotRepository>
        		<id>nexus-snapshots</id>
        		<name>Nexus Snapshots Repository</name>
        		<url>http://ip:port/nexus/content/repositories/snapshots</url>
        	</snapshotRepository>
        </distributionManagement>
        
        settings.xml
        <!-- 配置对应的server,id与distributionManagement中repository的id保持一致,username、password就是nexus私服的账户密码 -->
        <servers>
        	<server>
        		<id>nexus-releases</id>
        		<username>username</username>
        		<password>xxxxxx</password>
        	</server>
        	<server>
        		<id>nexus-snapshots</id>
        		<username>username</username>
        		<password>xxxxxx</password>
        	</server>
        </servers>

         

    • 运行maven deploy即可上传至配置nexus私服中

参考文章:
1.maven标签大全:https://www.cnblogs.com/sharpest/p/7738444.html
2.maven仓库优先级:https://blog.csdn.net/zcc_0015/article/details/80254546
3.pom.xml与settings.xml:https://www.cnblogs.com/xrq730/p/5530069.html

转载于:https://my.oschina.net/u/2452759/blog/1936718

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值