A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 汪一汪 初级黑马   /  2019-6-21 12:47  /  808 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

Maven高级Maven常用命令
clean:清理
compile:编译
test:测试
package:打包
install:安装
Maven的依赖范围
依赖范围
对于编译classpath有效
对于测试classpath有效
对于运行时classpath有效
例子

compileYYYspring-core
test-Y-Junit
providedYY-servlet-api
runtime-YYJDBC驱动
systemYY-本地的,maven仓库之外的类库依赖冲突Maven提供的依赖调节原则
第一声明者优先原则:产生依赖冲突时,Maven优先使用靠前的依赖,此时可以通过调换位置解决冲突
路径近者优先原则:若同一依赖的不同版本分别是直接导入的依赖和依赖传递的依赖,优先使用直接导入的依赖
排除依赖
使用exclusions可以将依赖传递的冲突依赖直接排除
<!--排除依赖-->
<exclusions>
    <exclusion>
        <!--厂商-->
       <artifactId>org.springframework</artifactId>
        <!--包名-->
        <groupId>spring-beans</groupId>
    </exclusion>
</exclusions>版本锁定
使用dependencyManagement将有冲突的依赖包裹,Maven默认使用包裹起来的版本,且后面的依赖可以不写版本号
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
    </dependencies>
</dependencyManagement>插件排除
File >> Settings >> Plugins >> 搜索Maven Helper >> 重启IDE >> 点击项目pom.xml >> 删除冲突的依赖(conflicts)
搭建Maven私服windows下安装nexus
打开cmd窗口并进入下载安装好的nexus的bin目录下,执行nexus.bat install命令安装服务(注意需要以管理员身份运行cmd命令)
在命令行执行nexus.bat start命令启动nexus
访问 http://localhost:8081/nexus ,进行登录,默认账号admin,密码admin123
linux下安装nexus
解压
tar -zvxf nexus-3.10.0-04-unix.tar.gz
解压的文件夹移到nexus(自动创建)
mv nexus-3.10.0-04/ /usr/local/nexus
防火墙开启8081端口,该端口是nexus的默认端口(在/usr/local/nexus/etc/nexus-default.properties文件中)
vi  /etc/sysconfig/iptables
在打开的文件中加入如下内容
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8081 -j ACCEPT
启动
./nexus run  & ./nexus start
成功页面
#启动成功会有如下信息
WARNING: ************************************************************
WARNING: Detected execution as "root" user.  This is NOT recommended!
WARNING: ************************************************************
Starting nexusnexus仓库类型
hosted,宿主仓库,部署自己的jar到这个类型的仓库,包括Releases和Snapshots两部分,Releases为公司内部发布版本仓库、 Snapshots为公司内部测试版本仓库
proxy,代理仓库,用于代理远程的公共仓库,如maven中央仓库,用户连接私服,私服自动去中央仓库下载jar包或者插件
group,仓库组,用来合并多个hosted/proxy仓库,通常我们配置自己的maven连接仓库组
virtual(虚拟):兼容Maven1版本的jar或者插件

项目发布到maven私服
配置maven的settings.xml文件(注意:一定要在idea工具中引入的maven的settings.xml文件中配置 )
<server>
    <id>releases</id>
    <username>admin</username>   
    <password>admin123</password>
</server>
<server>
    <id>snapshots</id>
    <username>admin</username>
    <password>admin123</password>
</server>
配置项目的pom.xml文件
<distributionManagement>
<repository>
   <id>releases</id>
   <url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
   <id>snapshots</id>               <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>    </snapshotRepository>
</distributionManagement>
执行mvn deploy命令
从私服下载jar到本地仓库
在maven的settings.xml文件中配置下载模板
<profile>
    <id>dev</id>
        <repositories>
        <repository>
            <id>nexus</id>
        <!--仓库地址,即nexus仓库组的地址-->
            <url>
            http://localhost:8081/nexus/content/groups/public/</url>
        <!--是否下载releases构件-->
            <releases>
            <enabled>true</enabled>
            </releases>
        <!--是否下载snapshots构件-->
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        </repositories>
<pluginRepositories>
    <!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件 -->
    <pluginRepository>
        <id>public</id>
        <name>Public Repositories</name>
        <url>
        http://localhost:8081/nexus/content/groups/public/</url>
        </pluginRepository>
        </pluginRepositories>
</profile>
在maven的settings.xml文件中配置激活下载模板
<activeProfiles>
    <activeProfile>dev</activeProfile>
</activeProfiles>将第三方jar安装到本地仓库和maven私服安装到本地仓库
下载第三方jar包
mvn install命令进行安装
  mvn install:install-file -Dfile=ojdbc14-10.2.0.4.0.jar -DgroupId=com.oracle -DartifactId=ojdbc14 –

  Dversion=10.2.0.4.0 -Dpackaging=jar
查看本地maven仓库,确认安装是否成功
安装到Maven私服
下载第三方jar包
在maven的settings.xml配置文件中配置第三方仓库的server信息
<server>
  <id>thirdparty</id>
  <username>admin</username>
  <password>admin123</password>
</server>
执行mvn deploy命令进行安装
mvn deploy:deploy-file -Dfile=ojdbc14-10.2.0.4.0.jar -DgroupId=com.oracle -DartifactId=ojdbc14 –

Dversion=10.2.0.4.0 -Dpackaging=jar –

Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马