Maven实战

一、Pom.xml

1.坐标

  maven 的所有构件均通过坐标进行组织和管理。maven 的坐标通过 5 个元素进行定义,其中 groupId、artifactId、version 是必须的,packaging 是可选的(默认为jar),classifier 是不能直接定义的。

groupId:定义当前 Maven 项目所属的实际项目,跟 Java 包名类似,通常与域名反向一一对应。
artifactId:定义当前 Maven 项目的一个模块,默认情况下,Maven 生成的构件,其文件名会以 artifactId 开头。
version:定义项目版本。<version>节点包含-SNAPSHOT为snapshot版本(不稳定版本或者是在开发中版本)。
packaging:定义项目打包方式,如 jar,war,pom,zip ……,默认为 jar。
classifier:定义项目的附属构件,其中 sources 和 javadoc 就是这两个附属构件的 classifier。classifier 不能直接定义,通常由附加的插件帮助生成。

2 . 依赖

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>3.2.0.RELEASE</version>
        <type>jar</type>
        <scope>test</scope>
        <systemPath>${java.home}/lib/rt.jar</systemPath>
        <optional>false</optional>
        <exclusions>
            <exclusion></exclusion>
        </exclusions>
    </dependency>
</dependencies>

    type:依赖类型,对应构件中定义的 packaging,可不声明,默认为 jar;
    scope:依赖范围;
    optional:依赖是否可选;
    exclusions:排除传递依赖。

依赖范围:

Maven 对应的有三套 classpath:编译classpath、测试classpath,运行classpath。scope 选项的值,决定了该依赖构件会被引入到哪一个 classpath 中。

compile:编译依赖范围,默认值。此选项对编译、测试、运行三种 classpath 都有效,如 hibernate-core-3.6.5.Final.jar,表明在编译、测试、运行的时候都需要该依赖;
test:测试依赖范围。只对测试有效,表明只在测试的时候需要,在编译和运行时将无法使用该类依赖,如 junit;
provided:已提供依赖范围。编译和测试有效,运行无效。如 servlet-api ,在项目运行时,tomcat 等容器已经提供,无需 Maven 重复引入;
runtime:运行时依赖范围。测试和运行有效,编译无效。如 jdbc 驱动实现,编译时只需接口,测试或运行时才需要具体的 jdbc 驱动实现;
system:系统依赖范围。和 provided 依赖范围一致,需要通过 <systemPath> 显示指定,且可以引用环境变量;
import:导入依赖范围。使用该选项,通常需要 <type>pom</type>,将目标 pom 的 dependencyManagement 配置导入合并到当前 pom 的  dependencyManagement 元素。

3.maven打包生成doc

 <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <testSource>${java.version}</testSource>
                    <testTarget>${java.version}</testTarget>
                </configuration>
            </plugin>

            <!--配置生成Javadoc包-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.9.1</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <aggregate>true</aggregate>
                    <charset>UTF-8</charset>
                    <docencoding>UTF-8</docencoding>
                </configuration>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!--配置生成源码包-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

二、setting.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">  


	<!--本地仓库位置-->
	<!--<localRepository>D:\Software\maven-3.6.2\repository</localRepository>	-->

	<!-- 配置镜像 -->  
	<mirrors>     
		<!--
			id是唯一标识一个mirror
			name貌似没多大用,相当于描述
			url是官方的库地址
			mirrorOf代表了一个镜像的替代位置,例如central就表示代替官方的中央库。
			mirrorOf改为resp1,此时当前mirror只会拦截仓库resp1的依赖请求
			默认情况下配置多个mirror的情况下,只有第一个生效。那么我们可以将最后一个作为默认值,前面配置的使用环境变量动态切换。
			
		-->
		
		<mirror>
			<id>XXX</id>
			<name>Nexus Mirror</name>
			<url>http://XXX:8081/nexus/content/groups/public</url>
			<mirrorOf>${XXX}</mirrorOf>
		</mirror>

		<!-- 阿里云仓库 mvn help:effective-settings -Daliyun=central -->
		<mirror>
			<id>alimaven</id>
			<name>aliyun maven</name>
			<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
			<mirrorOf>central</mirrorOf>			
		</mirror>
		<!-- 中央仓库1 -->
		<mirror>
			<id>repo1</id>
			<mirrorOf>central</mirrorOf>
			<name>Human Readable Name for this Mirror.</name>
			<url>http://repo1.maven.org/maven2/</url>
		</mirror>
		<!-- 中央仓库2 -->
		<mirror>
			<id>repo2</id>
			<mirrorOf>central</mirrorOf>
			<name>Human Readable Name for this Mirror.</name>
			<url>http://repo2.maven.org/maven2/</url>
		</mirror>
	</mirrors>  

	<!-- settings.xml中的profile元素是pom.xml中profile元素的裁剪版本。它包含了activation, repositories, pluginRepositories 和 properties元素。  
	这里的profile元素只包含这四个子元素是因为这里只关心构建系统这个整体(这正是settings.xml文件的角色定位),而非单独的项目对象模型设置。    
	如果一个settings中的profile被激活,它的值会覆盖任何其它定义在POM中或者profile.xml中的带有相同id的profile。 -->   
	<profiles>  
		<profile>  
			<id>development</id>  
			<!-- 仓库。仓库是Maven用来填充构建系统本地仓库所使用的一组远程项目。而Maven是从本地仓库中使用其插件和依赖。  
		不同的远程仓库可能含有不同的项目,而在某个激活的profile下,可能定义了一些仓库来搜索需要的发布版或快照版构件。有了Nexus,这些应该交由Nexus完成 -->  
			<repositories>  
				<repository>  
					<id>central</id>  
					<!-- 虚拟的URL形式,指向镜像的URL,因为所有的镜像都是用的是nexus,这里的central实际上指向的是http://repos.d.xxx.com/nexus/content/groups/public -->  
					<url>http://central</url>  
					<!-- 表示可以从这个仓库下载releases版本的构件-->  
					<releases>
						<enabled>true</enabled>
					</releases>  
					<!-- 表示可以从这个仓库下载snapshot版本的构件 -->  
					<snapshots>
						<enabled>true</enabled>
					</snapshots>  
				</repository>  
			</repositories>  

			<!-- 插件仓库。仓库是两种主要构件的家。第一种构件被用作其它构件的依赖。这是中央仓库中存储大部分构件类型。  
		另外一种构件类型是插件。Maven插件是一种特殊类型的构件。由于这个原因,插件仓库独立于其它仓库。  
		pluginRepositories元素的结构和repositories元素的结构类似。每个pluginRepository元素指定一个Maven可以用来寻找新插件的远程地址。 -->    
			<pluginRepositories>  
				<pluginRepository>  
					<id>central</id>  
					<url>http://central</url>  
					<releases>
						<enabled>true</enabled>
					</releases>  
					<snapshots>
						<enabled>true</enabled>
					</snapshots>  
				</pluginRepository>  
			</pluginRepositories>  
		</profile>  

		<profile>  
			<!--this profile will allow snapshots to be searched when activated-->  
			<id>public-snapshots</id>  
			<repositories>  
				<repository>  
					<id>public-snapshots</id>  
					<!-- 虚拟的URL形式,指向镜像的URL,这里指向的是http://repos.d.xxx.com/nexus/content/groups/public-snapshots -->  
					<url>http://public-snapshots</url>  
					<releases>
						<enabled>false</enabled>
					</releases>  
					<snapshots>
						<enabled>true</enabled>
					</snapshots>  
				</repository>  
			</repositories>  
			<pluginRepositories>  
				<pluginRepository>  
					<id>public-snapshots</id>  
					<url>http://public-snapshots</url>  
					<releases>
						<enabled>false</enabled>
					</releases>  
					<snapshots>
						<enabled>true</enabled>
					</snapshots>  
				</pluginRepository>  
			</pluginRepositories>  
		</profile>  
	</profiles>  

	<!-- 激活的Profile。activation元素并不是激活profile的唯一方式。settings.xml文件中的activeProfile元素可以包含profile的id,  
	任何在activeProfile中定义的profile id,不论环境设置如何,其对应的profile都会被激活。如果没有匹配的profile,则什么都不会发生。  
	profile也可以通过在命令行,使用-P标记和逗号分隔的列表来显式的激活(如,-P test)。  
	要了解在某个特定的构建中哪些profile会激活,可以使用maven-help-plugin(mvn help:active-profiles)。 -->    
	<activeProfiles>  
		<!-- 没有显示激活public-snapshots -->  
		<activeProfile>development</activeProfile>  
	</activeProfiles>  


	<!-- 发布的服务器和密码,暂时未限制权限 -->  
	<servers>  
		<server>  
			<!-- 发布的位置在POM中配置,以ID为关联,有很多公用的信息需要配置在POM文件里,最佳实践是定义一个公司级别的root pom -->  
			<id>nexus</id>  
			<username>admin</username>  
			<password>admin123</password>  
		</server>  
		<server>  
			<id>user-release</id>
			<username>admin</username>  
			<password>admin123</password>  
		</server>  
		<server>  
			<id>user-snapshot</id>
			<username>admin</username>  
			<password>admin123</password>  
		</server>  
	</servers>  
</settings>  

 

三、发布项目到私服

Nexus 的仓库分为这么几类:

hosted 宿主仓库:主要用于部署无法从公共仓库获取的构件(如 oracle 的 JDBC 驱动)以及自己或第三方的项目构件;
proxy 代理仓库:代理公共的远程仓库;
virtual 虚拟仓库:用于适配 Maven 1;
group 仓库组:Nexus 通过仓库组的概念统一管理多个仓库,这样我们在项目中直接请求仓库组即可请求到仓库组管理的多个仓库。

私服默认密码:admin admin123

1.私服配置成可发布

Release版本的项目应该发布到Releases仓库中,对应的,Snapshot版本应该发布到Snapshots仓库中。Maven根据pom.xml文件中版本号<version>节点的属性是否包含-SNAPSHOT,来判断该项目是否是snapshot版本。如果是snapshot版本,在执行mvn deploy部署命令时,maven会自动将项目发布到Snapshots仓库。要发布项目,首先需要将Releases仓库和Snapshots仓库的“Deployment Policy”设置为“Allow Redeploy”:

2.setting.xml配置

                <server>  
			<id>user-release</id>
			<username>admin</username>  
			<password>admin123</password>  
		</server>  
		<server>  
			<id>user-snapshot</id>
			<username>admin</username>  
			<password>admin123</password>  
		</server>  

3.pom.xml配置

<distributionManagement>
        <snapshotRepository>
            <id>user-snapshot</id>
            <name>User Porject Snapshot</name>
            <url>http://XXX/nexus/content/repositories/snapshots/</url>
            <uniqueVersion>true</uniqueVersion>
        </snapshotRepository>
        <repository>
            <id>user-release</id>
            <name>User Porject Release</name>
            <url>http://XXX/nexus/content/repositories/releases/</url>
        </repository>
    </distributionManagement>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值