私服搭建
前提:配置好java环境变量。 </br> 不涉及maven的其他知识。
1.下载安装
最新nexus下载地址:http://www.sonatype.org/nexus/go
<!--more -->
解压:
cd /opt/softwares/nexus
tar -zxvf nexus-latest-bundle.tar.gz
ll
drwxr-xr-x 8 root root 4096 5月 28 2014 nexus-2.8.1-01
drwxr-xr-x 3 root root 4096 5月 28 2014 sonatype-work
解压后会有两个文件夹 出现两个文件夹:nexus-oss-webapp-1.8.0和sonatype-work,前者包含了nexus的运行环境和应用程序,后者包含了你自己的配置和数据。
2.启动nexus
cd nexus-2.8.1-01/
bin/nexus start
注意:root 用户启动会提示配置RUN_AS_USER 修改:
cd bin/
vim nexus
RUN_AS_USER=root
启动:
[root@hadoop bin]# nexus start
[root@hadoop bin]# jps
8650 Jps
2859 JswLauncher // nexus
3.配置nexus
访问网址:http://yourhostname:8081/nexus
右上角以admin登陆,默认用户名/密码:admin/admin123。
3.1配置邮箱
由于私服基本上配置一次就可以,很长时间不会操作,配置邮箱用于找回密码。
3.2配置下载中央仓库的jar包
配置全局的Public Repositories
配置Central 仓库,并更新下索引
3rd party、Snapshots、Releases这三个,分别用来保存第三方jar、项目组内部的快照、项目组内部的发布版.
4.手动添加第三方jar包
点击Upload Artifact(s)按钮提交后即上传。
5.配置远程仓库
5.1下载
在本地maven的setting.xml中配置
<profiles>
<profile>
<id>dev</id>
<!--仓库地址-->
<repositories>
<repository>
<id>nexus</id>
<url>http://192.168.226.128:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
<updatePolicy></updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<!--插件库地址-->
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://192.168.226.128:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!--激活生效dev-->
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
<!--用于把本地jar包推送到私服,看下边-->
<servers>
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
此时就可以从远程仓库中下载我们自己上传的第三方jar包,下载到本地库中,需要在pom文件中加入依赖。
<dependency>
<groupId>com.mainbo</groupId>
<artifactId>jypt-parent</artifactId>
<version>1.0.0</version>
</dependency>
5.2上传
此时如果想要 mvn clean install deploy 需要在 pom.xml 文件中加入
<distributionManagement>
<!-- 要和 setting.xml 配置对应 -->
<repository>
<id>releases</id>
<url>http://192.168.226.128:8081/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://192.168.226.128:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
从这里复制。
实验中遇到的问题: 推送私服的时候可能会遇到 无法执行 maven-deploy-plugin的问题 在pom.xml 工程中加入
<build>
<!-- To define the plugin version in your parent POM -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
<!-- To use the plugin goals in your POM or parent POM -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</build>
PS: 这只是简单的测试,实际生产环境中可能会遇到很多其他问题。
Maven的其他知识
1.Maven的基本命令
mvn clean -->表示运行清理操作(会默认把target文件夹中的数据清理)
mvn clean compile-->表示先运行清理之后运行编译,会见代码编译到target文件夹中
mvn clean test-->运行清理和测试
mvn clean package-->运行清理和打包
mvn clean install-->运行清理和安装,会将打好的包安装到本地仓库中,以便其他的项目可以调用
mvn clean deploy-->运行清理和发布(发布到私服上面)
2.Maven的依赖
2.1依赖包的查询
1、所有的依赖都是通过坐标来进行存储的(GAV-->groupId、artifactId、version) 2、有一些网上的仓库提供了坐标的查询(http://mvnrepository.com) 3、通过<dependencies>设置依赖
maven是如何搜索依赖的?首先会在本地仓库查询如果本地仓库没有,就去中央仓库查询
2.2传递的依赖性
1.依赖是会被传递 A-->C B-->A ==> B-->C(这种依赖是基于compile这个范围进行传递) 对于依赖的传递而言,主要是针对compile作用域传递 2.冲突解决
2.3依赖的范围
1、test范围指的是测试范围有效,在编译和打包时都不会使用这个依赖 2、compile范围指的是编译范围有效,在编译和打包时都会将依赖存储进去 3、provided依赖:在编译和测试的过程有效,最后生成war包时不会加入,诸如:servlet-api,因为servlet-api,tomcat等web服务器已经存在了,如果再打包会冲突 4、runtime在运行的时候依赖,在编译的时候不依赖 默认的依赖范围是compile
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
2.4聚合和继承
聚合: 当工程中有很多模块分层的时候,如果我们要打包需要一个一个的package,这样会比较麻烦,此时可以使用聚合来管理我们的工程。
3.Maven的生命周期
3套生命周期
1、clean
pre-clean 执行一些需要在clean之前完成的工作
clean 移除所有上一次构建生成的文件
post-clean 执行一些需要在clean之后立刻完成的工作
2、compile
validate
generate-sources
process-sources
generate-resources
process-resources 复制并处理资源文件,至目标目录,准备打包。
compile 编译项目的源代码。
process-classes
generate-test-sources
process-test-sources
generate-test-resources
process-test-resources 复制并处理资源文件,至目标测试目录。
test-compile 编译测试源代码。
process-test-classes
test 使用合适的单元测试框架运行测试。这些测试代码不会被打包或部署。
prepare-package
package 接受编译好的代码,打包成可发布的格式,如 JAR 。
pre-integration-test
integration-test
post-integration-test
verify
install 将包安装至本地仓库,以让其它项目依赖。
deploy 将最终的包复制到远程的仓库,以让其它开发人员与项目共享。
3、site
pre-site 执行一些需要在生成站点文档之前完成的工作
site 生成项目的站点文档
post-site 执行一些需要在生成站点文档之后完成的工作,并且为部署做准备
site-deploy 将生成的站点文档部署到特定的服务器上
当我们执行install时,install上边的所有周期都会执行。
3.1 插件
插件是maven的核心,所有执行的操作都是基于插件来完成的 为了让一个插件中可以实现众多的类似功能,maven为插件设定了目标,一个插件中有可能有多个目标 其实生命周期中的重要的每个阶段都是由插件的一个具体目标来执行的 jetty插件的使用 在uemis/pom.xml中添加
<project>
...
<plugins>
...
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webApp>
<contextPath>/web</contextPath>
</webApp>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>8787</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugins>
</project>
clean package jetty:run 启动.
百度云视频:Maven3实战。链接: http://pan.baidu.com/s/1geP8L4f 密码: b2eu